Simplifies the development of creating a MyBatis-based data access layer.
Switch branches/tags
Nothing to show
Clone or download
Latest commit de27ef7 Jun 7, 2018
Failed to load latest commit information.
src Add Rest tests - Close #9 Nov 29, 2016
.gitignore Migrated to latest version spring-data Jun 4, 2015 Update Jun 7, 2018
pom.xml Add Rest tests - Close #9 Nov 29, 2016


Spring Data MyBatis

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.


This project defines a MyBatisRepository base interface :

public interface MyBatisRepository<T, ID extends Serializable> extends Repository<T, ID> {	
	T findOne(ID id);
	List<T> findAll();
	boolean exists(ID id);
	long count();

The only goal for now of this module is to make your MyBatis mappers created by MyBatis-Spring :

  • implement these four methods by only providing one select statement
  • registered as Spring Data repositories

Quick Start

Build and deploy it into your local maven repository :

git clone
cd spring-data-mybatis
mvn install

Add the jar to your maven project :


Configure your infrastructure:

  1. add @EnableMyBatisRepositories annotation:
public class ApplicationConfig {

  1. Configure datasource:
spring.datasource.validation-query=SELECT 1;

#package for scanning mappers see default value *
#package mybatis aliases see default value ""

Create an entity:

public class User {

	private Integer id;
	private String firstname;
	private String lastname;
	// Getters and setters

Create a repository interface:

public interface CustomerRepository extends MyBatisRepository<Customer, Integer> {

Write your mapper :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-// Mapper 3.0//EN" 
<mapper namespace="com.acme.CustomerRepository">
	<resultMap id="customerResultMap" type="Customer">
		<id property="id" column="id" />
		<result property="firstName" column="first_name" />
		<result property="lastName" column="last_name" />
	<select id="find" resultMap="customerResultMap">
		SELECT id,
		customer.first_name first_name,
		customer.last_name last_name
		customer customer	
		<if test="pk">
			WHERE = #{pk.value}

The select statement id must be named "find". The parameter type of the select statement is a map. This map has a key pk whose value is the object identifying this domain object.

Write a test client

@ContextConfiguration(classes = ApplicationConfig.class)
public class UserRepositoryIntegrationTest {
  @Autowired UserRepository repository;
  public void sampleTestCase() {         
	Customer customer = customerRepository.findOne(100);
	List<Customer> customers = customerRepository.findAll();
	assertTrue(customers.size() > 0);

Transaction management


See the test classes for more.