Mybatis中where标签与if标签结合使用详细说明
作者:鳄鱼儿 发布时间:2021-07-27 08:15:53
标签:mybatis,where标签,if标签
前言
由于不小心将and
或者or
写在了语句后面,导致mybatis无法自主判别,这种问题在新上手的同学中很是常见。下面我们探讨一下,在哪些情况下Mybatis无法判断动态SQL语句中的and
或者or
。
使用<where>标签
select筛选出视图对象的参数,用于给前端返回页面参数使用。
<sql id="selectFileVo">
select file_id,
uuid,
file_name,
file_url,
status,
create_time,
update_time
from file
</sql>
以下代码格式是正确,我们先观察下and
或者or
的位置。
<select id="selectFileList" parameterType="File" resultMap="FileResult">
<include refid="selectFileVo"/>
<where>
<if test="fileName != null and fileName != ''">
and file_name like concat('%', #{fileName}, '%')
</if>
<if test="status != null and status != ''">
and status = #{status}
</if>
<if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''">
and create_time between #{params.beginCreateTime} and #{params.endCreateTime}
</if>
</where>
</select>
再看一下错误的写法;
<select id="selectFileList" parameterType="File" resultMap="FileResult">
<include refid="selectFileVo"/>
<where>
<if test="fileName != null and fileName != ''">
file_name like concat('%', #{fileName}, '%') and
</if>
<if test="status != null and status != ''">
status = #{status} and
</if>
<if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''">
create_time between #{params.beginCreateTime} and #{params.endCreateTime}
</if>
</where>
</select>
这时候运行该代码,当beginCreateTime
或endCreateTime
为空时,我们会发现报错SQL执行异常,原因是where多了一个and
。
总结
当<if>
标签判断失败后, <where>
标签关键字可以自动去除掉库表字段赋值前面的and
,不会去掉语句后面的and
关键字,即<where>
标签只会去掉<if>
标签语句中的最开始的and
关键字。所以上面的写法(and
写在后面)是不符合mybatis规范的。
不使用<where>标签
当不使用<where>
标签时,正确的写法可以参考以下代码:
<select id="selectFileList" parameterType="File" resultMap="FileResult">
<include refid="selectFileVo"/>
where 1=1
<if test="fileName != null and fileName != ''">
and file_name like concat('%', #{fileName}, '%')
</if>
<if test="status != null and status != ''">
and status = #{status}
</if>
<if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''">
and create_time between #{params.beginCreateTime} and #{params.endCreateTime}
</if>
</select>
此时我们发现and
是写在前面的,同时增加了1=1
条件。
如果我们去掉1=1
条件,同时去掉第一个<if>
标签的and
。
<select id="selectFileList" parameterType="File" resultMap="FileResult">
<include refid="selectFileVo"/>
where
<if test="fileName != null and fileName != ''">
file_name like concat('%', #{fileName}, '%')
</if>
<if test="status != null and status != ''">
and status = #{status}
</if>
<if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''">
and create_time between #{params.beginCreateTime} and #{params.endCreateTime}
</if>
</select>
这种情况下,当fileName
为空时,sql语句中会出现where and
这种错误的语法,最终导致sql执行异常。所以正确的代码中,使用1=1
条件,当fileName
为空时,sql语句就会变成where 1=1
,后面接不接and
都能正确执行。
在不使用<where>
标签的情况下,and
写在后面,在where
条件最后增加1=1
判断,原理和上面一样,这里就不再赘述了。
来源:https://blog.csdn.net/Ber_Bai/article/details/128091211
0
投稿
猜你喜欢
- 前言Set 表示由无重复对象组成的集合,也是集合框架中重要的一种集合类型,直接扩展自 Collection 接口。在一个 Set 中,不能有
- 区别一如果Mybatis Plus是扳手,那Mybatis Generator就是生产扳手的工厂。通俗来讲——MyBatis:一种操作数据库
- Java注解的Excel导出依赖: <dependency> &
- 前言Spring Data Jpa框架的目标是显著减少实现各种持久性存储的数据访问层所需的样板代码量。Spring Data Jpa存储库抽
- 使用filter设置要排除的URL@WebFilter(urlPatterns = "/*")@Order(value
- java调用Rsync并发迁移数据并执行校验java代码如下RsyncFile.javaimport lombok.NoArgsConstr
- 由于需要访问MongoDB,但是本地开发环境不能直接连接MongoDB,需要通过SecureCRT使用127.0.0.2本地IP代理。但是程
- 实现方案:我们直接参考实例代码:private String pattern = "((http|ftp
- 一. 递归1. 简介所谓的递归,其实是一种解决问题的方式。就是在解决具有既定规律的问题时,在方法内部调用方法自身的一种编程方式。 即方法在运
- 直接来,RequestDemo5代码,get请求和post请求都请求转发了,转发到RequestDemo6请求 RequestDe
- 如下所示:from jpype import *jvmPath = getDefaultJVMPath()jars = ["./F
- 前言Java8 由Oracle在2014年发布,是继Java5之后最具革命性的版本。Java8吸收其他语言的精髓带来了函数式编程,lambd
- 前言日志,在我们开发中是一个非常重要的话题,良好的日志打印可以帮助我们快速的定位问题,可能现在我们开发用到最多的日志框架就是slf4j了,但
- centos下搭建GitLab+Jenkins持续集成环境,供大家参考,具体内容如下1、安装JDKyum install -y java2、
- 1. 前言我们知道,在日常开发中使用的 HashMap 是线程不安全的,而线程安全类 HashTable 和 SynchronizedMap
- 使用场景:自己项目对接多个外部系统,各个外部系统使用的字段并没有统一,所以要根据不同系统动态的输出序列化数据,使适应各个系统的要求实现方式使
- 本文实例总结了Java编程实现生成给定范围内不重复随机数的方法。分享给大家供大家参考,具体如下:在Java中的Math类中存在一个rando
- 目录概述代码实现完整代码概述在PC端用.NET开发一个蓝牙下载的程序。实现在PC上查找周围的蓝牙设备(主要是手机),并将PC上的文件通过蓝牙
- 一、啥是Fluent-Mybatis与Mybatis-Plus类似,是对Mybaits进一步的封装,使之语法简洁明了,更重要的是不需要在自主
- 首先对于一个SpringBoot工程来说,最明显的标志的就是 @SpringBootApplication它标记了这是一个SpringBoo