此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Data Relational 3.4.5! |
MyBatis 集成
CRUD作和查询方法可以委托给 MyBatis。 本节描述了如何配置 Spring Data JDBC 以与 MyBatis 集成,以及要遵循哪些约定来移交查询的运行以及到库的映射。
配置
将 MyBatis 正确插入 Spring Data JDBC 的最简单方法是导入MyBatisJdbcConfiguration
进入您的应用程序配置:
@Configuration
@EnableJdbcRepositories
@Import(MyBatisJdbcConfiguration.class)
class Application {
@Bean
SqlSessionFactoryBean sqlSessionFactoryBean() {
// Configure MyBatis here
}
}
如您所见,您需要声明的只是一个SqlSessionFactoryBean
如MyBatisJdbcConfiguration
依赖于SqlSession
bean 在ApplicationContext
最终。
使用约定
对于CrudRepository
,Spring Data JDBC 运行多个语句。
如果存在SqlSessionFactory
在应用程序上下文中,Spring Data 会检查每个步骤的SessionFactory
提供一个声明。
如果找到一个,则使用该语句(包括其配置的到实体的映射)。
语句的名称是通过将实体类型的完全限定名称与Mapper.
以及String
确定语句的类型。
例如,如果org.example.User
是插入的,则 Spring Data JDBC 会查找名为org.example.UserMapper.insert
.
当语句运行时,[MyBatisContext
] 作为参数传递,这使得各种参数可用于语句。
下表描述了可用的 MyBatis 语句:
名字 | 目的 | CrudRepository 方法,可能会触发此语句 | 可用的属性MyBatisContext |
---|---|---|---|
|
插入单个实体。这也适用于聚合根引用的实体。 |
|
|
|
更新单个实体。这也适用于聚合根引用的实体。 |
|
|
|
删除单个实体。 |
|
|
|
删除用作给定属性路径的前缀的类型的任何聚合根引用的所有实体。 请注意,用于为语句名称添加前缀的类型是聚合根的名称,而不是要删除的实体的名称。 |
|
|
|
删除用作前缀的类型的所有聚合根 |
|
|
|
删除具有给定 propertyPath 的聚合根引用的所有实体 |
|
|
|
按 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 |
|
|