Mybatis系列--14-动态sql之if,choose(when,otherwise),set,where,trim语法
2022/7/25 2:23:02
本文主要是介绍Mybatis系列--14-动态sql之if,choose(when,otherwise),set,where,trim语法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Mybatis系列--14-动态sql之if,choose(when,otherwise),set语法
概述
本篇接上篇博客, 继续学习动态sql中的if,choose,set等元素
if
if 条件判断大家都很熟悉,当某个条件成立,则执行什么动作,
下面的示例中给出当title
给出时,where
语句中拼接title
相关条件,当author
给出时,where
语句中拼接author
相关条件的示例,其余文件参考上篇博客
BlogMapper.java
package com.kuang.dao; import java.util.List; import org.apache.ibatis.annotations.Param; import com.kuang.pojo.Blog; /** * 功能描述 * * @since 2022-07-22 */ public interface BlogMapper { List<Blog> getBlogs(@Param("title") String title, @Param("author") String author); }
if
的详细使用方法见BlogMapper.xml文件
test
后面添加条件语句,与if结尾符中间添加执行语句,此处注意,为了保证两条if语句的执行正确,我们在sql中添加了一个where 1=1
的语句,这个语句不够优雅,在项目中也不应该存在,应该使用更优雅的解决方案where
标签
BlogMapper.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.kuang.dao.BlogMapper"> <select id="getBlogs" resultType="blog"> select * from blog where 1=1 <if test="title != null"> and title = #{title} </if> <if test="author != null"> and author = #{author} </if> </select> </mapper>
where
标签
where
标签即上面所说的where 1=1
的优雅的解决方案
添加改标签后,在执行的sql语句中会自动添加where 且会判断是否是第一个where条件,如果是,则将条件前面的and或者or给去掉
改造上面的例子
BlogMapper.java
package com.kuang.dao; import java.util.List; import org.apache.ibatis.annotations.Param; import com.kuang.pojo.Blog; /** * 功能描述 * * @since 2022-07-22 */ public interface BlogMapper { List<Blog> getBlogsWhere(@Param("title") String title, @Param("author") String author); }
BlogMapper.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.kuang.dao.BlogMapper"> <select id="getBlogsWhere" resultType="blog"> select * from blog <where> <if test="title != null"> and title = #{title} </if> <if test="author != null"> and author = #{author} </if> </where> </select> </mapper>
测试的时候你会发现,不同的输入会形成不同的执行sql,会添加where并会自动去除条件中多余的and
set
标签
set
标签顾名思义,是用来代替update语句中的set语句的,在set语句中我们知道,有的时候需要更新的参数不确定,存在时我们才更新,且更薪语句后面的,
不确定是否需要添加,这些问题set
标签都帮我们搞定了
看个例子
BlogMapper.java
package com.kuang.dao; import java.util.List; import org.apache.ibatis.annotations.Param; import com.kuang.pojo.Blog; /** * 功能描述 * * @since 2022-07-22 */ public interface BlogMapper { void updateBlog(@Param("title") String title, @Param("author") String author, @Param("id") String id); }
BlogMapper.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.kuang.dao.BlogMapper"> <update id="updateBlog"> update blog <set> <if test="title != null"> title = #{title}, </if> <if test="author != null"> author = #{author}, </if> </set> where id=#{id} </update> </mapper>
trim
标签
讲完where
和set
标签,我们发现这两个标签有共同点,在sql中会添加where
或set
,并自动的将语句中and
或,
优化,构成一个正确的sql用于执行,其实这两个标签都是由trim标签实现的,实现方法也很简单,我们一起来揭开他的神秘面纱
trim
标签可以设置 前缀(prefix),前缀覆盖(prefixOverrides),后缀(suffix) 前缀覆盖(suffixOverrides)
where
标签的实现
<trim prefix="WHERE" prefixOverrides="AND |OR "> ... </trim>
set
标签的实现
<trim prefix="SET" suffixOverrides=","> ... </trim>
choose(when,otherwise)
标签
choose
标签与java中的switch
语句类似,仅匹配条件中的一个, when
相当于case
, otherwise
相当于default
,不同之处在于,choose
标签不需要添加break类似的语句,不会继续执行,仅匹配一个条件
BlogMapper.java
package com.kuang.dao; import java.util.List; import org.apache.ibatis.annotations.Param; import com.kuang.pojo.Blog; /** * 功能描述 * * @since 2022-07-22 */ public interface BlogMapper { List<Blog> getBlogsChoose(@Param("title") String title, @Param("author") String author, @Param("views") int views); }
BlogMapper.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.kuang.dao.BlogMapper"> <select id="getBlogsChoose" resultType="blog"> select * from blog <where> <choose> <when test="title != null"> and title = #{title} </when> <when test="author != null"> and author = #{author} </when> <otherwise> and views >= #{views} </otherwise> </choose> </where> </select> </mapper>
这篇关于Mybatis系列--14-动态sql之if,choose(when,otherwise),set,where,trim语法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-09flutter3.x_macos桌面os实战
- 2024-05-09Rust中的并发性:Sync 和 Send Traits
- 2024-05-08使用Ollama和OpenWebUI在CPU上玩转Meta Llama3-8B
- 2024-05-08完工标准(DoD)与验收条件(AC)究竟有什么不同?
- 2024-05-084万 star 的 NocoDB 在 sealos 上一键起,轻松把数据库编程智能表格
- 2024-05-08Mac 版Stable Diffusion WebUI的安装
- 2024-05-08解锁CodeGeeX智能问答中3项独有的隐藏技能
- 2024-05-08RAG算法优化+新增代码仓库支持,CodeGeeX的@repo功能效果提升
- 2024-05-08代码报错不用愁,CodeGeeX一键完成代码修复、错误解释的功能上线了!
- 2024-05-08今天开始程序员不用再发愁写commit message了,全部由CodeGeeX自动完成!