此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Data Relational 3.4.5spring-doc.cadn.net.cn

MyBatis 集成

CRUD作和查询方法可以委托给 MyBatis。 本节描述了如何配置 Spring Data JDBC 以与 MyBatis 集成,以及要遵循哪些约定来移交查询的运行以及到库的映射。spring-doc.cadn.net.cn

配置

将 MyBatis 正确插入 Spring Data JDBC 的最简单方法是导入MyBatisJdbcConfiguration进入您的应用程序配置:spring-doc.cadn.net.cn

@Configuration
@EnableJdbcRepositories
@Import(MyBatisJdbcConfiguration.class)
class Application {

    @Bean
    SqlSessionFactoryBean sqlSessionFactoryBean() {
        // Configure MyBatis here
    }
}

如您所见,您需要声明的只是一个SqlSessionFactoryBeanMyBatisJdbcConfiguration依赖于SqlSessionbean 在ApplicationContext最终。spring-doc.cadn.net.cn

使用约定

对于CrudRepository,Spring Data JDBC 运行多个语句。 如果存在SqlSessionFactory在应用程序上下文中,Spring Data 会检查每个步骤的SessionFactory提供一个声明。 如果找到一个,则使用该语句(包括其配置的到实体的映射)。spring-doc.cadn.net.cn

语句的名称是通过将实体类型的完全限定名称与Mapper.以及String确定语句的类型。 例如,如果org.example.User是插入的,则 Spring Data JDBC 会查找名为org.example.UserMapper.insert.spring-doc.cadn.net.cn

当语句运行时,[MyBatisContext] 作为参数传递,这使得各种参数可用于语句。spring-doc.cadn.net.cn

下表描述了可用的 MyBatis 语句:spring-doc.cadn.net.cn

名字 目的 CrudRepository 方法,可能会触发此语句 可用的属性MyBatisContext

insertspring-doc.cadn.net.cn

插入单个实体。这也适用于聚合根引用的实体。spring-doc.cadn.net.cn

save,saveAll.spring-doc.cadn.net.cn

getInstance:要保存的实例spring-doc.cadn.net.cn

getDomainType:要保存的实体的类型。spring-doc.cadn.net.cn

get(<key>):引用实体的 ID,其中<key>是由NamingStrategy.spring-doc.cadn.net.cn

updatespring-doc.cadn.net.cn

更新单个实体。这也适用于聚合根引用的实体。spring-doc.cadn.net.cn

save,saveAll.spring-doc.cadn.net.cn

getInstance:要保存的实例spring-doc.cadn.net.cn

getDomainType:要保存的实体的类型。spring-doc.cadn.net.cn

deletespring-doc.cadn.net.cn

删除单个实体。spring-doc.cadn.net.cn

delete,deleteById.spring-doc.cadn.net.cn

getId:需要删除的实例的 IDspring-doc.cadn.net.cn

getDomainType:要删除的实体的类型。spring-doc.cadn.net.cn

deleteAll-<propertyPath>spring-doc.cadn.net.cn

删除用作给定属性路径的前缀的类型的任何聚合根引用的所有实体。 请注意,用于为语句名称添加前缀的类型是聚合根的名称,而不是要删除的实体的名称。spring-doc.cadn.net.cn

deleteAll.spring-doc.cadn.net.cn

getDomainType:要删除的实体的类型。spring-doc.cadn.net.cn

deleteAllspring-doc.cadn.net.cn

删除用作前缀的类型的所有聚合根spring-doc.cadn.net.cn

deleteAll.spring-doc.cadn.net.cn

getDomainType:要删除的实体的类型。spring-doc.cadn.net.cn

delete-<propertyPath>spring-doc.cadn.net.cn

删除具有给定 propertyPath 的聚合根引用的所有实体spring-doc.cadn.net.cn

deleteById.spring-doc.cadn.net.cn

getId:要删除其引用实体的聚合根的 ID。spring-doc.cadn.net.cn

getDomainType:要删除的实体的类型。spring-doc.cadn.net.cn

findByIdspring-doc.cadn.net.cn

按 ID 选择聚合根spring-doc.cadn.net.cn

findById.spring-doc.cadn.net.cn

getId:要加载的实体的 ID。spring-doc.cadn.net.cn

getDomainType:要加载的实体的类型。spring-doc.cadn.net.cn

findAllspring-doc.cadn.net.cn

Select all aggregate rootsspring-doc.cadn.net.cn

findAll.spring-doc.cadn.net.cn

getDomainType:要加载的实体的类型。spring-doc.cadn.net.cn

findAllByIdspring-doc.cadn.net.cn

Select a set of aggregate roots by ID valuesspring-doc.cadn.net.cn

findAllById.spring-doc.cadn.net.cn

getId: A list of ID values of the entities to load.spring-doc.cadn.net.cn

getDomainType:要加载的实体的类型。spring-doc.cadn.net.cn

findAllByProperty-<propertyName>spring-doc.cadn.net.cn

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 findAllByPath insteadspring-doc.cadn.net.cn

All find* methods. If no query is defined for findAllByPathspring-doc.cadn.net.cn

getId: The ID of the entity referencing the entities to be loaded.spring-doc.cadn.net.cn

getDomainType:要加载的实体的类型。spring-doc.cadn.net.cn

findAllByPath-<propertyPath>spring-doc.cadn.net.cn

Select a set of entities that is referenced by another entity via a property path.spring-doc.cadn.net.cn

All find* methods.spring-doc.cadn.net.cn

getIdentifier: The Identifier holding the id of the aggregate root plus the keys and list indexes of all path elements.spring-doc.cadn.net.cn

getDomainType:要加载的实体的类型。spring-doc.cadn.net.cn

findAllSortedspring-doc.cadn.net.cn

Select all aggregate roots, sortedspring-doc.cadn.net.cn

findAll(Sort).spring-doc.cadn.net.cn

getSort: The sorting specification.spring-doc.cadn.net.cn

findAllPagedspring-doc.cadn.net.cn

Select a page of aggregate roots, optionally sortedspring-doc.cadn.net.cn

findAll(Page).spring-doc.cadn.net.cn

getPageable: The paging specification.spring-doc.cadn.net.cn

countspring-doc.cadn.net.cn

Count the number of aggregate root of the type used as prefixspring-doc.cadn.net.cn

countspring-doc.cadn.net.cn

getDomainType: The type of aggregate roots to count.spring-doc.cadn.net.cn