MyBatis is an open-source Java persistence framework that abstracts JDBC boilerplate code and provides a simple and easy-to-use API to interact with the database.
Unlike Hibernate , a full-blown ORM framework, MyBatis is a SQL mapping framework. It automates the process of populating the SQL resultset into Java objects, and it persists data into tables by extracting the data from the Java objects.
This chapter covers how to use the Spring Boot MyBatis starter, execute database queries using MyBatis XML, and use annotation-based mappers.
Using the Spring Boot MyBatis Starter
The MyBatis community built the Spring Boot starter for MyBatis, which you can use while creating the Spring Boot project from the Spring Initializer or the IDE. You can explore the source code on GitHub at https://github.com/mybatis/spring-boot-starter .
- 1.
Create a Spring Boot Maven project and configure the MyBatis Starter dependency and H2/MySQL driver dependencies .
- 2.
Create the MyBatis SQL Mapper interface UserMapper.java with a few database operations, as shown in Listing 6-1.
com.apress.demo.mappers.UserMapper.java
- 3.
You must create mapper XML files to define the queries for the SQL statements mapped to the corresponding mapper interface methods. Create the UserMapper.xml file in the src/main/resources/com/apress/demo/mappers/ directory, as shown in Listing 6-2.
src/main/resources/com/apress/demo/mappers/UserMapper.xml
A few things to observe here are the following:
The namespace in the mapper XML should be the same as the Fully Qualified Name (FQN) of the mapper interface.
The statement id values should be the same as the mapper interface method names.
Suppose the query result column names are different from the bean property names. If so, you can use the <resultMap> configuration to provide the mapping between column names and their corresponding bean property names.
- 4.
MyBatis also provides annotation-based query configurations without requiring mapper XMLs. You can create the UserMapper.java interface and configure the mapped SQLs using annotations, as shown in Listing 6-3.
com.apress.demo.mappers.UserMapper.java
- 5.Next, you must configure the starter configuration parameters . Configure the type-aliases-package and mapper-locations parameters in application.properties as follows:mybatis.type-aliases-package=com.apress.demo.domainmybatis.mapper-locations=classpath*:/mappers/*.xml
- 6.
Create the entry point class called SpringbootMyBatisDemoApplication.java, as shown in Listing 6-4.
com.apress.demo.SpringbootMyBatisDemoApplication.java
This example uses the @MapperScan("com.apress.demo.mappers") annotation to specify where to look for the mapper interfaces. Instead of using @MapperScan, you could also use annotated mapper interfaces with the new @Mapper annotation that shipped with MyBatis 3.4.0.
- 7.
Create a JUnit test class and test the UserMapper methods , as shown in Listing 6-5.
com.apress.demo.SpringbootMyBatisDemoApplicationTests.java
You can read more about MyBatis at http://blog.mybatis.org/p/products.html and https://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/ .
Summary
In this chapter, you learned how to work with MyBatis in Spring Boot applications. The next chapter covers how to use another popular Java persistence framework with Spring Boot, called JOOQ.