Some days before , I have introduced how to use springboot and mybatis to do a hello world example.This post would demo how to run a mapper xml example on springboot+MyBatis+MySQL.
- SpringBoot 1.5.12
- MySQL 5.1.38
- Java 1.8
- mybatis-spring-boot-starter 1.3.2
The database table:
Springboot Main entry class:
The domain class:
The Mapper interface:
Here we use a @Mapper to annotate an interface named StudentMapper, which has a method findAll, this method has a corresponding parts in the StudentMapper.xml.
Notice the mybatis.mapper-locations is the location of the mapper xml files, the files must be in the src/main/resources
the Directories structure
Now you should have a directory structure like this:
Here we write a junit test case to test the dao:
As you can see, we just @Autowired the studentMapper, and then use the findAll method, then we run the test, we got the green bar.
It’s so easy, do you think so? Next time I would introduce more complicated springboot and mybatis example.
You can find detail documents about the springboot and unit testing here: