1、字符串的模糊匹配语法
第一种方式:使用 $ 符号编写
但是缺点就是 $ 符号无法防止sql注入问题
<if test="title != null and title != ''"> and title like "%${title}%"</if>
第二种方式:使用 # 好处就是防止sql注入
因为#{...}解析成sql语句时候,会在变量外侧自动加单引号' ',所以这里 % 需要使用双引号" ",不能使用单引号 ' ',不然会查不到任何结果。
<if test="title != null and title != ''"> and title like "%"#{title}"%"</if>
第三种方式:使用concat进行连接
<if test="title != null and title != ''"> and title like concat('%', #{title} ,'%')</if>
2、日期查询语法
第一种方式:使用 date_format 字符串格式化操作
between date_format(#{beginTime},'%Y-%m-%d 00:00:00') and date_format(#{endTime},'%Y-%m-%d 23:59:59')
第二种方式:使用MySQL 函数 concat 连接操作
between concat(#{beginTime},' ','00:00:00') and concat(#{endTime},' ','23:59:59')
mybatis常用查询语法集合
最新推荐文章于 2024-11-04 20:50:05 发布
本文介绍三种SQL模糊查询方法:使用$符号、#符号及concat函数,并对比其优劣;同时提供两种日期范围查询的方法,包括使用date_format进行格式化及concat进行连接。
833

被折叠的 条评论
为什么被折叠?



