MyBatis动态sql之choose、when、otherwise标签
有些时候不想用到所有的条件语句,而只想从中择取一二,针对这种情况,MyBatis 提供了 元素,它有点像 Java 中的 switch 语句。
<!--使用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>
来源:http://c.biancheng.net/view/4376.html