您的位置:首頁技術文章
文章詳情頁

Mybatis 實現一個搜索框對多個字段進行模糊查詢

瀏覽:25日期:2023-10-20 10:53:21
1、問題描述:

最近項目需要提供一個搜索框對多個字段進行模糊查詢的操作代替下拉列表選擇單個字段條件進行模糊查詢的操作。

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>

現在進行模糊查詢的代碼:

<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關鍵字模糊查詢結果檢索多個字段解決方案

Mybatis用戶名模糊查詢,賬號模糊查詢我相信大家都會。那么如何輸入關鍵字之后既可以查詢到用戶名的結果又可以查詢到賬號的結果呢?

我這里設定的是id和username兩個字段的關鍵字模糊查詢。

先看下效果圖:

關鍵字搜索之前的列表數據

Mybatis 實現一個搜索框對多個字段進行模糊查詢

關鍵字搜索之后的數據

Mybatis 實現一個搜索框對多個字段進行模糊查詢

實現核心代碼:

<select resultType='com.swkj.pojo.Member'> SELECT * FROM tb_member WHERE 1=1 <if test='keyword!=’’ and keyword!=null'> <!--bind 標簽的兩個屬性都是必選項, name 為綁定到上下文的變量名,value為OGNL表達式。--> <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&lt;=#{edate} </if> limit #{m},#{n} </select>原理分析:

這里其實就是在where條件后面將id和username通過concat()函數連接了起來,然后在對關鍵字進行模糊查詢,就能得到自己想要的結果了。So easy!

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。如有錯誤或未考慮完全的地方,望不吝賜教。

標簽: Mybatis 數據庫
相關文章:
国产综合久久一区二区三区