mysql中的concat()函數模糊查詢代替${}問題
mysql concat()函數 模糊查詢 代替${}
平常在寫代碼的時候,在寫模糊查詢的時候,一直都是使用 字段名 like ${}的方式,但是自從換了一家公司之后,發(fā)現分頁進行模糊查詢時使用了 concat函數,
下面我將sql語句復制過來,給大家參考一下 ,希望可以幫助到大家。
<select id="countDeviceByKeyword" resultType="java.lang.Integer"> select count(*) from zz_device where isDel='0' and (Device_Name like concat('%',#{keyword},'%') OR Device_Alias LIKE concat('%',#{keyword},'%')) </select>
這里寫的是sql語句,
下面把對應的dao層方法展現給大家
int countDeviceByKeyword(String keyword);
mysql模糊查詢的三種方式
Mybatis常用模糊查詢方法
- 直接使用 % 字符串拼接,如 ‘%’#{name}‘%’ 或 “%”#{name}“%”,單引號或雙引號都可以。
- 使用’%${name}%',會有SQL注入危險
- 使用函數concat拼接concat(“%”,#{name},“%”)
1.使用concat(“%”,#{name},“%”)
UserMapper.xml文件:
<select id="selectUsersByCondition" resultType="qin.com.entity.Users" parameterType="Users"> select * from users <where> <if test="name != null and name != ''"> <!-- and name like concat(concat("%",#{name},"%")) ==> Preparing: select * from users WHERE name like concat(concat("%",?,"%")) order by id desc --> and name like concat("%",#{name},"%") </if> <if test="sex != null and sex != ''"> and sex = #{sex} </if> <if test="birthday != null and birthday != ''"> and birthday = #{birthday} </if> <if test="createTime != null and createTime != ''"> and createTime>=#{createTime} </if> <if test="updateTime != null and updateTime != ''"> and updateTime <= #{updateTime} </if> </where> order by id desc </select>
Test測試代碼
@Test public void testSelectUsersByCondition() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); UsersService usersService =(UsersService) applicationContext.getBean("usersService"); Users users = new Users(); users.setName("小"); List<Users> list = usersService.selectUsersByCondition(users); list.forEach(users1 -> System.out.println("根據條件查詢用戶信息:"+users1)); }
輸出結果:
查詢語句
==> Preparing: select * from users WHERE name like concat(“%”,?,“%”) order by id desc
==> Parameters: 小(String)
2.使用’%${name}%’
UserMapper.xml文件:
<if test="name != null and name != ''"> <!--and name like '%${name}%'--> and name like '%${name}%' </if>
輸出結果:
==> Preparing: select * from users WHERE name like ‘%小%’ order by id desc
==> Parameters:
3.使用"%“#{name}”%"
<if test="name != null and name != ''"> and name like "%"#{name}"%" </if>
輸出語句:
==> Preparing: select * from users WHERE name like “%”?“%” order by id desc
==> Parameters: 小(String)
心得
推薦使用第一和第三種方式
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- MySQL基于group_concat()函數合并多行數據
- MySQL中組合字段之concat()
- Mybatis?mysql模糊查詢方式(CONCAT多個字段)及bug
- MySQL中CONCAT和GROUP_CONCAT方法的區(qū)別詳解
- mysql 模糊查詢 concat()的用法詳解
- Mysql中的concat函數(拼接函數)詳解
- MySQL group_concat函數使用方法詳解
- mysql中GROUP_CONCAT函數使用及遇到問題詳解
- mysql中GROUP_CONCAT函數使用技巧及問題詳解
- MySQL中的CONCAT()函數:輕松拼接字符串的利器
- MySQL中CONCAT()函數出現值為空的問題及解決辦法
相關文章
mysql之key和index的區(qū)別及創(chuàng)建刪除索引方式
這篇文章主要介紹了mysql之key和index的區(qū)別及創(chuàng)建刪除索引方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12mysql error 1071: 創(chuàng)建唯一索引時字段長度限制的問題
這篇文章主要介紹了mysql error 1071: 創(chuàng)建唯一索引時字段長度限制的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09mysql存儲過程 在動態(tài)SQL內獲取返回值的方法詳解
本篇文章是對mysql存儲過程在動態(tài)SQL內獲取返回值進行了詳細的分析介紹,需要的朋友參考下2013-06-06坑人的Mysql5.7問題(默認不支持Group By語句)
這篇文章主要介紹了坑人的Mysql5.7問題(默認不支持Group By語句),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10解析sql語句中l(wèi)eft_join、inner_join中的on與where的區(qū)別
以下是對在sql語句中l(wèi)eft_join、inner_join中的on與where的區(qū)別進行了詳細的分析介紹,需要的朋友可以參考下2013-07-07