最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MyBatis注解實(shí)現(xiàn)動(dòng)態(tài)SQL問題

 更新時(shí)間:2023年02月07日 08:58:39   作者:楊哥學(xué)編程  
這篇文章主要介紹了MyBatis注解實(shí)現(xiàn)動(dòng)態(tài)SQL問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

MyBatis注解實(shí)現(xiàn)動(dòng)態(tài)SQL

在 Mybatis 中,使用注解可以很方便的進(jìn)行sql操作,但很多動(dòng)態(tài) SQL 都是由 xml 配置實(shí)現(xiàn)的。

而隨著 SpringBoot的逐漸發(fā)展,越來(lái)越多的配置由配置文件轉(zhuǎn)成注解的形式。其中包括動(dòng)態(tài) SQL 。

Mybatis 的注解中,使用太過長(zhǎng)的sql語(yǔ)句看起來(lái)很不美觀。

@Select("SELECT title from book_tbl where id = #{id}")

如果想要在 mapper 中進(jìn)行非必要關(guān)鍵字的查詢時(shí)就需要使用動(dòng)態(tài) SQL,與 xml 配置不同的是,@Select注解中 SQL 語(yǔ)句必須以 <script> 標(biāo)簽包裹。

@Select("<script>"+
			"SELECT id " +
            "from book_tbl" +
            "<where>" +
            "<if test 'company_id != null'>" +
            "and company_id = #{company_id}" +
            "</if>" +
            "<if test 'title != null'>" +
            "and title like CONCAT('%',#{title},'%')" +
            "</if>" +
            "</where>" +
            "ORDER BY create_time desc,rank desc" +
        "</script>")

MyBatis動(dòng)態(tài)拼接 SQL參數(shù)

之前用JPA可以在@Query注解上拼接sql,實(shí)現(xiàn)動(dòng)態(tài)查詢;現(xiàn)在用mybatis,如何實(shí)現(xiàn)sql的動(dòng)態(tài)拼接參數(shù)

舉例,在JPA中可以實(shí)現(xiàn)類似于下面的sql拼接

plateNumber2 = plateNumber2 + "," + plateNumber;
String[] split = plateNumber2.split(",");
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("SELECT plate_number from p_park_record where park_key = '"+parkDO.getParkKey()+"'");
stringBuffer.append(" and (");
for (int i = 0; i < split.length; i++) {
? ? stringBuffer.append(" plate_number = "+split[i]);
? ? if (i != split.length-1){
? ? ? ? stringBuffer.append(" OR ");
? ? }
}
stringBuffer.append(" )");
stringBuffer.append(" order by sensor_exit_time desc");
stringBuffer.append(" limit 1");

此代碼中,核心問題是for循環(huán),將不確定數(shù)量的同一個(gè)參數(shù)做拼接

那么在mybatis中該如何實(shí)現(xiàn)

mybatis實(shí)現(xiàn):

plateNumber2 = plateNumber2 + "," + plateNumber;
ParkRecordDO parkRecordDO = new ParkRecordDO();
parkRecordDO.setPlateNumber(plateNumber2);
parkRecordDO.setParkKey(parkDO.getParkKey());
String onlyPlateNum = parkRecordMapper.getOnlyPlateNum(parkRecordDO);
? ? <select id="getOnlyPlateNum" parameterType="ParkRecordDO" resultType="java.lang.String">
? ? ? ? SELECT plate_number from p_park_record where park_key = #{parkKey} and sensor_exit_time is not null and
? ? ? ? <foreach collection="plateNumber.split(',')" index="index" item="id" open="(" separator=" or " close=")">
? ? ? ? ? ? plate_number = #{id}
? ? ? ? </foreach>
? ? ? ? order by sensor_exit_time desc
? ? ? ? limit 1
? ? </select>

如此實(shí)現(xiàn),與JPA拼接效果相同,重點(diǎn)在于“<foreach”標(biāo)簽中separator屬性,該屬性為元素之間的分隔符,將分隔符設(shè)置為“or”,以此來(lái)實(shí)現(xiàn)該程序邏輯的拼接

最后補(bǔ)充幾個(gè)知識(shí)點(diǎn)

foreach標(biāo)簽的使用

