1.set元素,set元素可以去掉SQL中多余的逗号
<?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">
<!-- namespace对应的是接口全限定名称 于是mybatis上下文就通过它找到对应的接口-->
<mapper namespace="xuexi.mybatis.dao.mapper.SysRoleMapper">
<update id = "updateSysRole" parameterType="sYsRole" >
update role
<set>
//r_name,note参数不为空时传入参数,此时note为空所以只传入r_name参数
//此时SQL中 #{r_name},后边多了个逗号,通过set就可以去掉
<if test = "r_name !=null and r_name !=''">
r_name = #{r_name},
</if>
<if test = "note !=null and note !=''">
note = #{note}
</if>
</set>
where r_id = #{r_id}
</update>
</mapper>
2.trim元素
prefix:在trim标签前加内容
suffix:在trim标签后加内容
prefixOverrides:trim标签内去掉SQL最前边的字符,如逗号prefixOverrides="and"
suffixOverrides:trim标签内去掉SQL最后边的字符,如逗号prefixOverrides=","
2.1 prefix trim标签前边加内容 set 字符 prefix="set"
suffixOverrides trim标签内SQL去掉最后一个字符逗 suffixOverrides=","
示例如下
<update id = "updateSysRole" parameterType="sYsRole" >
update role
<trim prefix="set" suffixOverrides=",">
<if test = "r_name !=null and r_name !=''">
r_name = #{r_name},
</if>
<if test = "note !=null and note !=''">
note = #{note},
</if>
<if test = "phone !=null and phone !=''">
phone = #{phone}
</if>
</trim>
where r_id = #{r_id}
</update>
对应SQL
update role set r_name = ?, note = ?,phone where r_id = ?
3.where元素
where元素作用主要是用来简化sql语句中where条件判断的书写的
where 标记会自动将其后第一个条件的and或者是or给去掉
<select id = "selectRole" parameterType="sYsRole" resultType="sYsRole" >
select * from role
<where>
<if test = "r_id !=null and r_id != ''">
and r_id = #{r_id}
</if>
<if test = "r_name !=null and r_name != ''">
and r_name = #{r_name}
</if>
</where>
</select>
对应SQL
select * from role WHERE r_id = ? and r_name = ?
如果where标签内的if标签的 布尔值都为false,SQL如下
select * from role