Mybatis 实现一个搜索框对多个字段进行模糊查询
时间:2021-06-15 09:15:33|栏目:JAVA代码|点击: 次
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两个字段的关键字模糊查询。
先看下效果图:
关键字搜索之前的列表数据

关键字搜索之后的数据

实现核心代码:
<select id="list" 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<=#{edate}
</if>
limit #{m},#{n}
</select>
原理分析:
这里其实就是在where条件后面将id和username通过concat()函数连接了起来,然后在对关键字进行模糊查询,就能得到自己想要的结果了。So easy!
上一篇:Java常用内置注解用法分析
栏 目:JAVA代码
本文标题:Mybatis 实现一个搜索框对多个字段进行模糊查询
本文地址:http://www.codeinn.net/misctech/142350.html


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




