同where标签功能类似,where用于去除第一个条件中出现的and前缀,那么set标签就是去除最后一个更新字段语句中出现的,[逗号]后缀。
where 和 set 完全都可以被trim替换使用,可以参考JavaEE互联网轻量级框架整合开发P157页。
where示例:
<select id="findByParam" resultMap="BaseResultMap" parameterType="map">
select
<include refid="Base_Column_List" />
from users
<where>
<if test="userName != null and userName != ''">
and userName = #{userName}
</if>
<if test="userSex != null and userSex != ''">
and user_sex = #{userSex}
</if>
<if test="nickName!= null and nickName!= ''">
and nick_name = #{nickName}
</if>
</where>
</select>
若只传递了nickName,实际打印的sql如下。
打印的sql中 and 自动去掉了,无需在sql中添加“1=1”。
set示例:
mybatis使用注解方式
@Update({"<script>",
"update role ",
"<set>",
" <if test='roleName != null'>role_name=#{roleName},</if>" ,
" <if test='roleCode != null'>role_code=#{roleCode},</if>" ,
"</set>",
"where id=#{id}",
"</script>"})
void update(Role role);
打印的sql中 更新第二个字段的 ,去掉了。
参考了他人的博客,地址如下
https://blog.csdn.net/u014268482/article/details/80625393