激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - MyBatis實現(xiàn)模糊查詢的幾種方式

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

2021-05-27 13:25行癲 Java教程

這篇文章主要介紹了MyBatis實現(xiàn)模糊查詢的幾種方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<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)然是報錯了:

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

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

解決方法

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<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é)果如下圖:

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<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é)果:

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<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é)果:

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

4.使用標(biāo)簽

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<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é)果:

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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();
 }

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

原文鏈接:https://blog.csdn.net/lonely_dog/article/details/74171314

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美一级黄色片免费观看 | av在线一区二区三区四区 | 色3344| 成人午夜精品久久久久久久蜜臀 | 黑人一区二区 | av黄色片网站 | 精品国产91久久久久久浪潮蜜月 | 久久99久久99精品 | 男男羞羞视频网站国产 | 亚洲一区二区三区91 | 国产午夜精品一区 | 欧美成人h版在线观看 | 欧美精品一区二区三区久久久 | 一级黄色在线观看 | 大学生一级毛片在线视频 | 亚洲日色 | 一级黄色淫片 | 能直接看av的网站 | 久久精品亚洲精品国产欧美kt∨ | 天天天干夜夜夜操 | 激情福利视频 | av7777777| 黄网站免费在线看 | 成人性生活视频 | 久久精品无码一区二区三区 | 久久久久久久久久久久免费 | 精品一区二区三区在线观看视频 | 91久久国产综合久久91猫猫 | 国产一级午夜 | av手机在线电影 | 91免费国产在线观看 | hdhdhd79xxxxх | 久久久婷婷一区二区三区不卡 | 国产精品一区二区手机在线观看 | 欧美视频首页 | 久久亚洲美女视频 | 九九热免费在线观看 | 茄子福利视频 | 日韩黄在线观看 | 成人免费淫片 | 欧美视频一二三区 |