<!--
? ? ? ? foreach
? ? ? ? collection:指定要遍歷的集合
? ? ? ? ? ? ? ? ? ? list類型的參數(shù)會(huì)特殊處理封裝在map中,map的key就叫l(wèi)ist
? ? ? ? ? ? ? item:將當(dāng)前遍歷出的元素賦值給指定的變量
? ? ? ? ? ? ? separator:元素之間的分割符
? ? ? ? ? ? ? open: 遍歷出的所有結(jié)果拼接一個(gè)開始字符
? ? ? ? ? ? ? close:遍歷出的所有結(jié)果拼接一個(gè)結(jié)束字符
? ? ? ? ? ? ? index:索引,遍歷list的時(shí)候是索引,item就是當(dāng)前值;
? ? ? ? ? ? ? ? ? ? ? ? ? 遍歷map時(shí)index標(biāo)識(shí)的就是map的key,item就是map的值
? ? ? ? ? ? ? #{變量名}: 就能取出變量的值就是當(dāng)前遍歷出的元素
-->
<select id="queryUserByForEach" resultType="user">
? ? select * from easybuy_user where id in
? ? <foreach collection="ids" item="item_id" separator=","
? ? ? ? ? ? ?open="(" close=")">
? ? ? ? #{item_id}
? ? </foreach>
</select>


