if用法
<!-- 使用if元素根据条件动态查询用户信息 -->
<select id="selectUserByIf" resultType="com.po.MyUser"
parameterType="com.po.MyUser">
select * from user where 1=1
<if test="uname!=null and uname!=''">
and uname like concat('%',#{uname},'%')
</if>
<if test="usex!=null and usex!=''">
and usex=#{usex}
</if>
</select>
choose,when,otherwise用法
<!-- 使用choose、when、otherwise元素根据条件动态查询用户信息 -->
<select id="selectUserByChoose" resultType="com.po.MyUser" parameterType="com.po.MyUser">
select * from user where 1=1
<choose>
<when test="uname!=null and uname!=''">
and uname like concat('%',#{uname},'%')
</when>
<when test="usex!=null and usex!=''">
and usex=#{usex}
</when>
<otherwise>
and uid > 10
</otherwise>
</choose>
</select>
set元素
<!-- 使用set元素动态修改一个用户 -->
<update id="updateUserBySet" parameterType="com.po.MyUser">
update user
<set>
<if test="uname!=null">uname=#{uname},</if>
<if test="usex!=null">usex=#{usex}</if>
</set>
where uid=#{uid}
</update>
foreach元素
item:集合中每一个元素进行迭代时的别名
index:指定一个名字,用于表示在迭代过程中每次迭代到的位置
open:该语句以什么开始
separator:在每次进行迭代之间以什么符号作为分隔符
close:以什么解释
collection为必选属性,但是特别容易出错
<!-- 使用foreach元素查询用户信息 -->
<select id="selectUserByForeach" resultType="com.po.MyUser"
parameterType="List">
select * from user where uid in
<foreach collection="list" item="item" index="index" open="("
separator="," close=")">
#{item}
</foreach>
</select>