Mybatis基于xml的动态sql实现
动态sql可以很方便的拼接sql语句,主要用于复合条件查询;
主要通过这几个标签实现:
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实现的更多相关文章
- MyBatis之基于XML的动态SQL
先说下我的梦想,大学的时候一直想着是能开店卖胡辣汤,到目前依然还是我的梦想,上周一家出版社联系我问我有没有时间可以合作出书,这也是我的梦想之一,想了想还是放弃了,至少觉得目前不行,毕竟工作还不到五年, ...
- MyBatis基于注解的动态SQL——概览
- Mybatis基于接口注解配置SQL映射器(一)
上文已经讲解了基于XML配置的SQL映射器,在XML配置的基础上MyBatis提供了简单的Java注解,使得我们可以不配置XML格式的Mapper文件,也能方便的编写简单的数据库操作代码. Mybat ...
- Spring mybatis源码篇章-动态SQL节点源码深入
通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-动态SQL基础语法以及原理 前话 前文描述到通过mybatis默认的解析驱动类org.apache.ibat ...
- MyBatis学习总结_11_MyBatis动态Sql语句
MyBatis中对数据库的操作,有时要带一些条件,因此动态SQL语句非常有必要,下面就主要来讲讲几个常用的动态SQL语句的语法 MyBatis中用于实现动态SQL的元素主要有: if choose(w ...
- MyBatis:学习笔记(4)——动态SQL
MyBatis:学习笔记(4)——动态SQL
- SSM框架之Mybatis(6)动态SQL
Mybatis(6)动态SQL 1.动态SQL 出现原因:有些时候业务逻辑复杂时,我们的 SQL 是动态变化的,此时在前面的学习中我们的 SQL 就不能满足要求了 1.1.if标签 我们根据实体类的不 ...
- Mybatis基于XML配置SQL映射器(三)
Mybatis之动态SQL mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类: if choo ...
- Mybatis基于XML配置SQL映射器(二)
Mybatis之XML注解 之前已经讲到通过 mybatis-generator 生成mapper映射接口和相关的映射配置文件: 下面我们将详细的讲解具体内容 首先我们新建映射接口文档 sysUse ...
随机推荐
- Centos 7配置nginx反向代理负载均衡集群
一,实验介绍 利用三台centos7虚拟机搭建简单的nginx反向代理负载集群, 三台虚拟机地址及功能介绍 192.168.2.76 nginx负载均衡器 192.168.2.82 web ...
- php 把数字转化为大写中文—升级版
继上篇之后,发现某同事悄悄改了新版本,于是被我偷偷保存起来了,功能一样,不过他的比较短小,emmm.放了快一年了,悄悄放到博客里面. 功能需求在另一篇博客里 <?php function cny ...
- 第06组 Beta冲刺(1/4)
队名:福大帮 组长博客链接: https://www.cnblogs.com/mhq-mhq/p/11990568.html 作业博客 : https://edu.cnblogs.com/campus ...
- 第09组 Alpha冲刺(4/4)
队名:软工9组 组长博客:https://www.cnblogs.com/cmlei/ 作业博客:https://edu.cnblogs.com/campus/fzu/SoftwareEngineer ...
- locust参数化(数据库取值)
locust参数化(数据库取值) 基于上一篇参数化的梳理,本篇用另一种方法从数据库中取出这100个用户来登录 思路:在 TaskSet 中的 on_start 方法表示执行任务前的操作,可以将数据库取 ...
- c 越界
int main(int argc, char* argv[]){ int i = 0; int arr[3] = {0}; for(; i<=3; i++){ arr[i] = 0; prin ...
- Redis和Memcached的异同
Memcached 可以利用多核优势,单实例吞吐量极高,可以达到几十万QPS: 只支持简单的key/value数据结构,不像Redis可以支持丰富的数据类型. 无法进行持久化,数据不能备份,只能用于缓 ...
- 【翻译】Flink Table Api & SQL —— 数据类型
本文翻译自官网:https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/types.html Flink Table ...
- springboot项目中使用spring-data-Redis对map序列化时报错
错误信息: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String at org.spri ...
- 第6/7Beta冲刺
1.团队成员 成员姓名 成员学号 秦裕航 201731062432(组长) 刘东 201731062227 张旭 201731062129 王伟 201731062214 2.SCRU部分 2.1各成 ...