MyBatis 动态拼接Sql字符串的问题
作者:mrr 发布时间:2021-08-09 05:23:46
标签:mybatis,动态,拼接,sql
MyBatis 的一个强大的特性之一通常是它的动态 SQL 能力。如果你有使用 JDBC 或其他 相似框架的经验,你就明白条件地串联 SQL 字符串在一起是多么的痛苦,确保不能忘了空格或在列表的最后省略逗号。动态 SQL 可以彻底处理这种痛苦。
动态SQL
MyBatis的动态SQL,解决了SQL字符串拼接的痛苦。
1.if
<select id="findActiveBlogWithTitleLike"
parameterType="Blog" resultType="Blog">
SELECT * FROM BLOG
WHERE state = 'ACTIVE'
<if test="title != null">
AND title like #{title}
</if>
</select>
这条一句会提供一个可选的文本查找功能。如果没有传递title,那么所有激活的博客都会被返回。
如果传递了title,那么就会查找相近的title。
2.choose,when,otherwise
<select id="findActiveBlogLike"
parameterType="BLOG" resultType="BLOG">
SELECT * FROM BLOG
WHERE
<choose>
<when test="title != null">
AND title like #{title}
</when>
<when test="author != null and author.name != null">
AND title like #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</select>
注:如果上述条件都没有匹配,则会变成SELECT * FROM BLOG WHERE
如果仅有第二个匹配,则会变成SELECT * FROM BLOG WHERE AND title LIKE somelike
显然这样会查询失败。要解决这个问题,mybatis提供了解决方法。
<select id="findActiveBlogLike"
parameterType="BLOG" resultType="BLOG">
SELECT * FROM BLOG
WHERE
<trim prefix="WHERE" prefixOverrides="AND |OR ">
<choose>
<when test="title != null">
AND title like #{title}
</when>
<when test="author != null and author.name != null">
AND title like #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</trim>
</select>
overrides属性采用管道文本分隔符来覆盖,这里的空白是重要的。它的结果就是移除在InnerText中overrides中指定的内容。
3.set
<update id="updateAuthorIfNecessary"
parameterType="Author">
update Author
<set>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
<if test="email != null">email=#{email}</if>
</set>
where id=#{id}
</update>
同上的问题,优化后:
<update id="updateAuthorIfNecessary"
parameterType="Author">
update Author
<trim prefix="where" prefixOverrides=",">
<set>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
<if test="email != null">email=#{email}</if>
</set>
where id=#{id}
</trim>
</update>
以上所述是小编给大家介绍的MyBatis 动态拼接Sql字符串的问题网站的支持!
0
投稿
猜你喜欢
- 本文为大家分享了Android自定义Toast之WindowManager,供大家参考,具体内容如下Toast:WindowManager三
- Json是一种类似于XML的通用数据交换格式,具有比XML更高的传输效率. 从结构上看,所有的数据(data)最终都可以分解成三种类型: 第
- Java反射机制一、什么是反射机制 简单的来
- 1、此文初衷平常我们在进行微服务开发完毕后,单个微服务理应部署单个虚机上(docker也可),然后服务集中发布到服务注册中心上,但是有些小的
- 概念二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树:1、若它的左子树不为空,则左子树上所有节点的值都小于根结点的值。
- 前言:在实际的应用开发中,很多时候往往因为一些不可控的因素导致程序出现一些错误,这个时候就要及时把异常信息反馈给客户端,便于客户端能够及时地
- 前言最近想体验下最新版本的SpringBoot,逛了下官网,发现SpringBoot目前最新版本已经是2.6.4了,版本更新确实够快的。之前
- 本文实例讲述了C#使用doggleReport生成pdf报表的方法。分享给大家供大家参考,具体如下:1. 安装nuget-install p
- 上文我们讨论了一种最简单的线性结构——顺序表,这节我们要讨论另一种线性结构——链表。什么是链表了,不要求逻辑上相邻的数据元素在物理存储位置上
- (1)首先实现AutoCompleteTextView功能所需要的适配器数据源共有两种方法,一种结果是手工配置的,另一汇总是通过xml文件制
- 前言JDK 1.5 之前 synchronized 的性能是比较低的,但在 JDK 1.5 中,官方推出一个重量级功能 Lock,一举改变了
- 最近要做一个java web项目,因为页面不是很多,所以就没有前后端分离,前后端写在一起,这时候就用到thymeleaf了,以下是不动脑式的
- What内含一个或多个maven模块的SpringBoot项目称为SpringBoot多模块项目Why便于维护 将一个大的单体项目分成多个子
- AOP事务管理<aop:advisor>两种配置方式方式一@transactionManagerbean.xml<?xml
- 1 修改项目打包类型在pom.xml里,项目打包类型将jar设置成war:<packaging>war</packagin
- 这篇文章主要介绍了SpringBoot下如何实现支付宝接口的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值
- 以下实例演示了如何使用 retainAll () 方法来计算两个数组的交集:Main.java 文件:import java.util.Ar
- 在 C# 中反射技术应用广泛,至于什么是反射.........你如果不了解的话,请看下段说明,否则请跳过下段。广告一下:喜欢我文章的朋友请关
- 我们经常在项目中使用的线程池,但是是否关心过线程池的关闭呢,可能很多时候直接再项目中直接创建线程池让它一直运行当任务执行结束不在需要了也不去
- 在我们开发SpringBoot后端服务时,一般需要给前端统一响应格式,方便前端调试及配置错误提示等等。这篇文章讲讲实际工作中统一响应格式及统