MyBatis Integration
CRUD 操作和查询方法可以委托给 MyBatis。本节介绍如何配置 Spring Data JDBC 集成到 MyBatis 中,以及遵循哪些约定来移交查询的运行以及到库的映射。
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
将 MyBatis 正确插入 Spring Data JDBC 的最简单方法是将 MyBatisJdbcConfiguration
导入您的应用程序配置:
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
}
}
如你所见,你只需要声明一个 SqlSessionFactoryBean
,因为 MyBatisJdbcConfiguration
依靠最终在 ApplicationContext
中有 SqlSession
bean 可用。
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
对于 CrudRepository
中的每个操作,Spring Data JDBC 都会运行多条语句。如果应用程序上下文中存在 link:https://github.com/mybatis/mybatis-3/blob/master/src/main/java/org/apache/ibatis/session/SqlSessionFactory.java[SqlSessionFactory
,则对于每个步骤,Spring Data 都会检查 SessionFactory
是否提供语句。如果找到一个,则将使用该语句(包括它已配置的到实体的映射)。
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.
通过将实体类型的完全限定名称与 Mapper.
连接,以及一个确定语句类型的 String
,来构造语句的名称。例如,如果要插入 org.example.User
的一个实例,Spring Data JDBC 会寻找名为 org.example.UserMapper.insert
的语句。
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
.
运行语句时,[MyBatisContext
] 的实例将作为参数传递,这会让语句可以使用各个参数。
When the statement is run, an instance of [MyBatisContext
] gets passed as an argument, which makes various arguments available to the statement.
下表描述了可用的 MyBatis 语句:
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 |
|
|