MyBatis 的动态 SQL 包括以下几种元素:

详细的使用参考官网文档:http://www.mybatis.org/mybatis-3/zh/dynamic-sql.html

本章内容简单描述这些动态 SQL 在使用的过程中需要注意的地方。

choose, when, otherwise

比如我们要实现如下功能:

  • 当学生姓名不为空,则只用学生姓名作为条件查询
  • 当学生性别不为空,则只用学生性别作为条件查询
  • 当学生姓名和学生性别都为空,则要求学生学生证件号不为空

针对这种情况,MyBatis 提供了 choose 元素,它有点像 Java 中的 switch 语句。

<select id="listByConditions" parameterType="studentQuery" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from t_student
<where>
<choose>
<when test="name != null and name != ''">
AND name LIKE CONCAT('%', #{name}, '%')
</when>
<when test="sex != null">
AND sex = #{sex}
</when>
<otherwise>
AND selfcard_no is not null
</otherwise>
</choose>
</where>
</select>

trim, where, set

比如下面的动态 SQL 有什么问题?

<select id="listByConditions" parameterType="studentQuery" resultMap="BaseResultMap">
SELECT id, name, sex, selfcard_no, note
FROM t_student
WHERE
<if test="ids != null and ids.size() > 0">
id IN
<foreach collection="ids" item="item" open="(" close=")" separator=",">
#{item}
</foreach>
</if>
<if test="name != null and name != ''">
AND name LIKE CONCAT('%', #{name}, '%')
</if>
<if test="sex != null">
AND sex = #{sex}
</if>
<if test="selfcardNo != null">
AND selfcard_no = #{selfcardNo}
</if>
</select>

如果这些条件没有一个能匹配上会发生什么?最终这条 SQL 会变成这样:

SELECT id, name, sex, selfcard_no, note
FROM t_student
WHERE

这样会导致 SQL 语句执行失败。如果仅仅第二个条件匹配又会怎样?这条 SQL 最终会是这样:

SELECT id, name, sex, selfcard_no, note
FROM t_student
WHERE
AND name LIKE CONCAT('%', 'a', '%')

这个查询也会失败。

MyBatis 提供了 <where> 元素可以解决上面的问题,将上面的动态 SQL 改成如下形式。

<select id="listByConditions" parameterType="studentQuery" resultMap="BaseResultMap">
SELECT id, name, sex, selfcard_no, note
FROM t_student
<where>
<if test="ids != null and ids.size() > 0">
AND id IN
<foreach collection="ids" item="item" open="(" close=")" separator=",">
#{item}
</foreach>
</if>
<if test="name != null and name != ''">
AND name LIKE CONCAT('%', #{name}, '%')
</if>
<if test="sex != null">
AND sex = #{sex}
</if>
<if test="selfcardNo != null">
AND selfcard_no = #{selfcardNo}
</if>
</where>
</select>

foreach

实现批量新增功能,动态 SQL 如下:

<insert id="batchInsertByNoAutoInc" parameterType="list">
<selectKey keyProperty="id" resultType="long" order="BEFORE">
select if(max(id) is null, 1, max(id) + 2) as newId from t_student
</selectKey>
insert into t_student (name, sex, selfcard_no, note)
values
<foreach collection="list" item="item" index="index" separator=",">
(
#{item.name,jdbcType=VARCHAR},
#{item.sex,jdbcType=TINYINT},
#{item.selfcardNo,jdbcType=BIGINT},
#{item.note,jdbcType=VARCHAR}
)
</foreach>
</insert>

批量新增的操作需要确保 list 中必须有值。

MyBatis 实用篇

MyBatis 概念

MyBatis 示例-简介

MyBatis 示例-类型处理器

MyBatis 示例-传递多个参数

MyBatis 示例-主键回填

MyBatis 示例-动态 SQL

MyBatis 示例-联合查询

MyBatis 示例-缓存

MyBatis 示例-插件

MyBatis 示例-动态 SQL的更多相关文章

  1. MyBatis的动态SQL详解

    MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑,本文详解mybatis的动态sql,需要的朋友可以参考下 MyBatis 的一个强大的特性之一通常是它 ...

  2. MyBatis的动态SQL详解-各种标签使用

    MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. MyBatis中用于实现动态SQL的元素主要有: if choose(when,otherwise) ...

  3. 利用MyBatis的动态SQL特性抽象统一SQL查询接口

    1. SQL查询的统一抽象 MyBatis制动动态SQL的构造,利用动态SQL和自定义的参数Bean抽象,可以将绝大部分SQL查询抽象为一个统一接口,查询参数使用一个自定义bean继承Map,使用映射 ...

  4. MyBatis中动态SQL元素的使用

    掌握MyBatis中动态SQL元素的使用 if choose(when,otherwise) trim where set foreach <SQL>和<include> 在应 ...

  5. Java-MyBatis:MyBatis 3 动态 SQL

    ylbtech-Java-MyBatis:MyBatis 3 动态 SQL 1.返回顶部 1. 动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其它类似框架 ...

  6. 一分钟带你了解下MyBatis的动态SQL!

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

  7. Mybatis中动态SQL语句中的parameterType不同数据类型的用法

    Mybatis中动态SQL语句中的parameterType不同数据类型的用法1. 简单数据类型,    此时#{id,jdbcType=INTEGER}中id可以取任意名字如#{a,jdbcType ...

  8. Mybatis解析动态sql原理分析

    前言 废话不多说,直接进入文章. 我们在使用mybatis的时候,会在xml中编写sql语句. 比如这段动态sql代码: <update id="update" parame ...

  9. mybatis 使用动态SQL

    RoleMapper.java public interface RoleMapper { public void add(Role role); public void update(Role ro ...

随机推荐

  1. Ubuntu下制作deb包的方法详解

    1  认识deb包 1.1   认识deb包 deb是Unix系统(其实主要是Linux)下的安装包,基于 tar 包,因此本身会记录文件的权限(读/写/可执行)以及所有者/用户组. 由于 Unix ...

  2. Centos - php5.4升级到7.1 yum安装

    查看当前 PHP 版本 1 php -v 查看当前 PHP 相关的安装包,删除之 1 2 3 4 5 yum list installed | grep php   yum remove php   ...

  3. JAVA的main方法

    在Java中,main()方法是Java应用程序的入口方法,也就是说,程序在运行的时候,第一个执行的方法就是main()方法,这个方法和其他的方 法有很大的不同,比如方法的名字必须是main,方法必须 ...

  4. kotlin陪伴对象

    在kotlin中并没有静态类成员的概念,但并不等于实现类似于静态类成员的功能,陪伴对象可以解决这个问题 fun main(arg: Array<String>) { val create ...

  5. JMX简介及was上的使用

    参考文章:https://www.ibm.com/developerworks/cn/websphere/library/techarticles/0908_sunyan_jmxdeploy/inde ...

  6. bs4笔记

    1.网页输出乱码的解决办法 r= requests.get('https://www.baidu.com/') r.encoding = 'gbk2312'   #有可能 gbk.utf-8 soup ...

  7. Jmeter之分布式部署测试

    在使用Jmeter进行性能测试时,因受单机电脑的配置限制,可能无法支持较大数量的并发,此时就需要使用Jmeter提供的分布式测试的功能. jmeter分布式测试的执行原理是选择一台作为调度机,其他机器 ...

  8. 深入浅出的分析 Set集合

    01. 摘要 Set集合的特点主要有:元素不重复.存储无序的特点. 打开 Set 集合,主要实现类有 HashSet.LinkedHashSet .TreeSet .EnumSet( RegularE ...

  9. XSSer:自动化XSS漏洞检测及利用工具

    转载自FreeBuf.COM XSS是一种非常常见的漏洞类型,它的影响非常的广泛并且很容易的就能被检测到. 攻击者可以在未经验证的情况下,将不受信任的JavaScript片段插入到你的应用程序中,然后 ...

  10. X-Forwarded-For注入漏洞实战

    准备工具 Burp suite Sqlmap 实训目标 1.掌握SQL注入的基本原理:2.了解服务器获取客户端IP的方式:3.了解SQL注入的工具使用: 解题方向 对登录表单的各参数进行测试,找到SQ ...