Mybatis 實現(xiàn)一個搜索框?qū)Χ鄠€字段進行模糊查詢
最近項目需要提供一個搜索框?qū)Χ鄠€字段進行模糊查詢的操作代替下拉列表選擇單個字段條件進行模糊查詢的操作。
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)在進行模糊查詢的代碼:
<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>
補充:最新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>原理分析:
這里其實就是在where條件后面將id和username通過concat()函數(shù)連接了起來,然后在對關(guān)鍵字進行模糊查詢,就能得到自己想要的結(jié)果了。So easy!
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. Mybatis 如何批量刪除數(shù)據(jù)的實現(xiàn)示例2. SQL?DCL數(shù)據(jù)控制語言的使用3. 幫你解決Oracle 9i和Tomcat的端口沖突4. django 將自帶的數(shù)據(jù)庫sqlite3改成mysql實例5. Microsoft Office Access取消主鍵的方法6. 給你的MyBatis-Plus裝上批量插入的翅膀(推薦)7. SQLite3中的日期時間函數(shù)使用小結(jié)8. MySQL MyISAM默認(rèn)存儲引擎實現(xiàn)原理9. Microsoft Office Access添加頁眉或頁腳的方法10. Mybatis批量修改時出現(xiàn)報錯問題解決方案
