本文是Mybatis基础系列的第三篇文章,点击下面链接可以查看前面的文章:

mybatis基础系列(二)——基础语法、别名、输入映射、输出映射

mybatis基础系列(一)——mybatis入门

动态sql

MyBatis 的强大特性之一便是它的动态 SQL。摆脱了JDBC中根据不同条件拼接 SQL 语句的痛苦。动态 SQL可以帮我们解决复杂需求。mybatis 动态SQL,通过 if, choose, when, otherwise, trim, where, set, foreach等标签组合成非常灵活的SQL语句。

根据员工编号或者员工姓名查询员工信息,输入的员工编号可能为空。

<select id="queryByEnameOrEempno" parameterType="com.itpsc.request.EmpRequest" resultType="com.itpsc.vo.EmpVo">
SELECT * FROM t_emp WHERE
<if test="emp.ename!=null">
ename=#{emp.ename}
</if>
<if test="emp.empno!=null">
and empno=#{emp.empno}
</if>
</select>

执行结果:

编号为空
==> Preparing: SELECT * FROM t_emp WHERE ename=?
==> Parameters: itpsc(String)
<== Columns: empno, ename, job, mgr, hiredate, sal, comm, deptno
<== Row: 7100, itpsc, developer, 7902, 1980-01-10, null, 1000.00, 20
<== Total: 1
姓名为空
==> Preparing: SELECT * FROM t_emp WHERE and empno=?
==> Parameters: 7100(Integer)

从执行结果可以看出,上面的if语句只能是在员工姓名不能为空的情况下执行,如果员工姓名为空sql语句就出错了,If+where语句可以解决这个问题。

if+where语句与trim 语句

<select id="queryByEnameOrEempno" parameterType="com.itpsc.request.EmpRequest" resultType="com.itpsc.vo.EmpVo">
SELECT * FROM t_emp
<where>
<if test="emp.ename!=null">
and ename=#{emp.ename}
</if>
<if test="emp.empno!=null">
and empno=#{emp.empno}
</if>
</where>
</select>

执行结果:

姓名为空
==> Preparing: SELECT * FROM t_emp WHERE empno=?
==> Parameters: 7100(Integer)
<== Columns: empno, ename, job, mgr, hiredate, sal, comm, deptno
<== Row: 7100, itpsc, developer, 7902, 1980-01-10, null, 1000.00, 20
<== Total: 1
编号为空
==> Preparing: SELECT * FROM t_emp WHERE ename=?
==> Parameters: itpsc(String)
<== Columns: empno, ename, job, mgr, hiredate, sal, comm, deptno
<== Row: 7100, itpsc, developer, 7902, 1980-01-10, null, 1000.00, 20
<== Total: 1

<where>标签中包含的标签中有返回值的话,它就插入一个where关键字,并且where 标签会自动将其后第一个条件的and或者是or给忽略掉。

trim语句可以实现<where>标签类似的功能:

(1)trim标签可以在包含的内容前加上前缀,也可以在其后加上后缀,对应的属性是prefix和suffix;

(2)trim标签可以把包含内容的首部某些内容忽略,也可以把尾部的某些内容忽略,对应的属性是prefixOverrides和suffixOverrides。

<select id="queryByEnameOrEempno" parameterType="com.itpsc.request.EmpRequest" resultType="com.itpsc.vo.EmpVo">
SELECT * FROM t_emp
<trim prefix="where" prefixOverrides="and | or">
<if test="emp.ename!=null">
and ename=#{emp.ename}
</if>
<if test="emp.empno!=null">
and empno=#{emp.empno}
</if>
</trim>
</select>

执行结果:

empno为空
==> Preparing: SELECT * FROM t_emp where ename=?
==> Parameters: itpsc2(String)
<== Columns: empno, ename, job, mgr, hiredate, sal, comm, deptno
<== Row: 7101, itpsc2, developer, 7902, 1980-01-10, 2000.00, 1000.00, 20
<== Total: 1 ename为空
==> Preparing: SELECT * FROM t_emp where empno=?
==> Parameters: 7100(Integer)
<== Columns: empno, ename, job, mgr, hiredate, sal, comm, deptno
<== Row: 7100, itpsc1, mannager, 7902, 1980-01-10, null, 1000.00, 20
<== Total: 1 empno、ename都不为空
==> Preparing: SELECT * FROM t_emp where ename=? and empno=?
==> Parameters: itpsc2(String), 7100(Integer)
<== Total: 0

if+set语句与trim语句

条件判断用<where>标签,同理更新操作用<set>标签。

<select id="updateEnameOrJob" parameterType="com.itpsc.request.EmpRequest">
UPDATE t_emp
<set>
<if test="emp.ename!=null">
ename=#{emp.ename}
</if>
<if test="emp.job!=null">
job=#{emp.job}
</if>
</set>
WHERE empno=#{emp.empno}
</select>

运行结果:

job为空
==> Preparing: UPDATE t_emp SET ename=? WHERE empno=?
==> Parameters: hello(String), 7100(Integer)
ename 为空
==> Preparing: UPDATE t_emp SET job=? WHERE empno=?
==> Parameters: itpsc(String), 7100(Integer)

