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


猜你喜欢
- 完成一个简单的基于MVC的数据查询模块,要求能够按照name进行模糊查询。Index.jsp:<%@ page import=&quo
- 一.对象在JVM的内存结构JAVA内存管理由JVM来管理。1)堆,所有new出来的对象(包括成员变量)2)栈,所有局部变量(包括方法的参数)
- 本文实例讲述了Android开发实现圆形图片功能。分享给大家供大家参考,具体如下:**绝对布局:通过直接给定控件起始坐标 ( x , y )
- 一:JDBCTemplate简介Spring为各种持久化技术提供了简单操作的模板和回调API:ORM持久化技术模板类原生JDBCorg.sp
- 五十七、只针对异常情况才使用异常: 不知道你否则遇见过下面的代码: &
- AndroidStudio打包jar最近更新androidstudio之后发现打包jar不可用了。先看下以前的方法更新后新的用法//Copy
- IDEA SpringBoot项目配置热更新的步骤1.在pom.xml中添加依赖:<dependency><groupId
- 本文实例讲述了Android开发实现读取Assets下文件及文件写入存储卡的方法。分享给大家供大家参考,具体如下:调用一个反编译的.so文件
- springboot返回文件流@GetMapping(value = "/file/{fileName}")public
- 要“监听”事件,我们总是可以将“ * ”作为事件源中的另一个方法写入事件,但这将使事件源与 * 的逻辑紧密耦合。对于实际事件,我们比直接方法
- 本文以Java程序代码为例展示如何通过格式转换的方式将PPT幻灯片文档转为HTML文件。这里的PPT幻灯片可以是.ppt/.pptx/.pp
- 在C#中,当引用类型需要转换的时候,经常会用到关键字is、as以及显式强转。本篇来体验这三者的用法。先来梳理.NET引用类型转换的"
- 1、认识XML解析技术1.1、XML相关概念(1)DTD:XML语法规则,是XML文件的验证机制,可以通过比较XML文档和DTD文件看文档是
- 1. Android.bp 文件是什么?Android.bp 文件首先是 Android 系统的一种编译配置文件,是用来代替原来
- 在AndroidMenifest.xml中,常常会有下面的语句: <uses-sdk android:minSdkVersion=&q
- 1. 定时任务实现方式定时任务实现方式:Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerT
- 各个框架版本信息springboot: 2.1.3springcloud: Greenwich.RELEASEseata: 1.0.0sha
- 前言前段时间看到了豆瓣FM的音乐播放界面,有一个环形的进度条,非常的好看,于是想了想,为什么不自己做一个呢,于是就开始了自定义的过程豆瓣FM
- 什么是抽象类什么是抽象类呢?抽象类顾名思义就是很抽象,就是当我们没有足够的信息去描述这个类的时候我们就可以先不用描述,这样的类就是抽象类。用
- Kotlin中的面向对象面向对象面向对象的含义大家应该并不陌生,通过将事物抽象成对象,大大简化了程序的开发难度。我们常用的Java、Pyth