How to Configure MyBatis with Spring Boot

MyBatis is a SQL mapper framework with support for mapping the custom SQL statements and stored procedures to Java objects, thus helping in using a relational database with object-oriented applications. In this tutorial, we will learn to configure MyBatis with Spring Boot 3 using mybatis-spring-boot-autoconfigure dependency and embedded database with an example.

1. Maven

When using with Spring boot, the easiest way is to include the mybatis-spring-boot-starter dependency in the project.


The above starter dependency transitively includes mybatis-spring-boot-autoconfigure, spring-boot-starter, spring-boot-starter-jdbc and mybatis-spring dependencies that otherwise must be included separately.

Additionally, we should include the relational database driver dependency for database connectivity. We are using H2 database, so include the following:


2. Model

MyBatis maps the POJO objects to SQL statements, so let us create a very simple TODO object for demo purposes.

public class TODO {

  private Long id;
  private String title;
  private String body;

The equivalent database schema script file schema.sql is:

    `id`        INTEGER PRIMARY KEY,
    `title`     VARCHAR(100) NOT NULL,
    `body`      VARCHAR(2000) NOT NULL

We can initialize the table with some default data using data.sql, if needed:


3. @Mapper Configuration

The mybatis-spring-boot-starter will search, by default, for mappers marked with the @Mapper annotation during the component scanning.

import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

public interface ToDoMapper {

  @Select("select * from TBL_TODO")
  List<TODO> findAll();

  @Select("SELECT * FROM TBL_TODO WHERE id = #{id}")
  TODO findById(@Param("id") Long id);

  @Delete("DELETE FROM TBL_TODO WHERE id = #{id}")
  int deleteById(@Param("id") Long id);

  @Insert("INSERT INTO TBL_TODO(id, title, body) " +
      " VALUES (#{id}, #{title}, #{body})")
  int createNew(TODO item);

  @Update("Update TBL_TODO set title=#{title}, " +
      " body=#{body} where id=#{id}")
  int update(TODO item);

We can also use the @MapperScan annotation to specify the packages in a Spring @Configuration class. It will scan all the interfaces in the specified packages for the following mapper annotations and will register these interfaces as mappers.

  • @Select
  • @Insert
  • @Update
  • @Delete
import org.mybatis.spring.annotation.MapperScan;

public class PersistenceConfig {

And define the mapper interfaces as follows:

public interface ToDoMapper { 

  @Select("select * from TBL_TODO")
  List<TODO> findAll();


4. DataSource Configuration

The MybatisAutoConfiguration class automatically scans and configures the SqlSessionFactory and DataSource beans as specified in the file.



To programmatically configure the datasource, we can define the beans in @Configuration class:

public class PersistenceConfig {

  public DataSource dataSource() {
    return new EmbeddedDatabaseBuilder()

  public SqlSessionFactory sqlSessionFactory() throws Exception {
    SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
    return factoryBean.getObject();

5. Demo

Finally, after the above Mapper and DataSource configurations, we can test the mapper for various add/update operations in the database. We are using the CommandLineRunner to execute the demo code.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class App implements CommandLineRunner
    public static void main(String[] args) {, args);

    private ToDoMapper todoMapper;

    public void run(String... args) throws Exception {

        TODO newItem = new TODO(2L, "title_2", "body_2");
        int createdCount = todoMapper.createNew(newItem);
        System.out.println("Created items count : " + createdCount);

        TODO item = todoMapper.findById(2L);

        int deletedCount = todoMapper.deleteById(2L);
        System.out.println("Deleted items count : " + deletedCount);

        TODO deletedItem = todoMapper.findById(2L);
        System.out.println("Deleted item should be null : " + deletedItem);

The program output:

Created items count : 1
TODO(id=2, title=title_2, body=body_2)
Deleted items count : 1
Deleted item should be null : null

6. Conclusion

In this short tutorial, we learned to configure MyBatis with Spring Boot. We learned about mapper scanning options and datasource configurations as well.

Happy Learning !!

Sourcecode on Github


Notify of
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.