一、本课目标
- 掌握使用if+set完成更新操作
- 掌握使用if+trim完成更新操作
二、if + set
在之前的更新操作中,sql语句如下:
<update id="modify" parameterType="user">
update smbms_user set userCode=#{userCode},
userName=#{userName},userPassword=#{userPassword},
gender=#{gender},phone=#{phone},address=#{address},
userRole=#{userRole},modifyBy=#{modifyBy},
modifyDate=#{modifyDate} where id=#{id}
</update>
传进来的参数类型是实体类,sql语句对每一个字段都进行了更新,但是在实际情况中,用户并不是对每个字段都修改了的。所以可以进行一下判断,如果某个字段传进来的值为空,那就不需要set了。
在之前xml配置文件基础上,测试代码如下时:
@Test
public void testModify() {
logger.debug("testAdd-------------------");
SqlSession sqlSession = null;
int count = 0;
sqlSession = MyBatisUtil.createSqlSession();
User user = new User();
try {
user.setId(16);
// user.setUserCode("001_M");
user.setUserName("测试用户001_M");
user.setAddress("测试地址_M");
// user.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2018-8-25"));
// user.setGender(1);
// user.setUserPassword("123456");
// user.setPhone("13526543651");
// user.setUserRole(1);
user.setModifyBy(1);
user.setModifyDate(new Date());
count = sqlSession.getMapper(UserMapper.class).modify(user);
sqlSession.commit();
} finally {
MyBatisUtil.closeSqlSession(sqlSession);
}
logger.debug(count);
}
运行结果:

数据库:

可以看到对于没有传进去参数的字段,都直接设置为null了,这当然是错误的。
也就是说,在这种情况下,更新用户表数据时,若某个参数为null,会导致更新错误。
解决方式是使用set+if做判断,修改后的xml文件如下:
<update id="modify" parameterType="user">
update
smbms_user
<set>
<if test="userCode != null">userCode=#{userCode},</if>
<if test="userName != null">userName=#{userName},</if>
<if test="userPassword != null">userPassword=#{userPassword},</if>
<if test="gender != null">gender=#{gender},</if>
<if test="phone != null">phone=#{phone},</if>
<if test="address != null">address=#{address},</if>
<if test="userRole != null">userRole=#{userRole},</if>
<if test="modifyBy != null">modifyBy=#{modifyBy},</if>
<if test="modifyDate != null">modifyDate=#{modifyDate},</if>
</set>
where id=#{id}
</update>
测试代码:
@Test
public void testModify() {
logger.debug("testAdd-------------------");
SqlSession sqlSession = null;
int count = 0;
sqlSession = MyBatisUtil.createSqlSession();
User user = new User();
try {
user.setId(14);
// user.setUserCode("001_M");
user.setUserName("测试用户001_M");
user.setAddress("测试地址_M");
// user.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2018-8-25"));
// user.setGender(1);
// user.setUserPassword("123456");
// user.setPhone("13526543651");
// user.setUserRole(1);
user.setModifyBy(1);
user.setModifyDate(new Date());
count = sqlSession.getMapper(UserMapper.class).modify(user);
sqlSession.commit();
} finally {
MyBatisUtil.closeSqlSession(sqlSession);
}
logger.debug(count);
}
运行之后结果正确,数据库结果也正确。

注:在set标签里面的if标签里面,后面要加上逗号,最后一个也加上,set标签有自动剔除功能。
三、if+trim
使用if+trim替代if+set进行更新用户表数据,效果一样。

xml配置文件如下:
<update id="modify" parameterType="user">
update
smbms_user
<trim prefix="set" suffixOverrides="," suffix="where id=#{id}">
<if test="userCode != null">userCode=#{userCode},</if>
<if test="userName != null">userName=#{userName},</if>
<if test="userPassword != null">userPassword=#{userPassword},</if>
<if test="gender != null">gender=#{gender},</if>
<if test="phone != null">phone=#{phone},</if>
<if test="address != null">address=#{address},</if>
<if test="userRole != null">userRole=#{userRole},</if>
<if test="modifyBy != null">modifyBy=#{modifyBy},</if>
<if test="modifyDate != null">modifyDate=#{modifyDate},</if>
</trim>
</update>
运行结果正常。

四、小结

网友评论