MyBatis實(shí)現(xiàn)模糊查詢的幾種方式
在學(xué)習(xí)MyBatis過程中想實(shí)現(xiàn)模糊查詢,可惜失敗了。后來上百度上查了一下,算是解決了。記錄一下MyBatis實(shí)現(xiàn)模糊查詢的幾種方式。
數(shù)據(jù)庫(kù)表名為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注入,所以這種方式雖然簡(jiǎn)單但是不推薦使用!?。?/em>
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();
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
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)用
下面小編就為大家?guī)硪黄媪私鈐ava基本類型和封裝類型的區(qū)別及應(yīng)用。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09
最長(zhǎng)公共子序列問題的深度分析與Java實(shí)現(xiàn)方式
本文詳細(xì)介紹了最長(zhǎng)公共子序列(LCS)問題,包括其概念、暴力解法、動(dòng)態(tài)規(guī)劃解法,并提供了Java代碼實(shí)現(xiàn),暴力解法雖然簡(jiǎn)單,但在大數(shù)據(jù)處理中效率較低,動(dòng)態(tài)規(guī)劃解法通過構(gòu)建DP表,顯著提高了計(jì)算效率,適用于大規(guī)模數(shù)據(jù)處理2025-02-02
詳解如何獲取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),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
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

