MyBatis Mapper.xml的choose/case标签
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';
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfhkchb
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13