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

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

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

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

數(shù)據(jù)庫表名為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注入,所以這種方式雖然簡單但是不推薦使用!??!

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();
 }

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

相關(guān)文章

  • scala 操作數(shù)據(jù)庫的方法

    scala 操作數(shù)據(jù)庫的方法

    這篇文章主要介紹了scala 操作數(shù)據(jù)庫的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • Java設(shè)計(jì)模式之建造者模式的示例詳解

    Java設(shè)計(jì)模式之建造者模式的示例詳解

    建造者模式,是一種對(duì)象構(gòu)建模式 它可以將復(fù)雜對(duì)象的建造過程抽象出來,使這個(gè)抽象過程的不同實(shí)現(xiàn)方法可以構(gòu)造出不同表現(xiàn)的對(duì)象。本文將通過示例講解建造者模式,需要的可以參考一下
    2022-02-02
  • Java實(shí)現(xiàn)HTTP請(qǐng)求的4種方式總結(jié)

    Java實(shí)現(xiàn)HTTP請(qǐng)求的4種方式總結(jié)

    這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)HTTP請(qǐng)求的4種方式,在java開發(fā)中,經(jīng)常遇到需要調(diào)用第三方提供的接口服務(wù)的需求,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下
    2023-08-08
  • 全面了解java基本類型和封裝類型的區(qū)別及應(yīng)用

    全面了解java基本類型和封裝類型的區(qū)別及應(yīng)用

    下面小編就為大家?guī)硪黄媪私鈐ava基本類型和封裝類型的區(qū)別及應(yīng)用。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-09-09
  • java虛擬機(jī)是做什么用的

    java虛擬機(jī)是做什么用的

    在本篇文章里小編給大家整理的是一篇關(guān)于java虛擬機(jī)作用等相關(guān)內(nèi)容,對(duì)此有興趣的朋友們可以學(xué)習(xí)參考下。
    2021-01-01
  • 最長公共子序列問題的深度分析與Java實(shí)現(xiàn)方式

    最長公共子序列問題的深度分析與Java實(shí)現(xiàn)方式

    本文詳細(xì)介紹了最長公共子序列(LCS)問題,包括其概念、暴力解法、動(dòng)態(tài)規(guī)劃解法,并提供了Java代碼實(shí)現(xiàn),暴力解法雖然簡單,但在大數(shù)據(jù)處理中效率較低,動(dòng)態(tài)規(guī)劃解法通過構(gòu)建DP表,顯著提高了計(jì)算效率,適用于大規(guī)模數(shù)據(jù)處理
    2025-02-02
  • 詳解如何獲取PreparedStatement參數(shù)示例詳解

    詳解如何獲取PreparedStatement參數(shù)示例詳解

    這篇文章主要為大家介紹了詳解如何獲取PreparedStatement參數(shù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • Java后臺(tái)接收數(shù)據(jù)的三種方式(url、form-data與application/json)

    Java后臺(tái)接收數(shù)據(jù)的三種方式(url、form-data與application/json)

    本文主要介紹了Java后臺(tái)接收數(shù)據(jù)的三種方式(url、form-data與application/json),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • spring Mvc配置xml使ResponseBody返回Json的方法示例

    spring Mvc配置xml使ResponseBody返回Json的方法示例

    這篇文章主要給大家介紹了關(guān)于spring Mvc配置xml使ResponseBody返回Json的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-04-04
  • Java編程之雙重循環(huán)打印圖形

    Java編程之雙重循環(huán)打印圖形

    這篇文章主要介紹了Java編程之雙重循環(huán)打印圖形,屬于Java編程基礎(chǔ)練習(xí)部分,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11

最新評(píng)論

乌拉特前旗| 瑞金市| 霍州市| 清新县| 绿春县| 德钦县| 清水河县| 辽阳县| 宣化县| 根河市| 铅山县| 枣强县| 洱源县| 兴文县| 新蔡县| 瑞金市| 双江| 武威市| 平安县| 兰州市| 武平县| 安福县| 琼结县| 鸡东县| 巫山县| 肥东县| 保山市| 平泉县| 宁蒗| 东明县| 阿图什市| 郧西县| 博湖县| 蒲江县| 饶阳县| 威海市| 客服| 长沙县| 宝鸡市| 台中市| 辛集市|