欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MyBatis實(shí)現(xiàn)模糊查詢的幾種方式

 更新時(shí)間:2018年08月22日 11:07:11   作者:行癲  
這篇文章主要介紹了MyBatis實(shí)現(xiàn)模糊查詢的幾種方式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

在學(xué)習(xí)MyBatis過(guò)程中想實(shí)現(xiàn)模糊查詢,可惜失敗了。后來(lái)上百度上查了一下,算是解決了。記錄一下MyBatis實(shí)現(xiàn)模糊查詢的幾種方式。

數(shù)據(jù)庫(kù)表名為test_student,初始化了幾條記錄,如圖:

 

起初我在MyBatis的mapper文件中是這樣寫的:

 <select id="searchStudents" resultType="com.example.entity.StudentEntity"
  parameterType="com.example.entity.StudentEntity">
  SELECT * FROM test_student
  <where>
   <if test="age != null and age != '' and compare != null and compare != ''">
    age
    ${compare}
    #{age}
   </if>
   <if test="name != null and name != ''">
    AND name LIKE '%#{name}%'
   </if>
   <if test="address != null and address != ''">
    AND address LIKE '%#{address}%'
   </if>
  </where>
  ORDER BY id
 </select>

寫完后自我感覺良好,很開心的就去跑程序了,結(jié)果當(dāng)然是報(bào)錯(cuò)了:

