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

MyBatis實現模糊查詢的幾種方式

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

在學習MyBatis過程中想實現模糊查詢,可惜失敗了。后來上百度上查了一下,算是解決了。記錄一下MyBatis實現模糊查詢的幾種方式。

數據庫表名為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>

寫完后自我感覺良好,很開心的就去跑程序了,結果當然是報錯了:

經百度得知,這么寫經MyBatis轉換后(‘%#{name}%')會變?yōu)?‘%?%'),而(‘%?%')會被看作是一個字符串,所以Java代碼在執(zhí)行找不到用于匹配參數的 ‘?' ,然后就報錯了。

解決方法

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>

查詢結果如下圖:

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

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>

查詢結果:

3.使用sql中的字符串拼接函數

<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>

查詢結果:

4.使用標簽

<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>

查詢結果:

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+"次消耗時間:" + time + "毫秒");

   begin = System.currentTimeMillis();
   testStringBuilder(count);
   end = System.currentTimeMillis();
   time = end - begin;
   System.out.println("StringBuilder 方法拼接"+count+"次消耗時間:" + 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();
 }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Java編程實現二項分布的采樣或抽樣實例代碼

    Java編程實現二項分布的采樣或抽樣實例代碼

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

    Java分布式session存儲解決方案圖解

    這篇文章主要介紹了Java分布式session存儲解決方案圖解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • IDEA設置字體隨鼠標滾動放大縮小的實現

    IDEA設置字體隨鼠標滾動放大縮小的實現

    這篇文章主要介紹了IDEA設置字體隨鼠標滾動放大縮小的實現方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • IDEA個性化設置注釋模板詳細講解版

    IDEA個性化設置注釋模板詳細講解版

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

    Netty 輕松實現文件上傳功能

    今天我們來完成一個使用netty進行文件傳輸的任務。在實際項目中,文件傳輸通常采用FTP或者HTTP附件的方式,對Netty 文件上傳功能感興趣的朋友一起看看吧
    2021-07-07
  • Eclipse+Maven構建Hadoop項目的方法步驟

    Eclipse+Maven構建Hadoop項目的方法步驟

    這篇文章主要介紹了Eclipse+Maven構建Hadoop項目的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-02-02
  • Java利用Socket和IO流實現文件的上傳與下載

    Java利用Socket和IO流實現文件的上傳與下載

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

    java判定數組或集合是否存在某個元素的實例

    下面小編就為大家?guī)硪黄猨ava判定數組或集合是否存在某個元素的實例。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • Java全面細致講解==和equals的使用

    Java全面細致講解==和equals的使用

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

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

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

最新評論