在學習mybatis過程中想實現模糊查詢,可惜失敗了。后來上百度上查了一下,算是解決了。記錄一下mybatis實現模糊查詢的幾種方式。
數據庫表名為test_student,初始化了幾條記錄,如圖:
起初我在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轉換后(‘%#{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> |
查詢結果如下圖:
注:使用${…}不能有效防止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> |
查詢結果:
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> |
查詢結果:
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> |
查詢結果:
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