mybatis concat 模糊查询方法

 

使用concat 模糊查询

<select id="queryByKeyWord" resultType="com.mingdutech.xelerator.eln.dto.ApparatusDto">
      select distinct b.*
      from base_apparatus b
      left join base_apparatus_column c
      on b.ID = c.apparatus_id
      <if test="keyWord != null">
          <where>
              CONCAT( IFNULL(b.name,''),
              IFNULL(code,''),
              IFNULL(serial,''),
              IFNULL(location,''),
              IFNULL(remark,''),
              IFNULL(b.create_user,''),
              IFNULL(b.update_user,''),
              IFNULL(c.field_value,''),
              IFNULL(batch_no,'') )
              like CONCAT('%',#{keyWord},'%')
          </where>
      </if>
  </select>

concat() 函数用于将多个字符串拼接成一个字符串

本举例

keyWord为空查询所有,keyWord不为空,检索所有contact中所有字段匹配的内容。

注意

所有字段需要IFNULL判断,否则某字段为空可能会导致查询结果丢失一条记录

 

模糊查询使用concat('%',#{str},'%')出错

经过我一套乱七八糟毫无思路地查找后,发现不是mybatis的原因,原来是SQL server不支持concat函数,直接用加号连接就好

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程宝库

mybatis like模糊查询特殊字符报错转义处理方式:方案1    <if test="projectName!=null and projectName!=''">  &n ...