經(jīng)百度得知,這么寫經(jīng)MyBatis轉(zhuǎn)換后(‘%#{name}%')會(huì)變?yōu)?‘%?%'),而(‘%?%')會(huì)被看作是一個(gè)字符串,所以Java代碼在執(zhí)行找不到用于匹配參數(shù)的 ‘?' ,然后就報(bào)錯(cuò)了。

解決方法

1.用${…}代替#{…}

<select id="searchStudents" resultType="com.example.entity.StudentEntity"
  parameterType="com.example.entity.StudentEntity">
  SELECT * FROM test_student
  <where>
   <if test="age != null and age != '' and compare != null and compare != ''">
    age
    ${compare}
    #{age}
   </if>
   <if test="name != null and name != ''">
    AND name LIKE '%${name}%'
   </if>
   <if test="address != null and address != ''">
    AND address LIKE '%${address}%'
   </if>
  </where>
  ORDER BY id
 </select>

查詢結(jié)果如下圖:

注:使用${…}不能有效防止SQL注入,所以這種方式雖然簡(jiǎn)單但是不推薦使用?。?!

2.把'%#{name}%'改為”%”#{name}”%”

 <select id="searchStudents" resultType="com.example.entity.StudentEntity"
  parameterType="com.example.entity.StudentEntity">
  SELECT * FROM test_student
  <where>
   <if test="age != null and age != '' and compare != null and compare != ''">
    age
    ${compare}
    #{age}
   </if>
   <if test="name != null and name != ''">
    AND name LIKE "%"#{name}"%"
   </if>
   <if test="address != null and address != ''">
    AND address LIKE "%"#{address}"%"
   </if>
  </where>
  ORDER BY id
 </select>

查詢結(jié)果:

3.使用sql中的字符串拼接函數(shù)

<select id="searchStudents" resultType="com.example.entity.StudentEntity"
  parameterType="com.example.entity.StudentEntity">
  SELECT * FROM test_student
  <where>
   <if test="age != null and age != '' and compare != null and compare != ''">
    age
    ${compare}
    #{age}
   </if>
   <if test="name != null and name != ''">
    AND name LIKE CONCAT(CONCAT('%',#{name},'%'))
   </if>
   <if test="address != null and address != ''">
    AND address LIKE CONCAT(CONCAT('%',#{address},'%'))
   </if>
  </where>
  ORDER BY id
 </select>

查詢結(jié)果:

4.使用標(biāo)簽

<select id="searchStudents" resultType="com.example.entity.StudentEntity"
  parameterType="com.example.entity.StudentEntity">
  <bind name="pattern1" value="'%' + _parameter.name + '%'" />
  <bind name="pattern2" value="'%' + _parameter.address + '%'" />
  SELECT * FROM test_student
  <where>
   <if test="age != null and age != '' and compare != null and compare != ''">
    age
    ${compare}
    #{age}
   </if>
   <if test="name != null and name != ''">
    AND name LIKE #{pattern1}
   </if>
   <if test="address != null and address != ''">
    AND address LIKE #{pattern2}
   </if>
  </where>
  ORDER BY id
 </select>

查詢結(jié)果:

5.在Java代碼中拼接字符串

public static void main(String[] args) {
  try {
   int count = 500;

   long begin = System.currentTimeMillis();
   testString(count);
   long end = System.currentTimeMillis();
   long time = end - begin;
   System.out.println("String 方法拼接"+count+"次消耗時(shí)間:" + time + "毫秒");

   begin = System.currentTimeMillis();
   testStringBuilder(count);
   end = System.currentTimeMillis();
   time = end - begin;
   System.out.println("StringBuilder 方法拼接"+count+"次消耗時(shí)間:" + time + "毫秒");

  } catch (Exception e) {
   e.printStackTrace();
  }

 }

 private static String testString(int count) {
  String result = "";

  for (int i = 0; i < count; i++) {
   result += "hello ";
  }

  return result;
 }

 private static String testStringBuilder(int count) {
  StringBuilder sb = new StringBuilder();

  for (int i = 0; i < count; i++) {
   sb.append("hello");
  }

  return sb.toString();
 }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java編程實(shí)現(xiàn)二項(xiàng)分布的采樣或抽樣實(shí)例代碼

    Java編程實(shí)現(xiàn)二項(xiàng)分布的采樣或抽樣實(shí)例代碼

    這篇文章主要介紹了Java編程實(shí)現(xiàn)二項(xiàng)分布的采樣或抽樣實(shí)例代碼,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • Java分布式session存儲(chǔ)解決方案圖解

    Java分布式session存儲(chǔ)解決方案圖解

    這篇文章主要介紹了Java分布式session存儲(chǔ)解決方案圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • IDEA設(shè)置字體隨鼠標(biāo)滾動(dòng)放大縮小的實(shí)現(xiàn)

    IDEA設(shè)置字體隨鼠標(biāo)滾動(dòng)放大縮小的實(shí)現(xiàn)

    這篇文章主要介紹了IDEA設(shè)置字體隨鼠標(biāo)滾動(dòng)放大縮小的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • IDEA個(gè)性化設(shè)置注釋模板詳細(xì)講解版

    IDEA個(gè)性化設(shè)置注釋模板詳細(xì)講解版

    IDEA自帶的注釋模板不是太好用,我本人到網(wǎng)上搜集了很多資料系統(tǒng)的整理了一下制作了一份比較完整的模板來(lái)分享給大家,下面這篇文章主要給大家介紹了IDEA個(gè)性化設(shè)置注釋模板的相關(guān)資料,需要的朋友可以參考下
    2024-01-01
  • Netty 輕松實(shí)現(xiàn)文件上傳功能

    Netty 輕松實(shí)現(xiàn)文件上傳功能

    今天我們來(lái)完成一個(gè)使用netty進(jìn)行文件傳輸?shù)娜蝿?wù)。在實(shí)際項(xiàng)目中,文件傳輸通常采用FTP或者HTTP附件的方式,對(duì)Netty 文件上傳功能感興趣的朋友一起看看吧
    2021-07-07
  • Eclipse+Maven構(gòu)建Hadoop項(xiàng)目的方法步驟

    Eclipse+Maven構(gòu)建Hadoop項(xiàng)目的方法步驟

    這篇文章主要介紹了Eclipse+Maven構(gòu)建Hadoop項(xiàng)目的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • Java利用Socket和IO流實(shí)現(xiàn)文件的上傳與下載

    Java利用Socket和IO流實(shí)現(xiàn)文件的上傳與下載

    本文主要介紹了Java利用Socket和IO流實(shí)現(xiàn)文件的上傳與下載,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • java判定數(shù)組或集合是否存在某個(gè)元素的實(shí)例

    java判定數(shù)組或集合是否存在某個(gè)元素的實(shí)例

    下面小編就為大家?guī)?lái)一篇java判定數(shù)組或集合是否存在某個(gè)元素的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-01-01
  • Java全面細(xì)致講解==和equals的使用

    Java全面細(xì)致講解==和equals的使用

    這篇文章主要介紹了Java中==和equals()的區(qū)別,,==可以使用在基本數(shù)據(jù)類型變量和引用數(shù)據(jù)類型變量中,equals()是方法,只能用于引用數(shù)據(jù)類型,需要的朋友可以參考下
    2022-05-05
  • Java?mybatis?開發(fā)自定義插件

    Java?mybatis?開發(fā)自定義插件

    這篇文章主要介紹了Java?mybatis開發(fā)自定義插件,MyBatis允許你在映射語(yǔ)句執(zhí)行過(guò)程中的某一點(diǎn)進(jìn)行攔截調(diào)用,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08

最新評(píng)論