国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

服務器之家 - 編程語言 - Java教程 - MyBatis實現模糊查詢的幾種方式

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

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

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

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

數據庫表名為test_student,初始化了幾條記錄,如圖:

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

起初我在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>

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

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

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

解決方法

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>

查詢結果如下圖:

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

注:使用${…}不能有效防止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>

查詢結果:

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

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

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

查詢結果:

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

4.使用標簽

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

查詢結果:

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

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

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

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

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 欧美精品欧美精品系列 | 国产日韩欧美一二三区 | 午夜av毛片| 日韩一区二区在线观看 | 黄色的视频免费看 | 欧美一级欧美三级在线观看 | 在线一二三区 | 亚洲欧美另类久久久精品2019 | 中文在线√天堂 | 日本三级中国三级99人妇网站 | 国产日韩一区二区 | 免费观看国产精品 | 黄色福利视频 | 国产精品久久久久久一区二区三区 | 精品久久久久久国产 | 欧美在线观看禁18 | 黄色tv在线观看 | 久久精品综合 | 久久精品国产免费 | 91精品麻豆日日躁夜夜躁 | 久久久久久久久99精品 | 国产精久久久 | 亚洲精品欧美 | 日日色视频| 国产 日韩 欧美 中文 在线播放 | 亚洲激情都市 | 羞羞网站免费 | 国产综合精品 | 日本视频免费高清一本18 | 欧美在线免费观看 | 91一区二区在线 | 久久久久久免费精品 | 寡妇激情毛片免费视频 | 日韩欧美一级精品久久 | 日韩精品一区二区三区四区五区 | 91中文字幕在线 | 北条麻妃在线一区二区三区 | 亚洲色图p| 久草热8精品视频在线观看 久久亚洲精品中文字幕 | 欧美激情在线精品一区二区三区 | 欧美黑人狂躁日本寡妇 |