动态SQL

时间:2023-02-09 07:21:53
一、where+if:
<where>自动判断其内部是否有返回值,如果有,则插入一个where并自动删除SQL语句前多余的and或or
<select id="getUserList" resultType="User">
select * from smbms_user
<where>
<if test="name !=null and name != ''">
userName like concat('%',#{name},'%')
</if>
<if test="role != null">
and userRole=#{role}
</if>
</where>
</select>
二、set+if:
<se>t标签自动判断其内部是否有返回值,如果有则插入一个set,并且删除SQL语句最后一个逗号
<update id="updateUser">
update smbms_user
<set>
<if test="userCode != null and userCode != ''">userCode=#{userCode},</if>
<if test="userPassword != null and userPassword != ''">userPassword=# {userPassword},</if>
<if test="userName != null and userName != ''">userName=#{userName},</if>
<if test="gender != null">gender=#{gender},</if>
<if test="birthday != null!= ''">birthday=#{birthday},</if>
<if test="phone != null">phone=#{phone},</if>
<if test="address != null and address != ''">address=#{address},</if>
<if test="userRole != null">userRole=#{userRole},</if>
<if test="createdBy != null">createdBy=#{createdBy},</if>
<if test="creationDate != null">creationDate=#{creationDate},</if>
<if test="modifyBy != null">modifyBy=#{modifyBy},</if>
<if test="modifyDate != null">modifyDate=#{modifyDate},</if>
</set>
where id=#{id}
</update>
三、foreach:
<foreach>自动遍历参数数组或今后。
属性:
collection:参数(Map集合)的键名,如果是数组,键名自动为array;如果是List,键名自动是list;如果是Map,键名需要于定义的键名保持相同。
item:参数名
oppen/close:以oppen开始,close结束
separator:一般是逗号
<select id="getUserListByRole" resultType="User">
select * from smbms_user
where userRole in
<foreach collection="array" item="roles" open="(" close=")" separator=",">
#{roles}
</foreach>
</select>
四、MyBatis参数小结:
1、MyBatis可以接收的参数类型有:基本数据类型、对象、List、数组、Map
2、无论是哪种类型,MyBatis都会将它封装一个Map集合
3、如果是单个参数有一下几种情况:
参数是基本数据类型:变量名为key,变量值为value;
参数是对象:对象的属性名为key,属性值为value;
参数是List:默认将“list”为key,该List本身为value;
参数数数组:默认将“array”为key,该数组本身为value;
参数是Map:MyBatis就使用我们自定义的键值对
五、choose+when+otherwise:
类似于Java中的switch结构,和JSTL中的choose基本一样;
<choose>以此判断<when>中的语句是否有返回值,如果有,则执行,并跳出整个<choose>结构;如果没有,则默认执行<otherwise>中的语 句,并且<otherwise>可省略
六、分页:
limit x,y: x从哪一行开始显示(下标从0开始),y显示y条
(m-1)*n

七、trim+if:
<trim>自动判断内部是否有返回值,如果有,则插入一个“前缀”或“后缀”,并且自动覆盖SQL语句中的指定内容:“前缀覆盖”“后缀覆盖”。
prefix:前缀 suffix:后缀
prefixOverrides:前缀覆盖
<select id="getUserList" resultType="User">
select * from smbms_user
<trim prefix="where" prefixOverrides="and | or">
<if test="name !=null and name != ''">
userName like concat('%',#{name},'%')
</if>
<if test="role != null">
and userRole=#{role}
</if>
</trim>
</select>
生成的SQL语句:select * from smbms_user where userName like concat('%',?,'%')
seffixOverrides:后缀覆盖
<update id="updateUser">
update smbms_user
<trim suffix="where id=#{id}" suffixOverrides="," prefix="set">
<if test="userCode != null and userCode != ''">userCode=#{userCode},</if>
<if test="userPassword != null and userPassword != ''">userPassword=#{userPassword},</if>
<if test="userName != null and userName != ''">userName=#{userName},</if>
<if test="gender != null">gender=#{gender},</if>
<if test="birthday != null!= ''">birthday=#{birthday},</if>
<if test="phone != null">phone=#{phone},</if>
<if test="address != null and address != ''">address=#{address},</if>
<if test="userRole != null">userRole=#{userRole},</if>
<if test="createdBy != null">createdBy=#{createdBy},</if>
<if test="creationDate != null">creationDate=#{creationDate},</if>
<if test="modifyBy != null">modifyBy=#{modifyBy},</if>
<if test="modifyDate != null">modifyDate=#{modifyDate},</if>
</trim>
</update>
生成的SQL语句:update smbms_user set userPassword=?, userName=? where id=?