转义字符参考网址
https://www.w3cschool.cn/htmltags/ref-entities.html
1. if语句实现条件查询
<!-- 动态sql-->
<select id="getUserByCondition" resultType="com.mj.domain.User">
select * from User
<where>
<if test="id!=null">
id > #{id}
</if>
<if test="username!=null">
and username like #{username}
</if>
<if test="birthday!=null">
and birthday < #{birthday}
</if>
<if test="sex!=null">
and sex=#{sex}
</if>
</where>
</select>
2. 分支查询:choose when
<!--分支查询-->
<select id="getUserByConditionChoose" resultType="com.mj.domain.User">
select * from User
<where>
<choose>
<when test="id!=null">
id > #{id}
</when>
<when test="username!=null and !username.equals("")">
username like #{username}
</when>
<when test="birthday!=null">
birthday < #{birthday}
</when>
<when test="sex!=null">
sex=#{sex}
</when>
<otherwise>
1=1
</otherwise>
</choose>
</where>
</select>
3. foreach 集合遍历
<select id="getUserByIds" resultType="com.mj.domain.User">
select * from User where id IN
<!-- 遍历集合-->
<!-- collection:集合的名称
item:集合中元素的别名,代表的为集合的元素的值value
index:索引:遍历List时保存指定变量的索引;遍历Map时保存指定变量的Key
separator:元素之间的分隔符
open:Sql开始符号
close:Sql结束符号 -->
<foreach collection="idlist" item="id_item" separator="," open="(" close=")">
#{id_item}
</foreach>
</select>
4. Update…Set…
<update id="updateUserAuto">
Update User
<set>
<if test="username!=null and !username.equals("")">
username = #{username},
</if>
<if test="birthday!=null">
birthday = #{birthday},
</if>
<if test="sex!=null">
sex = #{sex}
</if>
</set>
<where>
id = #{id}
</where>
</update>