Spring Data MyBatis

Build Status Gitter chat Maven Central License: Apache 2.0

1.x Document

The primary goal of the Spring Data project is to make it easier to build Spring-powered applications that use data access technologies. This module deals with enhanced support for MyBatis based data access layers.


Getting Help

If you have any question, please record a issue to me.

Quick Start

Download the jar through Maven:


The simple Spring Data Mybatis configuration with Java-Config looks like this:

        value = "org.springframework.data.mybatis.repository.sample",
        mapperLocations = "classpath*:/org/springframework/data/mybatis/repository/sample/mappers/*Mapper.xml"
public class TestConfig {

    public DataSource dataSource() throws SQLException {
        return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).addScript("classpath:/test-init.sql").build();

    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        return factoryBean;

    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);


Create an entity:

@Table(name = "user")
public class User extends LongId {

  private String firstname;
  @Condition(type=Condition.Type.CONTAINING)  private String lastname;
  private String fullName;
  @Conditions({@Condition, @Condition(type=Condition.Type.CONTAINING,properties = "fuzzyName")})
  private String fullName;
  private String fuzzyName;
  @Column(name = "usertype")
  private String status;
  // Getters and setters
  // (Firstname, Lastname)-constructor and noargs-constructor
  // equals / hashcode

When using findAll method of MybatisRepository the @Condition or @Conditions annotations will work

Create a repository interface in com.example.repositories:

public interface UserRepository extends MybatisRepository<User, Long> {
  List<User> findByLastname(String lastname);  

  @Query("select firstname from user")
  List<String> findUsersFirstName();


Write a test client:

@ContextConfiguration(classes = AppConfig.class)
public class UserRepositoryIntegrationTest {

  @Autowired UserRepository repository;

  public void sampleTestCase() {
    User dave = new User("Dave", "Matthews");
    dave = repository.save(dave);

    User carter = new User("Carter", "Beauford");
    carter = repository.save(carter);

    List<User> result = repository.findByLastname("Matthews");
    assertThat(result.size(), is(1));
    assertThat(result, hasItem(dave));

Use Spring Boot

add the jar through Maven:


If you need custom Mapper, you should add property in your application.yml like this:

  mapper-locations: "classpath*:/mapper/**/**Mapper.xml"

And you need not to define SqlSessionFactory manually.

The full test code like this:

public class SpringDataMybatisSamplesApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringDataMybatisSamplesApplication.class, args);

    public CommandLineRunner dummyCLR(ReservationRepository reservationRepository) {
        return args -> {
            Stream.of("Tom", "Jack", "Apple")
                    .forEach(name -> reservationRepository.save(new Reservation(name)));


@RepositoryRestResource // here we use RepositoryRestResource
interface ReservationRepository extends MybatisRepository<Reservation, Long> {

@Table(name = "user")
class Reservation extends LongId {

    private String reservationName;

    public Reservation() {

    public Reservation(String reservationName) {
        this.reservationName = reservationName;

    public String getReservationName() {
        return reservationName;

    public String toString() {
        return "Reservation{" +
                "reservationName='" + reservationName + '\'' +

The full example you can find in https://github.com/hatunet/spring-data-mybatis-samples

Contributing to Spring Data MyBatis

Here are some ways for you to get involved in the community:

Help me better - Donation