欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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&lt;=#{edate}
 </if>
 limit #{m},#{n}
 </select>

原理分析:

这里其实就是在where条件后面将id和username通过concat()函数连接了起来,然后在对关键字进行模糊查询,就能得到自己想要的结果了。So easy!

上一篇:Java常用内置注解用法分析

栏    目:JAVA代码

下一篇:Mybatis Plus代码生成器(时间管理大师)

本文标题:Mybatis 实现一个搜索框对多个字段进行模糊查询

本文地址:http://www.codeinn.net/misctech/142350.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有