Mybatis如何解決sql中l(wèi)ike通配符模糊匹配問題
更新時間:2022年01月14日 14:47:00 作者:時間辜負(fù)了誰
這篇文章主要介紹了Mybatis如何解決sql中l(wèi)ike通配符模糊匹配問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
sql中l(wèi)ike通配符模糊匹配問題
針對oracle數(shù)據(jù)庫:
將查詢條件通過功能類處理
/**
?? ? * Description: 處理轉(zhuǎn)義字符%和_,針對ORACLE數(shù)據(jù)庫
?? ? *?
?? ? * @param str
?? ? * @return
?? ? */
?? ?public static String escapeStr(String str) {
?? ??? ?String temp = "";
?? ??? ?for (int i = 0; i < str.length(); i++) {
?? ??? ??? ?if (str.charAt(i) == '%' || str.charAt(i) == '_') {
?? ??? ??? ??? ?temp += "\\" + str.charAt(i);
?? ??? ??? ?} else {
?? ??? ??? ??? ?temp += str.charAt(i);
?? ??? ??? ?}
?? ??? ?}
?? ??? ?return temp;
?? ?}后臺Contronller獲得查詢條件
并調(diào)用工具類處理
String areaname = request.getParameter("Areaname");
?? ?if (areaname != null) {
?? ??? ?if ("".equals(areaname)) {
?? ??? ??? ?areaname = null;
?? ??? ?} else {
?? ??? ??? ?areaname = StringUtils.escapeStr(areaname);
?? ? ? ? ? ?}
? ? ? ? }mapper.xml中對應(yīng)的使用方法
<if test="param.areaname!=null"> and areaname like '%'||#{param.areaname}||'%' escape '\'</if>使用like實(shí)現(xiàn)模糊匹配
方式一
select * from t_user where name like ' %${value}% '方式二
select * from t_user where name like '%'||${value}||'%'方式三
select * from t_user where name like #{do_it_in_java}以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot中發(fā)送QQ郵件功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了SpringBoot中發(fā)送QQ郵件功能的實(shí)現(xiàn)代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-02-02
運(yùn)用springboot搭建并部署web項(xiàng)目的示例
這篇文章主要介紹了運(yùn)用springboot搭建并部署web項(xiàng)目的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06
Eclipse中Debug時鼠標(biāo)懸停不能查看變量值解決辦法
這篇文章主要介紹了Eclipse中Debug時鼠標(biāo)懸停不能查看變量值解決辦法,以及分享了一個簡單補(bǔ)全代碼的方法,還是比較不錯的,需要的朋友可以參考下。2017-11-11

