(三)Mybatis总结之动态sql
动态sql
为何需要动态sql?因为简单的sql语句已经不能满足复杂的业务需求
动态sql相当于sql语句拼接
1.if语句
if语句:判断,如果执行多条件查询,如果中间某个条件变量为空,就跳过当前判断(包括if里面的sql语句),执行下一条语句。
栗子如下:
<select id="getUserBy" resultType="com.qf.pojo.User" parameterType="com.qf.pojo.User">
select * from user where 1=1
<if test="name != null and name != ''">
and name = #{name}
</if>
<if test="age != null and age != ''">
and age = #{age}
</if>
</select>
<!--如果不加1=1,当所有条件为空的时候,sql语句为select * from user where,就会报错。当然这样的做法很粗鲁,优雅的处理请看if+where。-->
2.if+where不定向查询
if+where语句:where标签里面如果包含了标签中的返回值的话(只要有if的条件不为空),它就插入一个where。如果if标签里面是以and或者or开头,则它(and和or)会被剔除。语法如下:
Select * from 表名
<where>
<if test=”属性名!=null”>
and/or 列名=#{取值}
</if>
……
</where>
栗子:
<select id="getUserByWhereIf" resultType="com.qf.pojo.User" parameterType="com.qf.pojo.User">
select * from user
<where>
<if test="name != null and name != ''">
name = #{name}
</if>
<if test="age != null and age != ''">
and age = #{age}
</if>
</where>
</select>
3.if+set不定向修改
if+set语句:一般用于修改语句,如果第一个条件为空,则添加第二个条件。如果第一个条件不为空,第二个条件为空,则添加第一个条件,如果两个条件都不为空,则两个条件都添加语法如下:
Update tablename
<set>
<if test=” 值1!=null or值1!=’’”>
tab.列1=#{值1},
</if>
<if test=” 值2!=null or值2!=’’”>
tab.列2=#{值2},
</if>
</set>
where id = #{id}
栗子:
<update id="updateUserBySet" parameterType="UserInfo">
UPDATE user
<set>
<if test="name != null and name != ''">
name = #{name},
</if>
<if test="age != null and age != 0">
age = #{age},
</if>
</set>
where id = #{id}
</update>
4.choose-when-otherwise
choose-when-otherwise:有时候,我们不想用到所有的查询条件,只想选择其中的一个,查询条件有一个满足即可,使用 choose 标签可以解决此类问题,类似于 Java的switch语句。语法如下所示:
Select * from user
<where>
<choose>
<when test=”列1!=null and 列1!=’’”>
列1 = #{值1}
</when>
<when test=”列2!=null and 列2!=’’”>
列2 = #{值2}
</when>
<when test=”列3!=null and 列3!=’’”>
列3 = #{值3}
</when>
<otherwise>
And 列4 = #{值4}
</otherwise>
</choose>
</where>
</select>
3选1的栗子:
select * from user
<where>
<choose>
<when test="id !='' and id != null">
id=#{id}
</when>
<when test="username !='' and username != null">
and username=#{username}
</when>
<otherwise>
and sex=#{sex}
</otherwise>
</choose>
</where>
<!--这里有三个条件,id,username,sex,只能选择一个作为查询条件。比如只有id不为空,语句为select * from user where id=? -->
5.trim 语句
- trim内含属性
- prefix:加上前缀
- prefixOverrides:去掉一个
and或者or suffixOverrides=",":去掉最后一个,,也可以去掉其他东西。
语法如下:
<trim prefix="where",prefixOverrides="and | or",suffixOverrides=",">
<if test="username != null">
and username=#{username}
</if>
<if test="sex != null">
and sex=#{sex}
</if>
</trim>
栗子:用trim改修语句
<!-- 改写之前
<set>
<if test="username != null and username != ''">
username = #{username},
</if>
<if test="sex != null and sex != ''">
sex = #{sex}
</if>
</set>
-->
<!-- 改写之后 -->
<update id="updateUserById" parameterType="com.qf.pojo.User">
update user
<trim prefix="set" suffixOverrides=",">
<if test="username != null and username != ''">
username = #{username},
</if>
<if test="sex != null and sex != ''">
sex = #{sex},
</if>
</trim>
where id=#{id}
</update>
6.sql片段
使用场景:有时候可能某个sql 语句用的特别多,为了增加代码的重用性,简化代码,需要将这些代码抽取出来,然后使用时直接调用。
栗子:
<!-- 定义使用多次的sql片段-->
<sql id="updateConditions">
<if test="name != null and name != ''">
name = #{name},
</if>
<if test="age != null and age !=''">
age = #{age},
</if>
</sql>
<!-- 引用sql片段-->
<update id="updateUserByTrim" parameterType="com.qf.pojo.User">
UPDATE USER
<trim prefix="set" suffixOverrides="," suffix="where">
<!-- 引用 sql 片段,如果refid 指定的不在本文件中,那么需要在前面加上 namespace -->
<include refid="updateConditions"></include>
<!-- 在这里还可以引用其他的 sql 片段 -->
</trim>
id = #{id}
</update>
7.foreach语句
使用场景:需要一次性查询id为1,2,3,4的用户
栗子:
<select id="getUserByForeach" resultType="com.qf.pojo.User" parameterType="com.qf.vo.UserVo">
SELECT * from USER
<where>
<foreach collection="ids" item="id" separator = "or">
id = #{id}
</foreach>
</where>
</select>
<select id="selectByForeach1" resultType="UserInfo" parameterType="com.qf.vo.UserVo">
SELECT * from USER
<where>
id in
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</where>
</select>
- collection:指定输入对象中的集合属性
- item:每次遍历生成的对象
- open:开始遍历时的拼接字符串
- close:结束时拼接的字符串
- separator:遍历对象之间需要拼接的字符串
欢迎各位大佬指点!
(三)Mybatis总结之动态sql的更多相关文章
- 【mybatis深度历险系列】mybatis中的动态sql
最近一直做项目,博文很长时间没有更新了,今天抽空,学习了一下mybatis,并且总结一下.在前面的博文中,小编主要简单的介绍了mybatis中的输入和输出映射,并且通过demo简单的介绍了输入映射和输 ...
- mybatis 详解------动态SQL
mybatis 详解------动态SQL 目录 1.动态SQL:if 语句 2.动态SQL:if+where 语句 3.动态SQL:if+set 语句 4.动态SQL:choose(when,o ...
- Mybatis入门之动态sql
Mybatis入门之动态sql 通过mybatis提供的各种标签方法实现动态拼接sql. 1.if.where.sql.include标签(条件.sql片段) <sql id="sel ...
- mybatis中的动态SQL
在实际开发中,数据库的查询很难一蹴而就,我们往往要根据各种不同的场景拼接出不同的SQL语句,这无疑是一项复杂的工作,我们在使用mybatis时,mybatis给我们提供了动态SQL,可以让我们根据具体 ...
- Mybatis映射文件动态SQL语句-01
因为在很多业务逻辑复杂的项目中,往往不是简单的sql语句就能查询出来自己想要的数据,所有mybatis引入了动态sql语句, UserMapper.xml <?xml version=" ...
- mybatis教程4(动态SQL)
动态SQL语句 MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句的痛苦.例如拼接时要确保不能忘记添加必要的空 ...
- MyBatis实战之动态SQL
如果使用JDBC或者其他框架,很多时候你得根据需要去拼接SQL,这是一个麻烦的事情,而MyBatis提供对SQL语句动态的组装能力,而且它只有几个基本的元素,非常简单明了,大量的判断都可以在MyBat ...
- mybatis第二天——动态SQL与关联查询
大纲摘要: 1.输入映射和输出映射 a) 输入参数映射 b) 返回值映射 2.动态sql a) If b) Where c) Foreach d) Sql片段 3.关联查询 a) 一对一关联 b) 一 ...
- mybatis入门基础----动态SQL
原文:http://www.cnblogs.com/selene/p/4613035.html 阅读目录 一:动态SQL 二:SQL片段 三:foreach 回到顶部 一:动态SQL 1.1.定义 m ...
随机推荐
- 何为幻读?MySQL又是如何解决幻读的?
一.什么是幻读 在一次事务里面,多次查询之后,查询的结果集的个数不一致的情况叫做幻读.而多出来或者少的哪一行被叫做 幻行 二.为什么要解决幻读 在高并发数据库系统中,需要保证事务与事务之间的隔离性,还 ...
- CNN卷积神经网络的改进(15年最新paper)
回归正题,今天要跟大家分享的是一些 Convolutional Neural Networks(CNN)的工作. 大家都知道,CNN 最早提出时,是以一定的人眼生理结构为基础,然后逐渐定下来了一些经典 ...
- 【Android】应用安全——反编译
用java开发最操心的就是得到反编译,所以作为开发人员我们须要知道怎么反编译,那样才干知道怎样防止反编译.保证代码安全. 以下来看下比較经常使用的方法! 第一种方式:利用apktool反编译 1,首先 ...
- Django打造大型企业官网(四)
4.3.轮播图布局和样式 templates/news/index.html <div class="news-wrapper"> <div class=&quo ...
- Session与Cookie解析
Session和Cookie这两个对象也接触了比較长的时间了,今天就来总结一下. 首先,他们都是会话跟踪中经常使用的技术.Cookie在client记录信息来确定用户身份,Session在服务端记录信 ...
- java web项目的部署
java web项目的部署 我刚开始学着编写java web项目,着实遇到不少麻烦,感觉JAVA真难侍候,好多东西都是手动.手动. 就拿这个web项目在tomcat上的部署来说吧.我在项目的build ...
- go4--break,continue + 标签
package main /* 指针 Go虽然保留了指针,但与其它编程语言不同的是,在Go当中不 支持指针运算以及”->”运算符,而直接采用”.”选择符来操作指针 目标对象的成员 操作符”&am ...
- 【BZOJ 3211&3038】 花神游历各国 & 上帝造题的七分钟2
[题目链接] [BZOJ 3211] 点击打开链接 [BZOJ 3038] 点击打开链接 [算法] 线段树 开根操作直接开到叶子节点,注意当区间中所有数都是0或1时,不需要开根 [代码] #inclu ...
- LOJ 6089 小Y的背包计数问题 —— 前缀和优化DP
题目:https://loj.ac/problem/6089 对于 i <= √n ,设 f[i][j] 表示前 i 种,体积为 j 的方案数,那么 f[i][j] = ∑(1 <= k ...
- 【410】Linux 系统 makefile 文件
makefile 主要是用来合并编译文件 CC = gcc puzzle: puzzle.c boardADT.o $(CC) puzzle.c boardADT.o -o puzzle -lm bo ...