MyBatis 示例-动态 SQL
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 示例-动态 SQL的更多相关文章
- MyBatis的动态SQL详解
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑,本文详解mybatis的动态sql,需要的朋友可以参考下 MyBatis 的一个强大的特性之一通常是它 ...
- MyBatis的动态SQL详解-各种标签使用
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. MyBatis中用于实现动态SQL的元素主要有: if choose(when,otherwise) ...
- 利用MyBatis的动态SQL特性抽象统一SQL查询接口
1. SQL查询的统一抽象 MyBatis制动动态SQL的构造,利用动态SQL和自定义的参数Bean抽象,可以将绝大部分SQL查询抽象为一个统一接口,查询参数使用一个自定义bean继承Map,使用映射 ...
- MyBatis中动态SQL元素的使用
掌握MyBatis中动态SQL元素的使用 if choose(when,otherwise) trim where set foreach <SQL>和<include> 在应 ...
- Java-MyBatis:MyBatis 3 动态 SQL
ylbtech-Java-MyBatis:MyBatis 3 动态 SQL 1.返回顶部 1. 动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其它类似框架 ...
- 一分钟带你了解下MyBatis的动态SQL!
MyBatis的强大特性之一便是它的动态SQL,以前拼接的时候需要注意的空格.列表最后的逗号等,现在都可以不用手动处理了,MyBatis采用功能强大的基于OGNL的表达式来实现,下面主要介绍下. 一. ...
- Mybatis中动态SQL语句中的parameterType不同数据类型的用法
Mybatis中动态SQL语句中的parameterType不同数据类型的用法1. 简单数据类型, 此时#{id,jdbcType=INTEGER}中id可以取任意名字如#{a,jdbcType ...
- Mybatis解析动态sql原理分析
前言 废话不多说,直接进入文章. 我们在使用mybatis的时候,会在xml中编写sql语句. 比如这段动态sql代码: <update id="update" parame ...
- mybatis 使用动态SQL
RoleMapper.java public interface RoleMapper { public void add(Role role); public void update(Role ro ...
随机推荐
- css定位中的百分比
----转载自自己在牛人部落中的相关文章--- 在前端css定位中经常面对的一个问题是,百分比定位究竟是针对于谁定位? 一.margin,padding的百分比 首先从css的设计意图说起,在浏览器默 ...
- ICEM—非结构化周期网格
原视频下载地址:https://yunpan.cn/cPBnmsNheJ46q 访问密码 3441
- ntp时间同步服务器的搭建
CentOS系统一般自带安装有ntp服务,仅需做相关配置即可. 一.配置ntp服务器: 在选定的ntp服务器上vim /etc/ntp.conf 添加一行:restrict default nomod ...
- 使用pycharm发布python程序到ubuntu中运行
前提条件: 1.ubuntu安装了vsftpd,可以参考:https://www.cnblogs.com/xienb/p/9322805.html 2.安装专业版pycharm 步骤: 1.新建一个P ...
- JAVA通过FTP方式向远程服务器或者客户端上传、下载文件,以及删除FTP服务器上的文件
1.在目标服务器上搭建FTP服务器 搭建方式有多种大家可以自行选择,例如使用Serv-U或者FTPServer.exe:这里我以FTPServer.exe为例搭建:在目标服务器(这里对应的IP是10. ...
- ThinkPHP使用smarty模板引擎的方法
ThinkPHP支持多种php模板引擎,可以根据个人需要加以配置.下面我们以Smarty模板引擎为例,给大家说说具体的操作流程! 首先去Smarty官网上下载一个Smarty.本站下载地址:http: ...
- C++ STL partial_sort_copy
#include <iostream>#include <deque>#include <algorithm>#include <vector> usi ...
- 一百三十一:CMS系统之轮播图上传图片功能
将七牛js放到common下 把获取uptoken的接口放到common视图中 把初始化七牛放到banners.js中 //初始化七牛$(function () { qiniujs.setUp({ ' ...
- nginx使用vhost子目录
在主配置文件http模块最后添加如下一句话 [root@host---- ~]# vi /etc/nginx/nginx.conf include /etc/nginx/conf.d/*.conf; ...
- 集群架构05·备份服务rsync
初识 开源,多功能,全量和增量的本地或远程数据同步备份的优秀工具,remote synchronization 俩服务器定/实时备份cron+rsync,数据同步,全网备份 一个rsync相当于scp ...