Use Mybatis in Spring Boot 2016-02-29 09:05

In this tutorial I will show how to use Mybatis in Spring Boot. Spring Boot can help you build RESTful project quickly, Mybatis is one of the best ORM framework. Use Mybatis in Spring Boot can help you build persistence layer of your project quickly and easily. I will provide a complete example for using MyBatis in Spring Boot step by step.


I use following software and tools to build this project. Java 7 + Maven + PostgreSQL + IntelliJ

Project Structure

Create a maven project in Intellij and the structure like below.

│  ├─java
│  │  └─com
│  │      └─henry
│  │          └─mybatis
│  │              │
│  │              │
│  │              ├─controller
│  │              │
│  │              │
│  │              ├─mapper
│  │              │
│  │              │
│  │              ├─model
│  │              │
│  │              │
│  │              └─service
│  │            
│  │
│  └─resources
│      │
│      │
│      │
│      └─mybatis
│              UserMapper.xml

Create table in your Database

I use following SQL to create a sample table in my Database. After creation insert a record in it.

CREATE TABLE public.users (
  age INTEGER,
  password CHARACTER VARYING(10)
INSERT INTO users VALUES (1, 'user1', 27, 'password');

Dependencies in pom.xml file


Define SqlSessionFactoryBean, DataSource and DataSourceTransactionManager in Configuration class

In normal Spring project we are used to define SqlSessionFactoryBean, DataSource and DataSourceTransactionManager in Spring configuration file(*.xml). Here we use @Bean define them in class. The annotation @MapperScan will scan the interfaces in appointed directory. In DataSource Bean we get the Database configuration in properties file. The whole code of Application:

@ComponentScan(value = "com.henry.xi.mybatis")
public class Application {
    private static Logger logger = Logger.getLogger(Application.class);

    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        return new org.apache.tomcat.jdbc.pool.DataSource();

    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        return sqlSessionFactoryBean.getObject();

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

    public static void main(String[] args) {, args);"SpringBoot Start Success");

Create mapper xml file and interface

In Mybatis we use interface and mapper xml file to get record from database.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-// Mapper 3.0//EN" "">
<mapper namespace="com.henry.xi.mybatis.mapper.UserMapper">

    <select id="findUserInfo" resultType="com.henry.xi.mybatis.model.User">
      select name, age,password from users;


UserMapper interface

public interface UserMapper {
    User findUserInfo();

Create controller and model

controller and model is sample.

public class UserController {

    private Logger logger = Logger.getLogger(UserController.class);

    private UserService userService;

    public User getUserInfo() {
        User user = userService.getUserInfo();
        return user;

model as following

public class User {
    private String name;
    private Integer age;
    private String password;

    //getter and setter

Now you can start your project by running the main method in Application class.