• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

MyBatis Mapper.xml的choose/case标签

武飞扬头像
sunnyday0426
帮助5

1.1 简述

今天在开发时使用choose动态拼接where后面的sql语句,以及使用到了case when then,现进行简单的记录。

1.2 choose详解

MyBatis Mapper.xmlchoose标签的作用类似于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
系列文章
更多 icon
同类精品
更多 icon
继续加载