Mybatis 實(shí)現(xiàn)一個(gè)搜索框?qū)Χ鄠€(gè)字段進(jìn)行模糊查詢
最近項(xiàng)目需要提供一個(gè)搜索框?qū)Χ鄠€(gè)字段進(jìn)行模糊查詢的操作代替下拉列表選擇單個(gè)字段條件進(jìn)行模糊查詢的操作。
2、解決辦法:之前的四個(gè)條件的模糊查詢代碼
<if test='featureCode != null'> AND plm_model_option.feature_code= #{featureCode} </if> <if test='featureName != null'> AND plm_feature_lib.feature_name= #{featureName} </if> <if test='optionCode != null'> AND plm_model_option.option_code= #{optionCode} </if> <if test='optionName != null'> AND plm_option_lib.option_name= #{optionName} </if>
現(xiàn)在進(jìn)行模糊查詢的代碼:
<if test='searchStr!=null and searchStr!=’’'> AND CONCAT(plm_model_option.feature_code,plm_feature_lib.feature_name,plm_model_option.option_code,plm_option_lib.option_name) LIKE CONCAT (’%’, #{searchStr},’%’)</if>
補(bǔ)充:最新Mybatis關(guān)鍵字模糊查詢結(jié)果檢索多個(gè)字段解決方案
Mybatis用戶名模糊查詢,賬號(hào)模糊查詢我相信大家都會(huì)。那么如何輸入關(guān)鍵字之后既可以查詢到用戶名的結(jié)果又可以查詢到賬號(hào)的結(jié)果呢?
我這里設(shè)定的是id和username兩個(gè)字段的關(guān)鍵字模糊查詢。
先看下效果圖:關(guān)鍵字搜索之前的列表數(shù)據(jù)
關(guān)鍵字搜索之后的數(shù)據(jù)
<select resultType='com.swkj.pojo.Member'> SELECT * FROM tb_member WHERE 1=1 <if test='keyword!=’’ and keyword!=null'> <!--bind 標(biāo)簽的兩個(gè)屬性都是必選項(xiàng), name 為綁定到上下文的變量名,value為OGNL表達(dá)式。--> <bind name='pattern' value='’%’ + keyword + ’%’'/> and CONCAT(username,id) like #{pattern} </if> <if test='sdate!=’’ and sdate!=null'> and starttime>=#{sdate} </if> <if test='edate!=’’ and edate!=null'> and starttime<=#{edate} </if> limit #{m},#{n} </select>原理分析:
這里其實(shí)就是在where條件后面將id和username通過(guò)concat()函數(shù)連接了起來(lái),然后在對(duì)關(guān)鍵字進(jìn)行模糊查詢,就能得到自己想要的結(jié)果了。So easy!
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. sQlite常用語(yǔ)句以及sQlite developer的使用與注冊(cè)2. SQL server 危險(xiǎn)存儲(chǔ)過(guò)程刪除與恢復(fù)3. VS自帶的SQL server修改密碼并連接使用4. sql2000下 分頁(yè)存儲(chǔ)過(guò)程5. 為SQLite3提供一個(gè)ANSI到UTF8的互轉(zhuǎn)函數(shù)6. MySQL8.0無(wú)法啟動(dòng)3534的解決方法7. Access 模糊參數(shù) 分頁(yè)查詢8. 傳 Oracle 欲 收購(gòu)金蝶?9. SQL SERVER偏移函數(shù)(LAG、LEAD、FIRST_VALUE、LAST _VALUE、NTH_VALUE)10. MySQL Truncate用法詳解
