1、set标签特点:
(1)set标签用于更新语句中
(2)set标签解析为set关键字
(3)set可以去除跟新语句中无用的逗号
(4)通常是和if标签一起使用
2、set标签的使用
(1)编写接口方法
/**
* 更新user
* @param user
*/
int updateUserById(User user);
(2)编写sql语句
<update id="updateUserById" parameterType="user">
update user
<set>
<if test="uid!=null">
uid=#{uid},
</if>
<if test="uname!=null and uname!=''">
uname=#{uname},
</if>
<if test="sex!=null and sex!=''">
sex=#{sex},
</if>
<if test="password!=null and password!=''">
password=#{password},
</if>
<if test="birthday!=null">
birthday=#{birthday}
</if>
</set>
where uid=#{uid}
</update>
(3)测试
@Test
public void demo03(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user=new User("","女","",null);
user.setUid(6);
int i=mapper.updateUserById(user);
System.out.println(i);
sqlSession.close();
}
通过产生的sql语句可以看出,当set标签中有条件成立时就会附加set关键字,字段为null时该列不会被更新。set可以忽略与sql无关的逗号。
更多推荐
Mybatis的set标签
发布评论