1.Mapper.xml的头文件
<?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="">
</mapper>
2.Sql片段
将复用的代码段提取出来
例子:
<sql id="BASE_COLUMN_LIST">
id,username,password,name,phone,email,avatar,role
</sql>
3.Mapper.xml中的模糊查询
username like concat('%',#{username},'%')
4.trim标签的使用
4.1 去除sql语句中多余的and关键字,逗号
4.2 给sql语句前拼接 “where“、“set“以及“values(“ 等前缀,或者添加“)“等后缀
4.3 可用于选择性插入、更新、删除或者条件查询等操作
<insert id="insert" parameterType="com.example.enetity.Admin" useGeneratedKeys="true">
insert into admin
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null ">id,</if>
<if test="username != null ">username,</if>
<if test="password != null ">password,</if>
<if test="name != null ">name,</if>
<if test="phone != null ">phone,</if>
<if test="email != null ">email,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null ">#{id},</if>
<if test="username != null ">#{username},</if>
<if test="password != null ">#{password},</if>
<if test="name != null ">#{name},</if>
<if test="phone != null ">#{phone},</if>
<if test="email != null ">#{email},</if>
</trim>
</insert>