1.1 简述
今天在开发时使用choose
动态拼接where
后面的sql
语句,以及使用到了case when then
,现进行简单的记录。
1.2 choose
详解
MyBatis Mapper.xml
的choose
标签的作用类似于java
中的switch
语句,使用choose
标签时为了避免使用大量的if
元素。choose
元素包含了when
元素(对应java
中的case
语句),otherwise
元素则对应java
中的default
语句。
1.2.1 choose
元素的DTD
定义
<!-- choose标签类似于java中的switch语句,用于大量的动态SQL,避免使用很多是if标签。 -->
<!ELEMENT choose (when* , otherwise?)>
备注:其中when
元素可以存在多个,otherwise
元素只能存在一个或零个。
1.2.2 when
元素的DTD
定义
<!-- when标签类似于java中的case语句,该标签存在一个test属性,用来测试给出的值是否与表达式匹配。
如果匹配,则将该SQL子语句应用到SQL语句中。 -->
<!ELEMENT when (#PCDATA | include | trim | where | set | foreach | choose | if | bind)*>
<!ATTLIST when
test CDATA #REQUIRED>
备注:test
属性表示该when
元素中的SQL
被使用的测试条件。
1.2.3 otherwise
元素的定义
<!-- otherwise标签类似于java中的default,当choose的所有when都没有满足时,则应用该标签 -->
<!ELEMENT otherwise (#PCDATA | include | trim | where | set | foreach | choose | if | bind)*>
1.2.4 实例
获取查询信息,使用多个choose
来动态拼凑条件。
<select id="selectConditionList" resultType="com.java.vo.GroupVO"
parameterType="com.java.vo.filter.GroupFilterVO">
SELECT count(b.sfp_user_id) people,a.id,a.group_name groupName, a.group_accord_name groupAccordName,a.app_id
appId,a.create_by createBy,
a.create_time createTime
FROM user_group a LEFT JOIN group_user b on b.user_group_id = a.id
<where>
a.app_id = #{appId}
<choose>
<when test="onlyMine">
and a.create_by=#{userNm}
<choose>
<when test="keyword != null and keyword !=''">
and a.group_accord_name like CONCAT('%',#{keyword},'%')
</when>
</choose>
</when>
<otherwise>
<choose>
<when test="keyword != null and keyword !=''">
and (a.create_by like CONCAT('%',#{keyword},'%') or a.group_accord_name like
CONCAT('%',#{keyword},'%'))
</when>
</choose>
</otherwise>
</choose>
</where>
GROUP BY a.group_name , a.group_accord_name ,a.app_id , a.create_by ,a.create_time ,a.id
ORDER BY
<choose>
<when test='peopleAscDesc == "0"'>
people DESC,createTime DESC
</when>
<when test='peopleAscDesc == "1"'>
people ASC,createTime DESC
</when>
<otherwise>
createTime DESC
</otherwise>
</choose>
</select>
1.3 CASE
详解
在mapper.xml
文件中,需要直接给查询的数据直接赋值或者别名显示的时候需要用到CASE WHEN THEN
1.3.1 查询
简单case
函数(推荐)
CASE xxx WHEN ‘x’ THEN ‘x’ WHEN ‘x’ THEN ‘x’ ELSE ‘xx’ END
case
搜索函数
CASE WHEN xxx = ‘x’ THEN ‘x’ WHEN xxx = ‘x’ THEN ‘x’ ELSE ‘xx’ END
扩展
select case
when total > max then 2
when total < min then 1
else 0
end
from ta
<select id="selectInfos" resultType="com.java.vo.passthrough.SfpUserVO"
parameterType="com.java.vo.filter.UserAnalysisFilterVO">
select a.id,
deviceId,
gender,
CASE gender
when '0' then '男'
when '1' then '女'
else '未知'
end genderName
from
sfp_user a left join group_user b on a.id = b.sfp_user_id AND b.user_group_id =
#{id}
<where>
b.user_group_id = #{id}
<if test="keyword !=null and keyword !=''">
AND (a.user_code like CONCAT('%',#{keyword},'%') or a.device_id like CONCAT('%',#{keyword},'%'))
</if>
<if test="gender !=null and gender !=''">
AND a.gender = #{gender}
</if>
<if test="cityCode !=null and cityCode !=''">
AND a.city_code = #{cityCode}
</if>
<if test="deviceId !=null and deviceId !=''">
AND a.device_id = #{deviceId}
</if>
<if test="registrationStartTime !=null and registrationEndTime !=null">
AND (a.registration_time BETWEEN #{registrationStartTime} AND #{registrationEndTime})
</if>
</where>
ORDER BY registrationTime DESC
</select>
1.3.2 更新
UPDATE table SET xxx =(
CASE
WHEN xxx > xx THEN 'xx'
WHEN xxx > xx THEN 'xx'
ELSE 'xx'
END )
WHERE y = 'a';