mybatis 模糊查询的几种方式

1.concat函数和#{}拼接的方式

select * from sys_user where student_name like concat('%',#{studentName},'%')

2.%和${}拼接的方式

select * from sys_user where student_name like '%${studentName}%'

3.concat函数和${}拼接的方式

select * from sys_user where student_name like concat('%','${studentName}','%')

4.||和#{}拼接的方式

select * from sys_user where student_name like '%'||#{studentName}||'%'

分析: (1)${}:表示拼接sql串,将接收到参数的内容不加任何修饰拼接在sql中,可能引发sql注入。 (2)#{ }是预编译处理,MyBatis在处理#{ }时,它会将sql中的#{ }替换为?,然后调用PreparedStatement的set方法来赋值,传入字符串后,会在值两边加上单引号,使用占位符的方式提高效率,可以防止sql注入。因此最好使用#{ }方式。 (3)concat函数最好不要使用,最好使用||,因为在Oracle中,concat()只能对两个字符串进行拼接(字符串多的话只能嵌套使用),而||可以对字符串无限拼接。
5. 使用band标签

<select id="selectUsersByName" resultType="User">
  select * from sys_user
  <where>
    <if test="name != null">
      <!-- 使用 bind 标签来创建一个可以在 SQL 中使用的模糊匹配变量 -->
      <bind name="pattern" value="'%' + name + '%'"/>
      and name like #{pattern}
    </if>
  </where>
</select>

相关推荐

  1. mybatis 模糊查询方式

    2024-05-13 03:08:09       11 阅读
  2. 【面试】MySQL查询方式

    2024-05-13 03:08:09       36 阅读
  3. MongoDB——模糊查询方法

    2024-05-13 03:08:09       38 阅读
  4. 单例模式实现方式

    2024-05-13 03:08:09       26 阅读
  5. 单例模式实现方式

    2024-05-13 03:08:09       19 阅读
  6. Spring中实现策略模式方式

    2024-05-13 03:08:09       11 阅读
  7. 单例模式实现方式

    2024-05-13 03:08:09       11 阅读
  8. 复习-详解查看Oracle用户权限方法

    2024-05-13 03:08:09       40 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-13 03:08:09       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-13 03:08:09       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-13 03:08:09       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-13 03:08:09       18 阅读

热门阅读

  1. Python 自动化脚本系列:第3集

    2024-05-13 03:08:09       13 阅读
  2. 拼接图片路径不显示:vue

    2024-05-13 03:08:09       10 阅读
  3. 力扣 516. 最长回文子序列 python AC

    2024-05-13 03:08:09       15 阅读
  4. 【linux软件基础知识】-cdev_alloc

    2024-05-13 03:08:09       11 阅读
  5. halcon学习之形状匹配

    2024-05-13 03:08:09       6 阅读
  6. logback 日志脱敏

    2024-05-13 03:08:09       7 阅读
  7. Google Gemma 2B 微调实战(IT科技新闻标题生成)

    2024-05-13 03:08:09       12 阅读
  8. 生成ssh来连接git

    2024-05-13 03:08:09       11 阅读
  9. 旋转木马案例

    2024-05-13 03:08:09       8 阅读
  10. HTTP 结构概述

    2024-05-13 03:08:09       11 阅读
  11. Android app通过jcifs-ng实现Samba连接共享文件夹

    2024-05-13 03:08:09       10 阅读
  12. MySQL教程-输入查询

    2024-05-13 03:08:09       10 阅读