For the latest stable version, please use Spring Data Relational 3.4.0!spring-doc.cn

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.spring-doc.cn

Configuration

The easiest way to properly plug MyBatis into Spring Data JDBC is by importing MyBatisJdbcConfiguration into you application configuration:spring-doc.cn

@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.spring-doc.cn

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.spring-doc.cn

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.spring-doc.cn

When the statement is run, an instance of [MyBatisContext] gets passed as an argument, which makes various arguments available to the statement.spring-doc.cn

The following table describes the available MyBatis statements:spring-doc.cn

Name Purpose CrudRepository methods that might trigger this statement Attributes available in the MyBatisContext

insertspring-doc.cn

Inserts a single entity. This also applies for entities referenced by the aggregate root.spring-doc.cn

save, saveAll.spring-doc.cn

getInstance: the instance to be savedspring-doc.cn

getDomainType: The type of the entity to be saved.spring-doc.cn

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

updatespring-doc.cn

Updates a single entity. This also applies for entities referenced by the aggregate root.spring-doc.cn

save, saveAll.spring-doc.cn

getInstance: The instance to be savedspring-doc.cn

getDomainType: The type of the entity to be saved.spring-doc.cn

deletespring-doc.cn

Deletes a single entity.spring-doc.cn

delete, deleteById.spring-doc.cn

getId: The ID of the instance to be deletedspring-doc.cn

getDomainType: The type of the entity to be deleted.spring-doc.cn

deleteAll-<propertyPath>spring-doc.cn

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.spring-doc.cn

deleteAll.spring-doc.cn

getDomainType: The types of the entities to be deleted.spring-doc.cn

deleteAllspring-doc.cn

Deletes all aggregate roots of the type used as the prefixspring-doc.cn

deleteAll.spring-doc.cn

getDomainType: The type of the entities to be deleted.spring-doc.cn

delete-<propertyPath>spring-doc.cn

Deletes all entities referenced by an aggregate root with the given propertyPathspring-doc.cn

deleteById.spring-doc.cn

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

getDomainType: The type of the entities to be deleted.spring-doc.cn

findByIdspring-doc.cn

Selects an aggregate root by IDspring-doc.cn

findById.spring-doc.cn

getId: The ID of the entity to load.spring-doc.cn

getDomainType: The type of the entity to load.spring-doc.cn

findAllspring-doc.cn

Select all aggregate rootsspring-doc.cn

findAll.spring-doc.cn

getDomainType: The type of the entity to load.spring-doc.cn

findAllByIdspring-doc.cn

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

findAllById.spring-doc.cn

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

getDomainType: The type of the entity to load.spring-doc.cn

findAllByProperty-<propertyName>spring-doc.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.cn

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

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

getDomainType: The type of the entity to load.spring-doc.cn

findAllByPath-<propertyPath>spring-doc.cn

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

All find* methods.spring-doc.cn

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

getDomainType: The type of the entity to load.spring-doc.cn

findAllSortedspring-doc.cn

Select all aggregate roots, sortedspring-doc.cn

findAll(Sort).spring-doc.cn

getSort: The sorting specification.spring-doc.cn

findAllPagedspring-doc.cn

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

findAll(Page).spring-doc.cn

getPageable: The paging specification.spring-doc.cn

countspring-doc.cn

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

countspring-doc.cn

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