7 . 动态sql-choose

choose-when-otherwise
只能满足一个when 中的条件,互斥的条件,不能同时存在
mapper.xml
<select id="selectstateByTitleOrstyle" parameterType="Blog" resultMap="blogResultMap" >
select * from blog where state = "1"
<choose>
<when test="title != null and title != ''">
and title like #{title}
</when>
<when test="style != null and style != ''">
and style = #{style}
</when>
<otherwise>
and featured = "1"
</otherwise>
</choose>
</select>
 实际应用:
1 <choose>
2             <when test="page !=null and page.orderBy != null and page.orderBy != ''">
3                 ORDER BY ${page.orderBy}
4             </when>
5             <otherwise>
6                 ORDER BY a.update_date DESC
7             </otherwise>
8         </choose>

猜你喜欢

转载自www.cnblogs.com/zhukaixin/p/9155964.html