Mybaits动态Sql
什么是动态SQL?
MyBatis的强大之处便是它的动态SQL,如果你使用JDBC那么在根据不同条件查询时,拼接SQL语句是多么的痛苦。 比如查询一个学生信息,可以根据学生的姓名,性别,班级,年龄,学历等信息来查询,并且多个条件可以任意组合。 而MyBatis中集成了非常强大的 OGNL表达式,可以帮助我们解决这个问题。
if标签
if标签作为mybatis动态sql中的条件判断,它可以判断你是否传入某个字段的值以决定你是否执行这条sql语句。
<select id="selectIf" parameterType="Map" resultMap="Employee">
select * from tal_employee where 1=1
<if test="lastName!=null and lastName!=''">
and last_name=#{lastName},
</if>
<if test="email!=null and email!=''">
and email=#{email},
</if>
<if test="gender!=null and gender!=''">
and gender=#{gender},
</if>
</select>
使用IF标签可以完成我们的动态sql语句并且,我们很多时候可能判断这个的时候却又不判断那个,我们只好分成几个方法写,有了这个if标签我们就可以很好根据业务需求完成自己的语句拼接合并成一个方法。
where标签
where标签可以替代if和choose标签,不需要添加多余的where 1=1条件。当where中的条件没有一个满足时,不输出where关键字。如果条件满足,会去掉第一个开头的and或者or。
<select id="findAll" resultMap="Employee" parameterType="Map">
select * from tal_employee
<!-- 使用where会去除前面 and/or -->
<where>
<if test="lastName!=null and lastName!=''">
and last_name=#{lastName}
</if>
<if test="email!=null and email!=''">
and email=#{email}
</if>
<if test="gender!=null and gender!=''">
and gender=#{gender}
</if>
</where>
</select>
choose标签
有些时候,我们不想用到所有的条件语句,而只想从中择其一二。 choose标签为Mybatis中的提供多重条件判断,使用when和otherwise标签进行条件判断,多个条件中只有一个被执行。
<select id="chooseTest" parameterType="Map" resultMap="Employee">
select * from tal_employee
<choose>
<when test="lastName != null and lastName != ''">
and last_name like concat('%',#{lastName},'%')
</when>
<when test="gender != null and gender != ''">
and gender = #{gender}
</when>
<!-- 如果上面两个条件不成立,那么会选择otherwise标签相当于ifelse -->
<otherwise>
and id = #{id}
</otherwise>
</choose>
</select>
set标签
set标签主要用于在update更新值时,动态添加更新的列,如果列没有值则不添加。避免使用多余的,号。
<update id="update" parameterType="Map">
update tal_employee
<!-- 使用set会去除末尾逗号 -->
<set>
<if test="lastName!=null and lastName!=''">
last_name=#{lastName},
</if>
<if test="email!=null and email!=''">
email=#{email},
</if>
<if test="gender!=null and gender!=''">
gender=#{gender},
</if>
</set>
where id = #{id}
</update>
有了set这个标签就想指定更新那个字段就是那个字段,高端大气上档次。
trim标签
where默认是将第一个的and或者or去掉,set是将更新中的,去掉,如果用户想自定义一些规则,则需要使用另外一个自定义标签<trim>。

<!-- 替代set标签 -->
<trim prefix="set" suffixOverrides=",">
<if test="lastName!= null"></if>
</trim>
<!-- 替代where标签 -->
<trim prefix="where" prefixOverrides="and|or">
<if test="lastName!= null"></if>
</trim>
foreach标签
foreach标签这个标签是用来迭代我们的集合对象,它支持list,array,map。它有属性:
collection:循环集合或指定类型
item:每次迭代的结果
separator:设置分隔符
open:开始符号(前缀)可选
close:结束符号(后缀)可选
index:list和数据的序号,可选
<select id="manyID" parameterType="List" resultMap="Employee">
select * from tal_employee where id
in
<foreach collection="list" item="id" open="(" close=")"
separator=",">
#{id}
</foreach>
</select>
SQL标签
用来定义常量,通过include来引用
<sql id="select">
select * from tal_employee
</sql>
<select id="findAll" resultMap="Employee">
<!--select * from tal_employee -->
<!-- 引入已经定义好的sql -->
<include refid="select"></include>
</select>
Mybaits动态Sql的更多相关文章
- JAVA框架 Mybaits 动态sql
动态sql 一:if标签使用: 我们在查询的时候,有时候由于查询的条件的不确定性,导致where的后面的条件的不同,这时候就需要我们进行where后面的条件进行拼接. Mapper配置文件: < ...
- mybaits动态SQL中的DECIMAL
数据库:mysql数据库字段类型:decimal(11,2)java程序类型:java.math.BigDecimal 使用mybatis的动态语句 <if test ="money! ...
- MyBaits动态sql语句
1 在接口中书写方法 public interface EmployeeMapperDynamicSQL { public List<Employee> getEmpsTestInnerP ...
- mybatis实战教程(mybatis in action)之八:mybatis 动态sql语句
mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类:1. if 语句 (简单的条件判断)2. c ...
- [刘阳Java]_MyBatis_动态SQL标签用法_第7讲
1.MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. 2.MyBatis中用于实现动态SQL的元素主要有 if choose(when,otherwi ...
- Mybatis解析动态sql原理分析
前言 废话不多说,直接进入文章. 我们在使用mybatis的时候,会在xml中编写sql语句. 比如这段动态sql代码: <update id="update" parame ...
- 【转】mybatis实战教程(mybatis in action)之八:mybatis 动态sql语句
转自:除非申明,文章均为一号门原创,转载请注明本文地址,谢谢! 转载地址:http://blog.csdn.net/kutejava/article/details/9164353#t5 1. if ...
- 自己动手实现mybatis动态sql
发现要坚持写博客真的是一件很困难的事情,各种原因都会导致顾不上博客.本来打算写自己动手实现orm,看看时间,还是先实现一个动态sql,下次有时间再补上orm完整的实现吧. 用过mybatis的人,估计 ...
- mybatis 动态sql和参数
mybatis 动态sql 名词解析 OGNL表达式 OGNL,全称为Object-Graph Navigation Language,它是一个功能强大的表达式语言,用来获取和设置Java对象的属性, ...
随机推荐
- mysql查询反斜杠字符串问题
马上上线发现一个问题,太坑了 写一个查询语句,明明数据库中有,但是就是查不到,后来发现是反斜杠的问题 比如 数据库中有一个字段名称为 name 存储的值为 “海尔厨电\洗碗机” 当我使用如下sql查 ...
- 【blog】Hibernate5如何设置SQLite的方言(待更新...)
参考链接 Hibernate3.Hibernate4.Hibernate5 hibernate5连接sqlite (目前参考的是这个方法)
- 【tmos】spring data jpa 创建方法名进行简单查询
参考链接 spring data jpa 创建方法名进行简单查询:http://www.cnblogs.com/toSeeMyDream/p/6170790.html
- no plugin found for prefix 'tomcat 7' in the current project
使用maven build编译出错 “no plugin found for prefix 'tomcat 7' in the current project..........” 参照下面方法 ht ...
- Springboot(一)概念
一.概念 Spring boot 用来简化新Spring应用的初始搭建和开发配置. 二.特性 1.创建独立的Spring应用程序,不是对spring功能增强,而是提快速使用Spring的功能: 2.嵌 ...
- Vue获取事件源
设置事件源 <label :data-weight="item1.EvaluateWeight" @click='radioClick' :data-id="ite ...
- Linux内存分配小结--malloc、brk、mmap【转】
转自:https://blog.csdn.net/gfgdsg/article/details/42709943 http://blog.163.com/xychenbaihu@yeah/blog/s ...
- ppp 完全理解(二)【转】
转自:https://blog.csdn.net/tianruxishui/article/details/44057717 ppp 完全理解(二) pppd 协议及代码分析 作者:李圳均 日期:20 ...
- LaTeX Error: Something's wrong--perhaps a missing \item
使用Latex 引用参考文献,.bib文件是个很好的助手,创建后 1.第一步点击Latex编译,可以获得*.aux文件.*.dvi文件.*.log文件以及*.gz文件: 2.第二步点击Bibtex编译 ...
- Git学习笔记02-创建版本库
版本库就是一个目录,这个目录里面的所有文件都会被Git管理,每个文件的修改,删除都能追踪.以便在某个时刻追踪历史记录,或者还原 路径切换,查看文件命令和linux差不多,cd 文件路径 ls查看路径 ...