Mybatis 實(shí)現(xiàn)一個搜索框?qū)Χ鄠€字段進(jìn)行模糊查詢
最近項目需要提供一個搜索框?qū)Χ鄠€字段進(jìn)行模糊查詢的操作代替下拉列表選擇單個字段條件進(jìn)行模糊查詢的操作。
2、解決辦法:之前的四個條件的模糊查詢代碼
<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é)果檢索多個字段解決方案
Mybatis用戶名模糊查詢,賬號模糊查詢我相信大家都會。那么如何輸入關(guān)鍵字之后既可以查詢到用戶名的結(jié)果又可以查詢到賬號的結(jié)果呢?
我這里設(shè)定的是id和username兩個字段的關(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)簽的兩個屬性都是必選項, 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通過concat()函數(shù)連接了起來,然后在對關(guān)鍵字進(jìn)行模糊查詢,就能得到自己想要的結(jié)果了。So easy!
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. MYSQL(電話號碼,身份證)數(shù)據(jù)脫敏的實(shí)現(xiàn)2. MySql導(dǎo)出后再導(dǎo)入數(shù)據(jù)時出錯問題3. Oracle的PDB數(shù)據(jù)庫創(chuàng)建DIRECTORY時遇到ORA-65254問題及解決方法4. MySQL存儲過程例子(包含事務(wù)、參數(shù)、嵌套調(diào)用、游標(biāo)循環(huán)等)5. Sql Server 2000數(shù)據(jù)庫日志日益龐大的解決方法6. MySQL decimal unsigned更新負(fù)數(shù)轉(zhuǎn)化為07. mysql-bin.000001文件的來源及處理方法8. 教你在AIX上安裝IBM DB2 9版本的分區(qū)環(huán)境9. 快速刪除ORACLE重復(fù)記錄10. Fluent Mybatis 批量更新的使用
