<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Mybatis模糊查詢:三種定義參數(shù)方法和聚合查詢、主鍵回填

          共 3748字,需瀏覽 8分鐘

           ·

          2023-04-11 12:19

          bbfd71602be3103038a1fb884206ddcd.webp程序員的成長(zhǎng)之路互聯(lián)網(wǎng)/程序員/技術(shù)/資料共享? 關(guān)注


          閱讀本文大概需要 3.5 分鐘。

          來(lái)自: blog.csdn.net/qq_53317005/article /details/129762660

          一、使用#定義參數(shù)

          1. 持久層接口添加根據(jù)名字內(nèi)容模糊查詢方法

              //?根據(jù)名字內(nèi)容模糊查詢
          List<User>?findByNameLike(String?username);

          2. UserMapper.xml映射文件添加標(biāo)簽

              <!--?使用#定義參數(shù)?-->
          <select?id="findByNameLike"?parameterType="string"?????????????
          resultType="com.mybatisstudy.pojo.User">

          ????select?*?from?user?where?username?like?#{name}
          </select>
          我們看到在映射文件中,parameterType的值為 string 而沒(méi)有寫java.lang.String?,這是為什么呢?參數(shù)/返回值類型為基本數(shù)據(jù)類型/包裝類/String等類型時(shí),我們可以寫全類名,也可以寫別名。下表就是一些數(shù)據(jù)類型對(duì)應(yīng)的別名a0ca0a00b4aba59be3261cac253e0767.webp

          3. 添加測(cè)試方法

              

          //?測(cè)試根據(jù)名字模糊查詢
          ????@Test
          ????public?void?testFindByNameLike(){
          ????????String?like?=?"%l%";
          ????????List<User>?list?=?userMapper.findByNameLike(like);
          ?
          ????????list.forEach(System.out::println);
          ????}

          4. 運(yùn)行結(jié)果

          869dd2c3f8142e06006880dd0a325575.webpOK,這里是成功查詢出來(lái)了,并且控制臺(tái)打印日志也和我們的參數(shù)一致

          二、使用$定義參數(shù)

          模糊查詢?nèi)绻幌朐谡{(diào)用方法時(shí)參數(shù)加%,可以使用拼接參數(shù)的方式設(shè)置Sql:

          1. UserMapper.xml映射文件更改標(biāo)簽內(nèi)容

              

          <!--?使用$定義參數(shù)?-->
          ????<select?id="findByNameLike"?resultType="com.mybatisstudy.pojo.User"?parameterType="string">
          ????????select?*?from?user?where?username?like?'%${value}%'
          ????</select>

          2. 修改測(cè)試方法

              

          //?測(cè)試根據(jù)名字模糊查詢
          ????@Test
          ????public?void?testFindByNameLike(){
          ????????String?like?=?"l";
          ????????List<User>?list?=?userMapper.findByNameLike(like);
          ?
          ????????list.forEach(System.out::println);
          ????}

          3. 運(yùn)行結(jié)果

          cd2d4aade2a1d7d6a1fbb64e294d6757.webp#和$的區(qū)別:
          • #表示sql模板的占位符,$表示將字符串拼接到sql模板中。
          • #可以防止sql注入,一般能用#就不用$。
          • ${}內(nèi)部的參數(shù)名必須寫value。

          三、使用<bind>標(biāo)簽定義參數(shù)

          如果使用?#?還不想在調(diào)用方法的參數(shù)中添加?%?,可以使用?<bind>?,?<bind>?允許我們?cè)?Sql語(yǔ)句以外創(chuàng)建一個(gè)變量,并可以將其綁定到當(dāng)前的Sql語(yǔ)句中。用法如下:

          1. UserMapper.xml映射文件更改標(biāo)簽內(nèi)容

              

          <!--?使用<bind>標(biāo)簽定義參數(shù)?-->
          ????<select?id="findByNameLike"?parameterType="String"?resultType="com.mybatisstudy.pojo.User">
          ????????<bind?name="likeName"?value="'%'?+?username?+?'%'"/>
          ????????select?*?from?user?where?username?like?#{likeName}
          ????</select>

          2. 運(yùn)行結(jié)果

          f3635281d21a96990ecca04c7f19d4bc.webp

          四、聚合查詢

          1. 持久層接口添加查詢所有用戶個(gè)數(shù)方法

              

          //?查詢用戶總數(shù)
          ????int?findCount();

          2. UserMapper.xml添加標(biāo)簽

              

          <!--?聚合查詢?-->
          ????<select?id="findCount"?resultType="int">
          ????????select?count(id)?from?user
          ????</select>

          3. 添加測(cè)試方法

              

          //?測(cè)試聚合查詢方法
          ????@Test
          ????public?void?testFindCount(){
          ????????System.out.println(userMapper.findCount());
          ????}

          4. 運(yùn)行結(jié)果

          1630a0b8b1329af4153de54d5223cee5.webp79f2de4ccbbcf9a0643f7e8e769611e9.webp還是比較可靠的,確實(shí)查詢出來(lái)了用戶總數(shù)

          五、主鍵回填

          有時(shí)我們需要獲取新插入數(shù)據(jù)的主鍵值。如果數(shù)據(jù)庫(kù)中主鍵是自增的,這時(shí)我們就需要使用MyBatis的主鍵回填功能。

          1. 持久層接口添加新增用戶方法

              

          //?主鍵回填-新增用戶
          ????void?add2(User?user);

          2. UserMapper.xml添加標(biāo)簽

              

          <!--?主鍵回填?-->
          ????<insert?id="add2"?parameterType="com.mybatisstudy.pojo.User">
          ????????<!--?keyProperty:主鍵屬性名,keyColumn:主鍵列名,resultType:主鍵類型,order:執(zhí)行時(shí)機(jī)?-->
          ????????<selectKey?keyProperty="id"?keyColumn="id"?resultType="int"?order="AFTER">
          ????????????select?last_insert_id();
          ????????</selectKey>
          ????????insert?into?user(username,sex,address)
          ????????values?(#{username},#{sex},#{address})
          ????</insert>

          SELECT LAST_INSERT_ID():查詢剛剛插入的記錄的主鍵值,只適用于自增主鍵,且必須和insert語(yǔ)句一起執(zhí)行。

          3. 添加測(cè)試方法

              

          //?測(cè)試主鍵回填功能
          ????@Test
          ????public?void?testAdd2(){
          ????????User?user?=?new?User("Lions","man","Beijing");
          ????????userMapper.add2(user);
          ?
          ????????session.commit();
          ????????System.out.println(user.getId());
          ????}

          4. 運(yùn)行結(jié)果

          e105c81cafb2487611894e82d7197346.webp好,控制臺(tái)是成功顯示添加了,用戶id為11,那我們看看表里面是否成功添加了58923c608103ca740608c18b96d479fa.webp確實(shí)是成功添加了,ok,本篇文章到此為止了,感謝小伙伴的瀏覽,順便點(diǎn)擊下面投一下票囖,看看您是否有學(xué)到了呢。<END>

          推薦閱讀:

          優(yōu)雅的接口防刷處理方案

          你還在用 @Autowired 和 @Resource?

              
                  互聯(lián)網(wǎng)初中高級(jí)大廠面試題(9個(gè)G)
                
              

          內(nèi)容包含Java基礎(chǔ)、JavaWeb、MySQL性能優(yōu)化、JVM、鎖、百萬(wàn)并發(fā)、消息隊(duì)列、高性能緩存、反射、Spring全家桶原理、微服務(wù)、Zookeeper......等技術(shù)棧!

          ?戳閱讀原文領(lǐng)??! ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??朕已閱? 4dca88c83fb0e49d45c00a33e8e65ea7.webp

          瀏覽 38
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  欧美手机在线观看 | 毛片网在线| 国产精品国产三级国产专区53 | 欧美成人做爰高潮片免费看贝隆尼 | 成人精品在线观看 |