欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mysql中的concat()函數模糊查詢代替${}問題

 更新時間:2023年08月31日 09:38:22   作者:taiguolaotu  
這篇文章主要介紹了mysql中的concat()函數模糊查詢代替${}問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

mysql concat()函數 模糊查詢 代替${}

平常在寫代碼的時候,在寫模糊查詢的時候,一直都是使用 字段名 like ${}的方式,但是自從換了一家公司之后,發(fā)現分頁進行模糊查詢時使用了 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 &lt;= #{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)

心得

推薦使用第一和第三種方式

總結

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論