mysql中的concat()函数模糊查询代替${}问题
作者:taiguolaotu
这篇文章主要介绍了mysql中的concat()函数模糊查询代替${}问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
mysql concat()函数 模糊查询 代替${}
平常在写代码的时候,在写模糊查询的时候,一直都是使用 字段名 like ${}的方式,但是自从换了一家公司之后,发现分页进行模糊查询时使用了 concat函数,
下面我将sql语句复制过来,给大家参考一下 ,希望可以帮助到大家。
 <select id="countDeviceByKeyword" resultType="java.lang.Integer">
  		select count(*) from zz_device where isDel='0'
  		and (Device_Name like concat('%',#{keyword},'%')
  		OR Device_Alias LIKE concat('%',#{keyword},'%'))
  </select>这里写的是sql语句,
下面把对应的dao层方法展现给大家
int countDeviceByKeyword(String keyword);
mysql模糊查询的三种方式
Mybatis常用模糊查询方法
- 直接使用 % 字符串拼接,如 ‘%’#{name}‘%’ 或 “%”#{name}“%”,单引号或双引号都可以。
 - 使用’%${name}%',会有SQL注入危险
 - 使用函数concat拼接concat(“%”,#{name},“%”)
 
1.使用concat(“%”,#{name},“%”)
UserMapper.xml文件:
 <select id="selectUsersByCondition" resultType="qin.com.entity.Users" parameterType="Users">
        select * from users
        <where>
            <if test="name != null and name != ''">
                <!--
                and name like concat(concat("%",#{name},"%"))
                ==>  Preparing: select * from users WHERE name like concat(concat("%",?,"%")) order by id desc
                -->
                and name like concat("%",#{name},"%")
            </if>
            <if test="sex != null and sex != ''">
                and sex = #{sex}
            </if>
            <if test="birthday != null and birthday != ''">
                and birthday = #{birthday}
            </if>
            <if test="createTime != null and createTime != ''">
                and createTime>=#{createTime}
            </if>
            <if test="updateTime != null and updateTime != ''">
                and updateTime <= #{updateTime}
            </if>
        </where>
        order by id desc
    </select>Test测试代码
@Test
    public void testSelectUsersByCondition() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UsersService usersService =(UsersService) applicationContext.getBean("usersService");
        Users users = new Users();
        users.setName("小");
        List<Users> list = usersService.selectUsersByCondition(users);
        list.forEach(users1 -> System.out.println("根据条件查询用户信息:"+users1));
    }输出结果:
查询语句
==> Preparing: select * from users WHERE name like concat(“%”,?,“%”) order by id desc
==> Parameters: 小(String)

2.使用’%${name}%’
UserMapper.xml文件:
 <if test="name != null and name != ''">
	   <!--and name like '%${name}%'-->
	   and name like '%${name}%'
 </if>输出结果:
==> Preparing: select * from users WHERE name like ‘%小%’ order by id desc
==> Parameters:

3.使用"%“#{name}”%"
<if test="name != null and name != ''">
   and name like "%"#{name}"%"
</if>输出语句:
==> Preparing: select * from users WHERE name like “%”?“%” order by id desc
==> Parameters: 小(String)

心得
推荐使用第一和第三种方式
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
- MySQL基于group_concat()函数合并多行数据
 - MySQL中组合字段之concat()
 - Mybatis mysql模糊查询方式(CONCAT多个字段)及bug
 - MySQL中CONCAT和GROUP_CONCAT方法的区别详解
 - mysql 模糊查询 concat()的用法详解
 - Mysql中的concat函数(拼接函数)详解
 - MySQL group_concat函数使用方法详解
 - mysql中GROUP_CONCAT函数使用及遇到问题详解
 - mysql中GROUP_CONCAT函数使用技巧及问题详解
 - MySQL中的CONCAT()函数:轻松拼接字符串的利器
 - MySQL中CONCAT()函数出现值为空的问题及解决办法
 
