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

insert

Inserts a single entity. This also applies for entities referenced by the aggregate root.

save, saveAll.

getInstance: the instance to be saved

getDomainType: The type of the entity to be saved.

get(<key>): ID of the referencing entity, where <key> is the name of the back reference column provided by the NamingStrategy.

update

Updates a single entity. This also applies for entities referenced by the aggregate root.

save, saveAll.

getInstance: The instance to be saved

getDomainType: The type of the entity to be saved.

delete

Deletes a single entity.

delete, deleteById.

getId: The ID of the instance to be deleted

getDomainType: The type of the entity to be deleted.

deleteAll-<propertyPath>

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.

deleteAll.

getDomainType: The types of the entities to be deleted.

deleteAll

Deletes all aggregate roots of the type used as the prefix

deleteAll.

getDomainType: The type of the entities to be deleted.

delete-<propertyPath>

Deletes all entities referenced by an aggregate root with the given propertyPath

deleteById.

getId: The ID of the aggregate root for which referenced entities are to be deleted.

getDomainType: The type of the entities to be deleted.

findById

Selects an aggregate root by ID

findById.

getId: The ID of the entity to load.

getDomainType: The type of the entity to load.

findAll

Select all aggregate roots

findAll.

getDomainType: The type of the entity to load.

findAllById

Select a set of aggregate roots by ID values

findAllById.

getId: A list of ID values of the entities to load.

getDomainType: The type of the entity to load.

findAllByProperty-<propertyName>

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 instead

All find* methods. If no query is defined for findAllByPath

getId: The ID of the entity referencing the entities to be loaded.

getDomainType: The type of the entity to load.

findAllByPath-<propertyPath>

Select a set of entities that is referenced by another entity via a property path.

All find* methods.

getIdentifier: The Identifier holding the id of the aggregate root plus the keys and list indexes of all path elements.

getDomainType: The type of the entity to load.

findAllSorted

Select all aggregate roots, sorted

findAll(Sort).

getSort: The sorting specification.

findAllPaged

Select a page of aggregate roots, optionally sorted

findAll(Page).

getPageable: The paging specification.

count

Count the number of aggregate root of the type used as prefix

count

getDomainType: The type of aggregate roots to count.