<insert id="batchSaveUser" parameterType="user">
? ? insert into easybuy_user(loginName, userName, password) values
? ? <foreach collection="userList" item="user" separator=",">
? ? ? ? (#{user.loginName}, #{user.userName}, #{user.password})
? ? </foreach>
</insert>

set標(biāo)簽的使用

<!--
? ? ? ? 使用set標(biāo)簽或者trim標(biāo)簽與if標(biāo)簽相結(jié)合
? ? ? ? 實(shí)現(xiàn)動(dòng)態(tài)更新sql語(yǔ)句的拼接
-->
<update id="updateUserByCondition" parameterType="user">
? ? update easybuy_user
? ? <set>
? ? ? ? <if test="userName != null">
? ? ? ? ? ? username = #{userName},
? ? ? ? </if>
? ? ? ? <if test="email != null">
? ? ? ? ? ? email = #{email},
? ? ? ? </if>
? ? </set>
? ? where id = 26;
</update>

choose和when標(biāo)簽的使用(作用類似與java中的switch-case)

<select id="queryUserByChoose" resultType="com.unistart.entity.User">
? ? select * from easybuy_user
? ? <where>
? ? ? ? <choose>
? ? ? ? ? ? <when test="id != null">
? ? ? ? ? ? ? ? id=#{id}
? ? ? ? ? ? </when>
? ? ? ? ? ? <when test="userName != null">
? ? ? ? ? ? ? ? userName like #{userName}
? ? ? ? ? ? </when>
? ? ? ? ? ? <when test="sex != null">
? ? ? ? ? ? ? ? sex = #{sex}
? ? ? ? ? ? </when>
? ? ? ? ? ? <otherwise>
? ? ? ? ? ? ? ? 1=1;
? ? ? ? ? ? </otherwise>
? ? ? ? </choose>
? ? </where>
</select>

trim標(biāo)簽的使用

<!--
? ? ? ? trim標(biāo)簽的使用:解決后面多出的and或or
? ? ? ? prefix="":前綴,trim標(biāo)簽體中是整個(gè)字符串拼串后的結(jié)果
? ? ? ? ? ? ? ? prefix給拼串后的整個(gè)字符串加一個(gè)前綴
? ? ? ? prefixOverrides="":前綴覆蓋:去掉整個(gè)字符串前面多余的字符
? ? ? ? suffix="":后綴,給拼串后的整個(gè)字符串加一個(gè)后綴
? ? ? ? suffixOverrides="":后綴覆蓋,去掉整個(gè)字符串后面多余的字符
-->
<select id="queryUserByTrim" resultType="user">
? ? select * from easybuy_user
? ? <trim prefix="where" suffixOverrides="and">
? ? ? ? <if test="id != null">
? ? ? ? ? ? id = #{id} and
? ? ? ? </if>
? ? ? ? <if test="userName != null and userName!=&quot;&quot;">
? ? ? ? ? ? userName like #{userName} and
? ? ? ? </if>
? ? ? ? <if test="email != null and email!=&quot;&quot;">
? ? ? ? ? ? email = #{email} and
? ? ? ? </if>
? ? </trim>
</select>

where標(biāo)簽的使用

<!--
? ? ? ? 查詢用戶:要求,攜帶哪些字段查詢條件就按這些字段進(jìn)行查詢
? ? ? ? 使用的OGNL,類似與EL表達(dá)式

? ? ? ? 從參數(shù)中取值進(jìn)行判斷,遇見特殊符號(hào)使用轉(zhuǎn)義字符
? ? ? ? 使用where標(biāo)簽時(shí)要注意,它只能去除第一個(gè)多出來(lái)的and或or
-->
<select id="queryUserByCondition" resultType="user">
? ? select * from easybuy_user
? ? <where>
? ? ? ? <if test="id != null">
? ? ? ? ? ? and id = #{id}
? ? ? ? </if>
? ? ? ? <if test="userName != null and userName!=&quot;&quot;">
? ? ? ? ? ? and userName like #{userName}
? ? ? ? </if>
? ? ? ? <if test="email != null and email!=&quot;&quot;">
? ? ? ? ? ? and email = #{email}
? ? ? ? </if>
? ? </where>
</select>

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • mybatis-plus主鍵策略生成失敗的解決

    mybatis-plus主鍵策略生成失敗的解決

    本文主要介紹了mybatis-plus主鍵策略生成失敗的解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • 使用注解+RequestBodyAdvice實(shí)現(xiàn)http請(qǐng)求內(nèi)容加解密方式

    使用注解+RequestBodyAdvice實(shí)現(xiàn)http請(qǐng)求內(nèi)容加解密方式

    這篇文章主要介紹了使用注解+RequestBodyAdvice實(shí)現(xiàn)http請(qǐng)求內(nèi)容加解密方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java數(shù)據(jù)結(jié)構(gòu)與算法之單鏈表深入理解

    Java數(shù)據(jù)結(jié)構(gòu)與算法之單鏈表深入理解

    這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)與算法之單鏈表深入理解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • 16個(gè)SpringBoot擴(kuò)展接口的總結(jié)和實(shí)例

    16個(gè)SpringBoot擴(kuò)展接口的總結(jié)和實(shí)例

    Spring Boot是一個(gè)開源的Java框架,它簡(jiǎn)化了基于Spring的應(yīng)用程序的開發(fā)和部署,它提供了許多強(qiáng)大的特性和擴(kuò)展接口,本文給大家介紹了16個(gè)常用的Spring Boot擴(kuò)展接口,需要的朋友可以參考下
    2023-09-09
  • Java中RocketMQ的流量削峰詳解

    Java中RocketMQ的流量削峰詳解

    這篇文章主要介紹了Java中RocketMQ的流量削峰詳解,MQ的主要特點(diǎn)為解耦、異步、削峰,該文章主要記錄與分享個(gè)人在實(shí)際項(xiàng)目中的RocketMQ削峰用法,用于減少數(shù)據(jù)庫(kù)壓力的業(yè)務(wù)場(chǎng)景,需要的朋友可以參考下
    2023-09-09
  • Spring監(jiān)聽器及定時(shí)任務(wù)實(shí)現(xiàn)方法詳解

    Spring監(jiān)聽器及定時(shí)任務(wù)實(shí)現(xiàn)方法詳解

    這篇文章主要介紹了Spring監(jiān)聽器及定時(shí)任務(wù)實(shí)現(xiàn)方法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • 我總結(jié)的幾種@Transactional失效原因說(shuō)明

    我總結(jié)的幾種@Transactional失效原因說(shuō)明

    這篇文章主要是我總結(jié)的幾種@Transactional失效原因說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Java中遍歷Map的六種方法實(shí)現(xiàn)

    Java中遍歷Map的六種方法實(shí)現(xiàn)

    這篇文章主要介紹了Java中遍歷Map的六種方法,Map是常用的數(shù)據(jù)結(jié)構(gòu)之一,它提供了鍵值對(duì)的存儲(chǔ)方式,可以方便地進(jìn)行數(shù)據(jù)查找和操作,感興趣想要詳細(xì)了解可以參考下文
    2023-05-05
  • Java中ArrayList在foreach里remove的問題詳析

    Java中ArrayList在foreach里remove的問題詳析

    這篇文章主要給大家介紹了關(guān)于Java中ArrayList在foreach里remove問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧
    2018-09-09
  • Java泛型中<?>和<T>的區(qū)別淺析

    Java泛型中<?>和<T>的區(qū)別淺析

    <T>和<?>的區(qū)別 <T>是參數(shù)類型,常常用于泛型類或泛型方法的定義,下面這篇文章主要給大家介紹了關(guān)于Java泛型中<?>和<T>區(qū)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-12-12

最新評(píng)論

尚志市| 武陟县| 滕州市| 金湖县| 乾安县| 泉州市| 恩平市| 清水河县| 漳平市| 平江县| 禄丰县| 乌兰县| 江源县| 长海县| 沁水县| 孟村| 丰宁| 嘉鱼县| 淮滨县| 唐海县| 涞源县| 青海省| 南康市| 汪清县| 富宁县| 罗定市| 台前县| 嘉义县| 西平县| 伊吾县| 丘北县| 抚顺县| 南昌县| 乐山市| 伊宁市| 博兴县| 高碑店市| 中超| 秭归县| 布拖县| 乾安县|