<set>标签中包含的标签中有返回值的话,它就插入一个set关键字。job和ename都不为空则sql错误,那又如何写呢,trim标签可以帮我们能实现。

<select id="updateEnameOrJob" parameterType="com.itpsc.request.EmpRequest">
UPDATE t_emp
<trim prefix="set" suffixOverrides=",">
<if test="emp.ename!=null">
ename=#{emp.ename},
</if>
<if test="emp.job!=null">
job=#{emp.job},
</if>
</trim>
WHERE empno=#{emp.empno}
</select>

运行结果:

==>  Preparing: UPDATE t_emp set ename=?, job=? WHERE empno=?
==> Parameters: itpsc(String), mannager(String), 7100(Integer)

choose(when,otherwise) 语句

上的例子中,job和ename都不为空则是一种条件,job为空且ename不为空是一种条件,ename为空且job不为空是一种条件。实际上,我们要只要满足任意一个条件,就可以执行。choose(when,otherwise) 语句可以帮我们解决,类似jstl。

<select id="updateEnameOrJob" parameterType="com.itpsc.request.EmpRequest">
UPDATE t_emp
<set>
<choose>
<when test="emp.ename!=null and emp.job!=null">
ename=#{emp.ename},job=#{emp.job}
</when>
<otherwise>
<if test="emp.ename!=null">
ename=#{emp.ename}
</if>
<if test="emp.job!=null">
job=#{emp.job}
</if>
</otherwise>
</choose>
</set>
WHERE empno=#{emp.empno}
</select>

执行结果:

ename为空且job不为空
==> Preparing: UPDATE t_emp SET job=? WHERE empno=?
==> Parameters: mannager(String), 7100(Integer) job为空且ename不为空
==> Preparing: UPDATE t_emp SET ename=? WHERE empno=?
==> Parameters: itpsc2(String), 7100(Integer) job和ename都不为空
==> Preparing: UPDATE t_emp SET ename=?,job=? WHERE empno=?
==> Parameters: itpsc2(String), mannager(String), 7100(Integer)

foreach 语句

需求:根据员工编号列表查询员工信息。7369,7499,7521。

Sql的写法是:select * from t_emp where 1=1 and empno=7369 or empno=7499 or empno=7521

mybatis的foreach语句可以帮我们实现类似功能。

<select id="queryByIds"
parameterType="com.itpsc.request.IdRequest" resultType="com.itpsc.vo.EmpVo">
SELECT * FROM t_emp
<where>
<foreach collection="ids" item="id" open="and (" close=")" separator="or">
empno=#{id}
</foreach>
</where>
</select>

collection:指定输入对象中的集合属性

item:每次遍历生成的对象

open:开始遍历时的拼接字符串

close:结束时拼接的字符串

separator:遍历对象之间需要拼接的字符串

运行结果:

==>  Preparing: SELECT * FROM t_emp WHERE ( empno=? or empno=? or empno=? )
==> Parameters: 7369(Integer), 7499(Integer), 7521(Integer)
<== Columns: empno, ename, job, mgr, hiredate, sal, comm, deptno
<== Row: 7369, SMITH, CLERK, 7902, 1980-12-17, 800.00, null, 20
<== Row: 7499, ALLEN, SALESMAN, 7698, 1981-02-20, 1600.00, 300.00, 30
<== Row: 7521, WARD, SALESMAN, 7698, 1981-02-22, 1250.00, 500.00, 30
<== Total: 3

sql片段

将上面动态sql代码块抽取出来,组成一个sql片段,其它的statement中就可以引用该sql片段。提供代码复用性,方便团队成员之间进行开发。

<select id="queryCount2" parameterType="com.itpsc.request.EmpRequest" resultType="int">
SELECT count(*) FROM t_emp
<where>
<if test="emp!=null">
<if test="emp.deptno!=null">
and emp.deptno=#{emp.deptno}
</if>
<if test="emp.ename!=null">
and emp.ename=#{emp.ename}
</if>
<if test="emp.job!=null">
and emp.job=#{emp.job}
</if>
</if>
</where>
</select>

上面where语句中代码,我们可以通过<sql>标签封装成一个sql片段,然后在其它statement中通过<include>引用。

<sql id="querySql">
<if test="emp!=null">
<if test="emp.deptno!=null">
and emp.deptno=#{emp.deptno}
</if>
<if test="emp.ename!=null">
and emp.ename=#{emp.ename}
</if>
<if test="emp.job!=null">
and emp.job=#{emp.job}
</if>
</if>
</sql> <select id="queryCount2" parameterType="com.itpsc.request.EmpRequest" resultType="int">
SELECT count(*) FROM t_emp
<where>
<include refid="querySql"></include>
</where>
</select>

本文到此,下篇 mybatis基础系列(四)——关联查询、延迟加载、一级缓存与二级缓存。

mybatis基础系列(三)——动态sql的更多相关文章

  1. mybatis基础系列(四)——关联查询、延迟加载、一级缓存与二级缓存

    关本文是Mybatis基础系列的第四篇文章,点击下面链接可以查看前面的文章: mybatis基础系列(三)——动态sql mybatis基础系列(二)——基础语法.别名.输入映射.输出映射 mybat ...

  2. mybatis基础系列(二)——基础语法、别名、输入映射、输出映射

    增删改查 mapper根节点及其子节点 mybatis框架需要读取映射文件创建会话工厂,映射文件是以<mapper>作为根节点,在根节点中支持9个元素,分别为insert.update.d ...

  3. MyBatis学习总结_11_MyBatis动态Sql语句

    MyBatis中对数据库的操作,有时要带一些条件,因此动态SQL语句非常有必要,下面就主要来讲讲几个常用的动态SQL语句的语法 MyBatis中用于实现动态SQL的元素主要有: if choose(w ...

  4. SQL Server 2008空间数据应用系列三:SQL Server 2008空间数据类型

    原文:SQL Server 2008空间数据应用系列三:SQL Server 2008空间数据类型 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft SQL Server ...

  5. mybatis入门系列三之类型转换器

    mybatis入门系列三之类型转换器 类型转换器介绍 mybatis作为一个ORM框架,要求java中的对象与数据库中的表记录应该对应 因此java类名-数据库表名,java类属性名-数据库表字段名, ...

  6. mybatis基础系列(一)——mybatis入门

    好久不发博客了,写博文的一个好处是能让心静下来,整理下之前学习过的一些知识一起分享,大神路过~ mybatis简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射. ...

  7. Spring mybatis源码篇章-动态SQL节点源码深入

    通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-动态SQL基础语法以及原理 前话 前文描述到通过mybatis默认的解析驱动类org.apache.ibat ...

  8. MyBatis学习系列三——结合Spring

    目录 MyBatis学习系列一之环境搭建 MyBatis学习系列二——增删改查 MyBatis学习系列三——结合Spring MyBatis在项目中应用一般都要结合Spring,这一章主要把MyBat ...

  9. 【C++自我精讲】基础系列三 重载

    [C++自我精讲]基础系列三 重载 0 前言 分二部分:函数重载,操作符重载. 1 函数重载 函数重载:指在同一名字空间中,函数名称相同,参数类型.顺序或数量不同的一类函数,同一函数名的函数能完成不同 ...

随机推荐

  1. Paired t-test

    1 Continuous Dependent Variable with normal distribution 1 (2 Level) Categorical Independent Variabl ...

  2. css的一些细节

    1.中文符号居中效果 对于动态输出文字可以不用在意,某些页面可能会有类似提示文案的地方,用英文标点符号,对于居中效果比较友好. 2.元素的上下间距 布局的时候从上往下开始写页面,一般都是写下一个的元素 ...

  3. sql: Query to Display Foreign Key Relationships and Name of the Constraint for Each Table in Database

    --20170505 --塗聚文 Geovin Du CREATE DATABASE DuMailSystem GO USE DuMailSystem GO --1查詢表的及备注说明 SELECT S ...

  4. Django框架理解和使用常见问题

    1.什么是中间件? 中间件是介于request与response处理之间的一道处理过程,相对比较轻量级,并且在全局上改变django的输入与输出. 中间件一般做认证或批量请求处理,django中的中间 ...

  5. 【读书笔记】iOS-自定义 URL Scheme 完全指南

    iPhone / iOS SDK 最酷的特性之一就是应用将其自身”绑定”到一个自定义 URL scheme 上,该 scheme 用于从浏览器或其他应用中启动本应用.   注册自定义 URL Sche ...

  6. 【读书笔记】iOS-Apple的移动设备硬件

    本书中有一个关键观点是:“硬件并不是特别重要,用户体验才是真正的杀手级应用.“尽管如此,多了解一些你使用的硬件的相关知识,对于整个项目来说是必备的,而对于设计和开发高质量的作品来说敢是不可或缺的. 人 ...

  7. 二层协议--LLDP协议总结

    二层协议--LLDP协议总结,待完善.

  8. 洗礼灵魂,修炼python(15)--列表进阶话题—>列表解析/列表生成器

    是的,我是想到什么知识点就说什么,没有固定的主题,我的标题都是在写完博客再给的.本篇博文说说列表进阶话题.其实列表应该是比较熟悉的了,而毫不夸张的说,在实际的开发中,列表也是使用的最多的,以后你会体会 ...

  9. 监控.net 网站 Glimpse

    使用Nuget 安装Glimpse 安装好后,config会默认添加几个节点 安装好之后 只需要浏览器输入  网站/Glimpse.axd 再次进入网站 就可以查看(ajax sql session ...

  10. 【PAT】B1003 我要通过!

    我觉得这是PAT中最坑的一道题,表述令人很不适应 分析过程: 条件1.只有P,A,T三种字符 条件2.xPATx正确,x可以是空串,或者由A组成的字符串 条件3.如果aPbTc是正确的,aPbATca ...