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通過concat()函數(shù)連接了起來,然后在對(duì)關(guān)鍵字進(jìn)行模糊查詢,就能得到自己想要的結(jié)果了。So easy!
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. SQLSERVER調(diào)用C#的代碼實(shí)現(xiàn)2. 如何在SQL Server 2005中為安裝程序增加計(jì)數(shù)器注冊(cè)表項(xiàng)值3. DB2的高可用性和災(zāi)難恢復(fù)概述4. MySQL/MariaDB 如何實(shí)現(xiàn)數(shù)據(jù)透視表的示例代碼5. sql server 災(zāi)難恢復(fù)6. mysql5.7 設(shè)置遠(yuǎn)程訪問的實(shí)現(xiàn)7. 如何修改MySQL字符集8. MSSQL跨服務(wù)器連接的幾種方法9. 講解MaxDB數(shù)據(jù)庫(kù)和MySQL數(shù)據(jù)庫(kù)的主要差別10. 詳解Centos 使用YUM安裝MariaDB
