The integration of MyBatis in spring boot allows you to choose the annotation-based approach or how to configure the XML file. The integration is done here using annotation-based approaches.
Introduction of Mybatis-spring-boot-starter
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId> mybatis-spring-boot-starter</artifactid>
<version>1.1.1</version>
</dependency >
’
2. Create Mapper.java files and mapper.xml files
There are two ways to automatically scan Mapper.java files:
1) Add @mapperscan annotations to the boot class on spring boot
2) Add @mapper annotations to each Mapper.java interface class
Using the first method, you need to specify the package path, for example: @MapperScan ("Com.ft.turorial.spring.boot.mapper"), but with one drawback, it does not support @mapperscan ("com.ft...mapper.* ") This way, so the second way is recommended.
Usermapper.java
The SQL statement of the/** * Mapper interface can be added to the interface class by adding annotations to the method, or by configuring the SQL statement to be scanned in the Mapper.xml file
*
Mapper interface, You can also add @mapperscan
* @author ft * */
@Mapper public
interface Usermapper {
User) on the startup class. Insertselective (user user);
User Insert (user user);
@Select ("SELECT * from user where id= #{id,jdbctype=integer}")
user findOne (int id);
List<user> findAll ();
User findlikename (String name);
int deletebyprimarykey (int id);
int updatebyprimarykeyselective (user user);
int Updatebyprimarykey (user user);
}
Usermapper.xml (Set up \mata-inf\mybatis\mappers folder in Resource directory to store, can be customized)
<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" > < Mapper namespace= "Com.ft.turorial.spring.boot.mapper.UserMapper" > <cache/> <resultmap id= "User" type= "C Om.ft.turorial.spring.boot.domain.User "> <id column=" id "property=" id "jdbctype=" INTEGER "/> <result column= "name" property= "name" jdbctype= "VARCHAR"/> <result column= "Birthday" property= "Birthday" jdbctype= "DAT E "/> </resultMap> <sql id=" base_column_list "> ID, Name, birthday </sql> <!--<sel ECT id= "FindOne" resultmap= "User" parametertype= "Java.lang.Integer" > select <include refid= "Base_column_lis T "/> from user where id = #{id,jdbctype=integer} </select> <select id=" FindAll "resultmap= "User" resulttype= "list" > select <include refid= "Base_column_list"/> from user </select> <select id= "Findlikename" resultmap= "User" parametertype= "string" resulttype= "List" > select
<include refid= "Base_column_list"/> from the user where name like concat ('% ', #{name,jdbctype=varchar}, '% ') </select> <insert id= "Insert" parametertype= "Com.ft.turorial.spring.boot.domain.User" usegeneratedkeys= " True "keyproperty=" id "> INSERT into User (ID, name, birthday) VALUES (#{id,jdbctype=integer}, #{name,jdbctype=v Archar}, #{birthday,jdbctype=date}) </insert> <insert id= "insertselective" parametertype= " Com.ft.turorial.spring.boot.domain.User "usegeneratedkeys=" true "keyproperty=" id "> INSERT INTO User <trim PR Efix= "(" suffix= ")" suffixoverrides= "," > <if test= "id! = NULL" > ID, </if> <i
F test= "Name! = NULL" > name, </if> <if test= "Birthday! = null" > Birthday, </if> </trim> <trim PREfix= "VALUES (" suffix= ")" suffixoverrides= "," > <if test= "id! = NULL" > #{id,jdbctype=integer}, </if> <if test= "Name! = NULL" > #{name,jdbctype=varchar}, </if> <if tes t= "Birthday! = null" > #{birthday,jdbctype=date}, </if> </trim> </insert> <d Elete id= "Deletebyprimarykey" parametertype= "Java.lang.Integer" > Delete from user where id = #{id,jdbctype=int EGER} </delete> <update id= "updatebyprimarykeyselective" parametertype= "User" > Update user <se T > <if test= "Name! = NULL" > name = #{name,jdbctype=varchar}, </if> <if test= "Birthday! = null" > Birthday = #{birthday,jdbctype=date}, </if> </set> where ID = #{ Id,jdbctype=integer} </update> <update id= "Updatebyprimarykey" parametertype= " Com.ft.turorial.spring.boot.domain.User "> UpdatE user Set name = #{name,jdbctype=varchar}, birthday = #{birthday,jdbctype=date} where id = #{id,jdbctype= INTEGER} </update> </mapper>
As can be seen from the above code, Mapper.xml can only be used as a field mapping, SQL statements can be written through MyBatis annotations (@Select, @Insert, @Delete, @Update) in the Mapper.java interface.
’
3. Specify the location of the mapping file (XML) and automatically scan the entity package name
Configure the following information in Application.properties:
# mybatis
# mapper.xml path
mybatis.mapperlocations=classpath*:/meta-inf/mybatis/mappers/*.xml
# entity/domain AutoScan Package
mybatis.type-aliases-package=com.ft.turorial.spring.boot
# use Auto-mapping after you can remove @ResultMap on mapper of interface
Mybatis.configuration.map-underscore-to-camel-case=true
’
4. Other