带你了解下MyBatis的动态SQL!

运维 数据库运维
MyBatis的强大特性之一便是它的动态SQL,以前拼接的时候需要注意的空格、列表最后的逗号等,现在都可以不用手动处理了,MyBatis采用功能强大的基于OGNL的表达式来实现,下面主要介绍下。

MyBatis的强大特性之一便是它的动态SQL,以前拼接的时候需要注意的空格、列表最后的逗号等,现在都可以不用手动处理了,MyBatis采用功能强大的基于OGNL的表达式来实现,下面主要介绍下。

[[283139]]

一、if标签

if是最常用标签,经常用在判断语句上,可以实现某些简单的条件选择。基本使用示例如下:

  1. <select id="queryAllUsersByName" resultType="com.example.springboot.mybatisxml.entity.User"
  2.        select * from user where 1=1 
  3.        <if test="name != null and name != ''"
  4.            and name = #{name
  5.        </if> 
  6.        <if test="age != null "
  7.            and age = #{age} 
  8.        </if> 
  9.    </select

二、where标签

上面的例子中使用了“1=1”,是为了避免后续条件不满足时候报错,那有没有办法避免这种写法呢?

当然有,就是接下来要说的where,where标签会自动判断如果包含的标签中有返回值的话,就在sql中插入一个where,如果where标签最后返回的内容是以and 或者or开头的,也会被自动移除掉,上面例子中换成where标签后写法如下:

  1. <select id="queryAllUsersByName" resultType="com.example.springboot.mybatisxml.entity.User"
  2.         select * from user  
  3.         <where
  4.             <if test="name != null and name != ''"
  5.                 and name = #{name
  6.             </if> 
  7.             <if test="age != null "
  8.                 and age = #{age} 
  9.             </if> 
  10.         </where
  11.     </select

三、trim标签

trim的作用是去除特殊的字符串,它的prefix属性代表语句的前缀,prefixOverrides属性代表需要去除的哪些特殊字符串,prefixOverrides属性会忽略通过管道分隔的字符,后缀的处理和前缀一样。

trim标签的主要属性如下

  • prefix:前缀覆盖并增加其内容。
  • suffix:后缀覆盖并增加其内容。
  • prefixOverrides:前缀判断的条件。
  • suffixOverrides:后缀判断的条件。

举两个例子。

使用前缀属性

  1. <select id="queryAllUsersByName" resultType="com.example.springboot.mybatisxml.entity.User"
  2.        select * from user 
  3.        <trim prefix="WHERE" prefixOverrides="AND |OR " > 
  4.            <if test="name != null and name != ''"
  5.                and name = #{name
  6.            </if> 
  7.            <if test="sex != null "
  8.                or sex = #{sex} 
  9.            </if> 
  10.            <if test="age != null "
  11.                and age = #{age} 
  12.            </if> 
  13.        </trim> 
  14.    </select

使用后缀属性

  1. <update id="update" parameterType="Object"
  2.         UPDATE user 
  3.         <trim prefix=" SET " prefixOverrides=","
  4.             <if test="id != null ">,id=#{id}</if> 
  5.             <if test="name != null ">,name=#{name}</if> 
  6.             <if test="age != null ">,age=#{age}</if> 
  7.         </trim> 
  8.         WHERE ID=#{id} 
  9.     </update

四、foreach标签

foreach的作用是遍历集合,它能够很好地支持数组和List、Set接口的集合的遍历,往往和sql中的in组合比较多。

foreach标签的主要属性如下

  • item:表示循环中当前的元素。
  • index:表示当前元素在集合的位置下标。
  • collection:配置list的属性名等。
  • open和close:配置的是以什么符号将这些集合元素包装起来。
  • separator:配置的是各个元素的间隔符。

举个例子:

  1. <select id="queryAllUsersByName" resultType="com.example.springboot.mybatisxml.entity.User"
  2.         select * from user where id in 
  3.         <foreach item="id" index="index" collection="userList" 
  4.                  open="(" separator="," close=")"
  5.             #{id} 
  6.         </foreach> 
  7.     </select
责任编辑:武晓燕 来源: Java碎碎念
相关推荐

2015-08-26 16:38:37

mybatissql

2021-03-02 09:15:24

MyBatisSQL数据库

2021-12-06 22:44:51

Windows 10Windows微软

2024-02-04 09:24:45

MyBatisSQL语句Spring

2020-03-10 14:30:14

动态路由RIP网关协议

2011-04-01 13:56:12

SQL Server数

2020-07-07 07:34:29

RedisSDS数据结构

2020-02-10 10:55:37

路由协议OSPFLSA

2020-03-22 21:32:37

动态路由OSPFLSA

2022-09-26 11:30:40

MQTT协议客户端协议

2019-12-19 08:56:21

MybatisSQL执行器

2018-09-18 14:34:43

GIT系统实践

2010-08-31 22:29:54

DHCP分配

2023-01-06 19:19:16

TensorFlow

2018-07-30 13:51:06

2019-09-27 09:40:06

ElvishShellLinux

2010-07-05 16:20:32

NetBEUI协议

2020-07-23 22:54:39

SQL数据库存储

2021-02-03 16:22:43

新基建SAP

2020-12-31 12:16:49

SAP云计算SAP产品
点赞
收藏

51CTO技术栈公众号