Class MyBatisDataAccessStrategy
- All Implemented Interfaces:
DataAccessStrategy
,RelationResolver
DataAccessStrategy
implementation based on MyBatis. Each method gets mapped to a statement. The name of the
statement gets constructed as follows: By default, the namespace is based on the class of the entity plus the suffix
"Mapper". This is then followed by the method name separated by a dot. For methods taking a PropertyPath
as
argument, the relevant entity is that of the root of the path, and the path itself gets as dot separated String
appended to the statement name. Each statement gets an instance of MyBatisContext
, which at least has the
entityType set. For methods taking a PropertyPath
the entityType if the context is set to the class of the
leaf type.- Author:
- Jens Schauder, Kazuki Shimizu, Oliver Gierke, Mark Paluch, Tyler Van Gorder, Milan Milanov, Myeonghyeon Lee, Chirag Tailor, Christopher Klein
-
Constructor Summary
ConstructorDescriptionMyBatisDataAccessStrategy
(org.apache.ibatis.session.SqlSession sqlSession, IdentifierProcessing identifierProcessing) Constructs aDataAccessStrategy
based on MyBatis. -
Method Summary
Modifier and TypeMethodDescription<T> void
acquireLockAll
(LockMode lockMode, Class<T> domainType) Acquire a lock on all aggregates of the given domain type.<T> void
acquireLockById
(Object id, LockMode lockMode, Class<T> domainType) Acquire a lock on the aggregate specified by id.long
Counts the rows in the table representing the given domain type.<T> long
Counts the rows in the table representing the given probe type, that match the givenquery
.static DataAccessStrategy
createCombinedAccessStrategy
(RelationalMappingContext context, JdbcConverter converter, org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations operations, org.apache.ibatis.session.SqlSession sqlSession, NamespaceStrategy namespaceStrategy, Dialect dialect) Create aDataAccessStrategy
that first checks for queries defined by MyBatis and if it doesn't find one uses aDefaultDataAccessStrategy
static DataAccessStrategy
createCombinedAccessStrategy
(RelationalMappingContext context, JdbcConverter converter, org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations operations, org.apache.ibatis.session.SqlSession sqlSession, Dialect dialect) Create aDataAccessStrategy
that first checks for queries defined by MyBatis and if it doesn't find one uses aDefaultDataAccessStrategy
void
Deletes multiple rows identified by the ids, from the table identified by the domainType.void
delete
(Iterable<Object> rootIds, org.springframework.data.mapping.PersistentPropertyPath<RelationalPersistentProperty> propertyPath) Deletes all entities reachable via propertyPath from the instances identified by rootIds.void
Deletes a single row identified by the id, from the table identified by the domainType.void
delete
(Object rootId, org.springframework.data.mapping.PersistentPropertyPath<RelationalPersistentProperty> propertyPath) Deletes all entities reachable via propertyPath from the instance identified by rootId.<T> void
Deletes all entities of the given domain type.void
deleteAll
(org.springframework.data.mapping.PersistentPropertyPath<RelationalPersistentProperty> propertyPath) Deletes all entities reachable via propertyPath from any instance.<T> void
deleteWithVersion
(Object id, Class<T> domainType, Number previousVersion) Deletes a single entity from the database and enforce optimistic record locking using the version property.<T> boolean
Determine whether there is an aggregate of typedomainType
that matches the providedQuery
.<T> boolean
existsById
(Object id, Class<T> domainType) returns if a row with the given id exists for the given type.<T> Iterable<T>
Loads all entities of the given type.<T> Iterable<T>
Loads all entities of the given type, paged and sorted.<T> Iterable<T>
Loads all entities of the given type, sorted.<T> Iterable<T>
Execute aSELECT
query and convert the resulting items to aIterable
.<T> Iterable<T>
Execute aSELECT
query and convert the resulting items to aIterable
.<T> Iterable<T>
findAllById
(Iterable<?> ids, Class<T> domainType) Loads all entities that match one of the ids passed as an argument.findAllByPath
(Identifier identifier, org.springframework.data.mapping.PersistentPropertyPath<? extends RelationalPersistentProperty> path) Finds all entities reachable via path.<T> T
Loads a single entity identified by type and id.<T> Optional<T>
Execute aSELECT
query and convert the resulting item to an entity ensuring exactly one result.<T> Object[]
insert
(List<InsertSubject<T>> insertSubjects, Class<T> domainType, IdValueSource idValueSource) Inserts the data of multiple entities.<T> Object
insert
(T instance, Class<T> domainType, Identifier identifier, IdValueSource idValueSource) Inserts the data of a single entity.void
setNamespaceStrategy
(NamespaceStrategy namespaceStrategy) Set a NamespaceStrategy to be used.<S> boolean
Updates the data of a single entity in the database.<S> boolean
updateWithVersion
(S instance, Class<S> domainType, Number previousVersion) Updates the data of a single entity in the database and enforce optimistic record locking using thepreviousVersion
property.
-
Constructor Details
-
MyBatisDataAccessStrategy
public MyBatisDataAccessStrategy(org.apache.ibatis.session.SqlSession sqlSession, IdentifierProcessing identifierProcessing) Constructs aDataAccessStrategy
based on MyBatis.Use a
SqlSessionTemplate
forSqlSession
or a similar implementation tying the session to the proper transaction. Note that the resultingDataAccessStrategy
only handles MyBatis. It does not include the functionality of theDefaultDataAccessStrategy
which one normally still wants. UsecreateCombinedAccessStrategy(RelationalMappingContext, JdbcConverter, NamedParameterJdbcOperations, SqlSession, NamespaceStrategy, Dialect)
to create such aDataAccessStrategy
.- Parameters:
sqlSession
- Must be non null.identifierProcessing
- theIdentifierProcessing
applied toSqlIdentifier
instances in order to turn them intoString
-
-
Method Details
-
createCombinedAccessStrategy
public static DataAccessStrategy createCombinedAccessStrategy(RelationalMappingContext context, JdbcConverter converter, org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations operations, org.apache.ibatis.session.SqlSession sqlSession, Dialect dialect) Create aDataAccessStrategy
that first checks for queries defined by MyBatis and if it doesn't find one uses aDefaultDataAccessStrategy
-
createCombinedAccessStrategy
public static DataAccessStrategy createCombinedAccessStrategy(RelationalMappingContext context, JdbcConverter converter, org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations operations, org.apache.ibatis.session.SqlSession sqlSession, NamespaceStrategy namespaceStrategy, Dialect dialect) Create aDataAccessStrategy
that first checks for queries defined by MyBatis and if it doesn't find one uses aDefaultDataAccessStrategy
-
setNamespaceStrategy
Set a NamespaceStrategy to be used.- Parameters:
namespaceStrategy
- Must be non null
-
insert
public <T> Object insert(T instance, Class<T> domainType, Identifier identifier, IdValueSource idValueSource) Description copied from interface:DataAccessStrategy
Inserts the data of a single entity. Referenced entities don't get handled.- Specified by:
insert
in interfaceDataAccessStrategy
- Type Parameters:
T
- the type of the instance.- Parameters:
instance
- the instance to be stored. Must not benull
.domainType
- the type of the instance. Must not benull
.identifier
- information about data that needs to be considered for the insert but which is not part of the entity. Namely, references back to a parent entity and key/index columns for entities that are stored in aMap
orList
.idValueSource
- theIdValueSource
for the insert.- Returns:
- the id generated by the database if any.
-
insert
public <T> Object[] insert(List<InsertSubject<T>> insertSubjects, Class<T> domainType, IdValueSource idValueSource) Description copied from interface:DataAccessStrategy
Inserts the data of multiple entities.- Specified by:
insert
in interfaceDataAccessStrategy
- Type Parameters:
T
- the type of the instance.- Parameters:
insertSubjects
- the subjects to be inserted, where each subject contains the instance and its identifier. Must not benull
.domainType
- the type of the instance. Must not benull
.idValueSource
- theIdValueSource
for the insert.- Returns:
- the ids corresponding to each record that was inserted, if ids were generated. If ids were not generated,
elements will be
null
.
-
update
Description copied from interface:DataAccessStrategy
Updates the data of a single entity in the database. Referenced entities don't get handled.- Specified by:
update
in interfaceDataAccessStrategy
- Type Parameters:
S
- the type of the instance to save.- Parameters:
instance
- the instance to save. Must not benull
.domainType
- the type of the instance to save. Must not benull
.- Returns:
- whether the update actually updated a row.
-
updateWithVersion
Description copied from interface:DataAccessStrategy
Updates the data of a single entity in the database and enforce optimistic record locking using thepreviousVersion
property. Referenced entities don't get handled.The statement will be of the form :
UPDATE … SET … WHERE ID = :id and VERSION_COLUMN = :previousVersion
and throw an optimistic record locking exception if no rows have been updated.- Specified by:
updateWithVersion
in interfaceDataAccessStrategy
- Type Parameters:
S
- the type of the instance to save.- Parameters:
instance
- the instance to save. Must not benull
.domainType
- the type of the instance to save. Must not benull
.previousVersion
- The previous version assigned to the instance being saved.- Returns:
- whether the update actually updated a row.
-
delete
Description copied from interface:DataAccessStrategy
Deletes a single row identified by the id, from the table identified by the domainType. Does not handle cascading deletes.The statement will be of the form :
DELETE FROM … WHERE ID = :id and VERSION_COLUMN = :version
and throw an optimistic record locking exception if no rows have been updated.- Specified by:
delete
in interfaceDataAccessStrategy
- Parameters:
id
- the id of the row to be deleted. Must not benull
.domainType
- the type of entity to be deleted. Implicitly determines the table to operate on. Must not benull
.
-
delete
Description copied from interface:DataAccessStrategy
Deletes multiple rows identified by the ids, from the table identified by the domainType. Does not handle cascading deletes.The statement will be of the form :
DELETE FROM … WHERE ID IN (:ids)
and throw an optimistic record locking exception if no rows have been updated.- Specified by:
delete
in interfaceDataAccessStrategy
- Parameters:
ids
- the ids of the rows to be deleted. Must not benull
.domainType
- the type of entity to be deleted. Implicitly determines the table to operate on. Must not benull
.
-
deleteWithVersion
Description copied from interface:DataAccessStrategy
Deletes a single entity from the database and enforce optimistic record locking using the version property. Does not handle cascading deletes.- Specified by:
deleteWithVersion
in interfaceDataAccessStrategy
- Parameters:
id
- the id of the row to be deleted. Must not benull
.domainType
- the type of entity to be deleted. Implicitly determines the table to operate on. Must not benull
.previousVersion
- The previous version assigned to the instance being saved.
-
delete
public void delete(Object rootId, org.springframework.data.mapping.PersistentPropertyPath<RelationalPersistentProperty> propertyPath) Description copied from interface:DataAccessStrategy
Deletes all entities reachable via propertyPath from the instance identified by rootId.- Specified by:
delete
in interfaceDataAccessStrategy
- Parameters:
rootId
- Id of the root object on which the propertyPath is based. Must not benull
.propertyPath
- Leading from the root object to the entities to be deleted. Must not benull
.
-
delete
public void delete(Iterable<Object> rootIds, org.springframework.data.mapping.PersistentPropertyPath<RelationalPersistentProperty> propertyPath) Description copied from interface:DataAccessStrategy
Deletes all entities reachable via propertyPath from the instances identified by rootIds.- Specified by:
delete
in interfaceDataAccessStrategy
- Parameters:
rootIds
- Ids of the root objects on which the propertyPath is based. Must not benull
or empty.propertyPath
- Leading from the root object to the entities to be deleted. Must not benull
.
-
deleteAll
Description copied from interface:DataAccessStrategy
Deletes all entities of the given domain type.- Specified by:
deleteAll
in interfaceDataAccessStrategy
- Type Parameters:
T
- type of the domain type.- Parameters:
domainType
- the domain type for which to delete all entries. Must not benull
.
-
deleteAll
public void deleteAll(org.springframework.data.mapping.PersistentPropertyPath<RelationalPersistentProperty> propertyPath) Description copied from interface:DataAccessStrategy
Deletes all entities reachable via propertyPath from any instance.- Specified by:
deleteAll
in interfaceDataAccessStrategy
- Parameters:
propertyPath
- Leading from the root object to the entities to be deleted. Must not benull
.
-
acquireLockById
Description copied from interface:DataAccessStrategy
Acquire a lock on the aggregate specified by id.- Specified by:
acquireLockById
in interfaceDataAccessStrategy
- Parameters:
id
- the id of the entity to load. Must not benull
.lockMode
- the lock mode for select. Must not benull
.domainType
- the domain type of the entity. Must not benull
.
-
acquireLockAll
Description copied from interface:DataAccessStrategy
Acquire a lock on all aggregates of the given domain type.- Specified by:
acquireLockAll
in interfaceDataAccessStrategy
- Parameters:
lockMode
- the lock mode for select. Must not benull
.domainType
- the domain type of the entity. Must not benull
.
-
findById
Description copied from interface:DataAccessStrategy
Loads a single entity identified by type and id.- Specified by:
findById
in interfaceDataAccessStrategy
- Type Parameters:
T
- the type of the entity.- Parameters:
id
- the id of the entity to load. Must not benull
.domainType
- the domain type of the entity. Must not benull
.- Returns:
- Might return
null
.
-
findAll
Description copied from interface:DataAccessStrategy
Loads all entities of the given type.- Specified by:
findAll
in interfaceDataAccessStrategy
- Type Parameters:
T
- the type of entities to load.- Parameters:
domainType
- the type of entities to load. Must not benull
.- Returns:
- Guaranteed to be not
null
.
-
findAllById
Description copied from interface:DataAccessStrategy
Loads all entities that match one of the ids passed as an argument. It is not guaranteed that the number of ids passed in matches the number of entities returned.- Specified by:
findAllById
in interfaceDataAccessStrategy
- Type Parameters:
T
- type of entities to load.- Parameters:
ids
- the Ids of the entities to load. Must not benull
.domainType
- the type of entities to load. Must not benull
.- Returns:
- the loaded entities. Guaranteed to be not
null
.
-
findAllByPath
public Iterable<Object> findAllByPath(Identifier identifier, org.springframework.data.mapping.PersistentPropertyPath<? extends RelationalPersistentProperty> path) Description copied from interface:RelationResolver
Finds all entities reachable via path.- Specified by:
findAllByPath
in interfaceDataAccessStrategy
- Specified by:
findAllByPath
in interfaceRelationResolver
- Parameters:
identifier
- the combination of Id, map keys and list indexes that identify the parent of the entity to be loaded. Must not be null.path
- the path from the aggregate root to the entities to be resolved. Must not be null.- Returns:
- guaranteed to be not null.
-
existsById
Description copied from interface:DataAccessStrategy
returns if a row with the given id exists for the given type.- Specified by:
existsById
in interfaceDataAccessStrategy
- Type Parameters:
T
- the type of the entity.- Parameters:
id
- the id of the entity for which to check. Must not benull
.domainType
- the type of the entity to check for. Must not benull
.- Returns:
true
if a matching row exists, otherwisefalse
.
-
findAll
Description copied from interface:DataAccessStrategy
Loads all entities of the given type, sorted.- Specified by:
findAll
in interfaceDataAccessStrategy
- Type Parameters:
T
- the type of entities to load.- Parameters:
domainType
- the type of entities to load. Must not benull
.sort
- the sorting information. Must not benull
.- Returns:
- Guaranteed to be not
null
.
-
findAll
public <T> Iterable<T> findAll(Class<T> domainType, org.springframework.data.domain.Pageable pageable) Description copied from interface:DataAccessStrategy
Loads all entities of the given type, paged and sorted.- Specified by:
findAll
in interfaceDataAccessStrategy
- Type Parameters:
T
- the type of entities to load.- Parameters:
domainType
- the type of entities to load. Must not benull
.pageable
- the pagination information. Must not benull
.- Returns:
- Guaranteed to be not
null
.
-
findOne
Description copied from interface:DataAccessStrategy
Execute aSELECT
query and convert the resulting item to an entity ensuring exactly one result.- Specified by:
findOne
in interfaceDataAccessStrategy
- Parameters:
query
- must not be null.probeType
- the type of entities. Must not benull
.- Returns:
- exactly one result or
Optional.empty()
if no match found.
-
findAll
Description copied from interface:DataAccessStrategy
Execute aSELECT
query and convert the resulting items to aIterable
.- Specified by:
findAll
in interfaceDataAccessStrategy
- Parameters:
query
- must not be null.probeType
- the type of entities. Must not benull
.- Returns:
- a non-null list with all the matching results.
-
findAll
public <T> Iterable<T> findAll(Query query, Class<T> probeType, org.springframework.data.domain.Pageable pageable) Description copied from interface:DataAccessStrategy
Execute aSELECT
query and convert the resulting items to aIterable
. Applies thePageable
to the result.- Specified by:
findAll
in interfaceDataAccessStrategy
- Parameters:
query
- must not be null.probeType
- the type of entities. Must not be null.pageable
- the pagination that should be applied. Must not be null.- Returns:
- a non-null list with all the matching results.
-
exists
Description copied from interface:DataAccessStrategy
Determine whether there is an aggregate of typedomainType
that matches the providedQuery
.- Specified by:
exists
in interfaceDataAccessStrategy
- Parameters:
query
- must not be null.probeType
- the type of entities. Must not benull
.- Returns:
- true if the object exists.
-
count
Description copied from interface:DataAccessStrategy
Counts the rows in the table representing the given probe type, that match the givenquery
.- Specified by:
count
in interfaceDataAccessStrategy
- Parameters:
query
- the query which elements have to match.probeType
- the probe type for which to count the elements. Must not benull
.- Returns:
- the count. Guaranteed to be not
null
.
-
count
Description copied from interface:DataAccessStrategy
Counts the rows in the table representing the given domain type.- Specified by:
count
in interfaceDataAccessStrategy
- Parameters:
domainType
- the domain type for which to count the elements. Must not benull
.- Returns:
- the count. Guaranteed to be not
null
.
-