一、< if >元素
动态SQL通常要做的事情是有条件地包含where子句的一部分。所以在MyBatis中,< if >元素是最常用的元素。它类似于Java中的if语句。在应用中,测试< if >元素,具体过程如下:
1.添加SQL映射语句
2.添加数据操作接口方法
3.调用数据操作接口方法
4.测试动态SQL语句
<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 >元素
<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