mybatis之sql标签
if标签
<if test="id != null">
and id = #{id}
</if>
注意下面的id必须是字符串类型,因为字符串才有trim()方法。
<if test="id != null and id.trim()!=''">
and id = #{id}
</if>
choose标签
insert into tb_user VALUES (
<choose>
<when test="user.id != null">
#{user.id},
</when>
<otherwise>
null,
</otherwise>
</choose>
#{user.userName},#{user.password},#{user.name},#{user.age},#{user.sex},#{user.birthday},#{user.created},#{user.updated})
Set标签:
注意set标签作用是可以结合if标签,如果某字段为null,则不修改这个字段
<update id="updateUser">
update tb_user
<set>
<if test="userName!=null and userName.trim()!=''">
user_name = #{userName},
</if>
<if test="password!=null and password.trim()!=''">
password = #{password},
</if>
updated = now()
</set>
where id = #{id}
</update>
where标签:
update tb_user
<set>
age = #{user.age},
birthday = #{user.birthday},
updated = #{user.updated}
</set>
<where>
id = #{user.id}
</where>
foreach标签: