动态sql可以很方便的拼接sql语句,主要用于复合条件查询;

主要通过这几个标签实现:

if 标签:

where 标签

choose标签:

foreach标签:


if 标签:

<select id="selectStudentByIf" resultType="Student">
select id,name,age,score from student where 1=1
<if test="name != null and name != ''">
and name like '%' #{name} '%'
</if>
<if test="age > 0">
and age < #{age}
</if>
</select>

使用if标签判断参数,可以使用where 1=1,后面跟上if标签,这样可以避免因为‘and’导致sql语句冲突;

where 标签

<select id="selectStudentByWhere" resultType="Student">
select id,name,age,score from student
<where>
<if test="name != null and name != ''">
and name like '%' #{name} '%'
</if>
<if test="age > 0">
and age < #{age}
</if>
</where>
</select>

  

由于使用where 1=1 对查询性能也有一定的影响;可以使用where嵌套 if 标签,可以不需要使用上面“where 1=1”;

where标签会自动过滤掉if标签里sql语句的第一个and连接词;

作用:用来简化SQL语句中的where条件,可以嵌套其他标签;

choose标签:

<select id="selectStudentByChoose" resultType="Student">
select id,name,age,score from student
<where>
<choose>
<when test="name != null and name != ''">
name like '%' #{name} '%'
</when>
<when test="age > 0">
age < #{age}
</when>
<otherwise>
1 != 1
</otherwise>
</choose>
</where>
</select>

  

 表示:

当查询条件有name时,不管有没有age,都只按name查询;

当查询条件没有name时,才按age进行查询;

当name和age都没有时,查询不到任何数据。

作用:类似于switch语句,选择多个条件中的一个;

foreach标签:

foreach用来遍历,遍历的对象可以是数组,也可以是集合。

属性:

collection可以指定遍历对象的类型:array,list

item是指定的集合中每一个元素迭代时候的别名;

separator表示在每次进行迭代之间以什么符号作为分隔符;

open表示该语句以什么开始,close表示以什么结束;

<select id="selectStudentByForeachArray" resultType="Student">
select id,name,age,score from student
<if test="array != null and array.length > 0">
where id in
<foreach collection="array" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</if>
</select>

  

表示遍历一个数组,迭代别名为id;

    <select id="selectStudentByForeachList" resultType="Student">
select id,name,age,score from student
<if test="list != null and list.size > 0">
where id in
<foreach collection="list" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</if>
</select>

  

遍历一个list集合;

    <select id="selectStudentByForeachList2" resultType="Student">
select id,name,age,score from student
<if test="list != null and list.size > 0">
where id in
<foreach collection="list" item="stu" open="(" close=")" separator=",">
#{stu.id}
</foreach>
</if>
</select>

遍历一个list,list中每个元素为对象;遍历对象的id属性;

Mybatis基于xml的动态sql实现的更多相关文章

  1. MyBatis之基于XML的动态SQL

    先说下我的梦想,大学的时候一直想着是能开店卖胡辣汤,到目前依然还是我的梦想,上周一家出版社联系我问我有没有时间可以合作出书,这也是我的梦想之一,想了想还是放弃了,至少觉得目前不行,毕竟工作还不到五年, ...

  2. MyBatis基于注解的动态SQL——概览

  3. Mybatis基于接口注解配置SQL映射器(一)

    上文已经讲解了基于XML配置的SQL映射器,在XML配置的基础上MyBatis提供了简单的Java注解,使得我们可以不配置XML格式的Mapper文件,也能方便的编写简单的数据库操作代码. Mybat ...

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

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

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

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

  6. MyBatis:学习笔记(4)——动态SQL

    MyBatis:学习笔记(4)——动态SQL

  7. SSM框架之Mybatis(6)动态SQL

    Mybatis(6)动态SQL 1.动态SQL 出现原因:有些时候业务逻辑复杂时,我们的 SQL 是动态变化的,此时在前面的学习中我们的 SQL 就不能满足要求了 1.1.if标签 我们根据实体类的不 ...

  8. Mybatis基于XML配置SQL映射器(三)

    Mybatis之动态SQL mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类: if choo ...

  9. Mybatis基于XML配置SQL映射器(二)

    Mybatis之XML注解 之前已经讲到通过 mybatis-generator 生成mapper映射接口和相关的映射配置文件: 下面我们将详细的讲解具体内容 首先我们新建映射接口文档  sysUse ...

随机推荐

  1. 京东Java架构师讲解购物车的原理及Java实现

    今天来写一下关于购物车的东西, 这里首先抛出四个问题: 1)用户没登陆用户名和密码,添加商品, 关闭浏览器再打开后 不登录用户名和密码问:购物车商品还在吗? 2)用户登陆了用户名密码,添加商品,关闭浏 ...

  2. ICEM-三角形特征几何

    原视频下载地址:https://pan.baidu.com/s/1qY8SKri 密码: wf17

  3. 子查询优化 - Hyper

    Unnesting Arbitrary Queries - T Neumann, A KemperThe Complete Story of Joins (in HyPer) - Thomas Neu ...

  4. [译]如何根据Pandas中的列名获取列所在的index位置?

    原文来源:https://stackoverflow.com/questions/13021654/get-column-index-from-column-name-in-python-pandas ...

  5. 深入理解Flink ---- 系统内部消息传递的exactly once语义

    At Most once,At Least once和Exactly once 在分布式系统中,组成系统的各个计算机是独立的.这些计算机有可能fail. 一个sender发送一条message到rec ...

  6. (转载) AutoML 与轻量模型大列表

    作者:guan-yuan 项目地址:awesome-AutoML-and-Lightweight-Models 博客地址:http://www.lib4dev.in/info/guan-yuan/aw ...

  7. [LeetCode] 361. Bomb Enemy 炸敌人

    Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return ...

  8. 【Python学习之四】集合类型

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 python3.6 一.字符串:字符串实际上就是字符的数组1.切片是指 ...

  9. 面试之哈希表leetcode

    1 案例1 leetcode-----242 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词. 示例 1: 输入: s = "anagram", t ...

  10. jvm面试常见题

    背景:jvm相关题目面试必问,后面要深入的进行总结. JVM 面试知识整理 jvm调优命令 调优工具 Minor GC ,Full GC 触发条件 Minor GC触发条件:当Eden区满时,触发Mi ...