For the latest stable version, please use Spring Data Relational 3.3.4! |
MyBatis Integration
The CRUD operations and query methods can be delegated to MyBatis. This section describes how to configure Spring Data JDBC to integrate with MyBatis and which conventions to follow to hand over the running of the queries as well as the mapping to the library.
Configuration
The easiest way to properly plug MyBatis into Spring Data JDBC is by importing MyBatisJdbcConfiguration
into you application configuration:
@Configuration
@EnableJdbcRepositories
@Import(MyBatisJdbcConfiguration.class)
class Application {
@Bean
SqlSessionFactoryBean sqlSessionFactoryBean() {
// Configure MyBatis here
}
}
As you can see, all you need to declare is a SqlSessionFactoryBean
as MyBatisJdbcConfiguration
relies on a SqlSession
bean to be available in the ApplicationContext
eventually.
Usage conventions
For each operation in CrudRepository
, Spring Data JDBC runs multiple statements.
If there is a SqlSessionFactory
in the application context, Spring Data checks, for each step, whether the SessionFactory
offers a statement.
If one is found, that statement (including its configured mapping to an entity) is used.
The name of the statement is constructed by concatenating the fully qualified name of the entity type with Mapper.
and a String
determining the kind of statement.
For example, if an instance of org.example.User
is to be inserted, Spring Data JDBC looks for a statement named org.example.UserMapper.insert
.
When the statement is run, an instance of [MyBatisContext
] gets passed as an argument, which makes various arguments available to the statement.
The following table describes the available MyBatis statements:
Name | Purpose | CrudRepository methods that might trigger this statement | Attributes available in the MyBatisContext |
---|---|---|---|
|
Inserts a single entity. This also applies for entities referenced by the aggregate root. |
|
|
|
Updates a single entity. This also applies for entities referenced by the aggregate root. |
|
|
|
Deletes a single entity. |
|
|
|
Deletes all entities referenced by any aggregate root of the type used as prefix with the given property path. Note that the type used for prefixing the statement name is the name of the aggregate root, not the one of the entity to be deleted. |
|
|
|
Deletes all aggregate roots of the type used as the prefix |
|
|
|
Deletes all entities referenced by an aggregate root with the given propertyPath |
|
|
|
Selects an aggregate root by ID |
|
|
|
Select all aggregate roots |
|
|
|
Select a set of aggregate roots by ID values |
|
|
|
Select a set of entities that is referenced by another entity. The type of the referencing entity is used for the prefix. The referenced entities type is used as the suffix. This method is deprecated. Use |
All |
|
|
Select a set of entities that is referenced by another entity via a property path. |
All |
|
|
Select all aggregate roots, sorted |
|
|
|
Select a page of aggregate roots, optionally sorted |
|
|
|
Count the number of aggregate root of the type used as prefix |
|
|