mybatis动态SQL语句
一 if标签
1
2
3
4
5
6
|
<select id= " getStudentListLikeName " parameterType= "StudentEntity" resultMap= "studentResultMap" > SELECT * from STUDENT_TBL ST < if test= "studentName!=null and studentName!='' " > WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT( '%' , #{studentName}), '%' ) </ if > </select> |
二 where标签
1
2
3
4
5
6
7
8
9
10
11
|
<select id= "getStudentListWhere" parameterType= "StudentEntity" resultMap= "studentResultMap" > SELECT * from STUDENT_TBL ST <where> < if test= "studentName!=null and studentName!='' " > ST.STUDENT_NAME LIKE CONCAT(CONCAT( '%' , #{studentName}), '%' ) </ if > < if test= "studentSex!= null and studentSex!= '' " > AND ST.STUDENT_SEX = #{studentSex} </ if > </where> </select> |
如果它包含的标签中有返回值的话就插入一个where。此外如果标签返回的内容是以AND或OR开头的,则它会剔除掉。
三 set 标签
使用set+if标签修改后,如果某项为null则不进行更新,而是保持数据库原值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<update id= "updateStudent" parameterType= "StudentEntity" > UPDATE STUDENT_TBL <set> < if test= "studentName!=null and studentName!='' " > STUDENT_TBL.STUDENT_NAME = #{studentName}, </ if > < if test= "studentSex!=null and studentSex!='' " > STUDENT_TBL.STUDENT_SEX = #{studentSex}, </ if > < if test= "studentBirthday!=null " > STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday}, </ if > < if test= "classEntity!=null and classEntity.classID!=null and classEntity.classID!='' " > STUDENT_TBL.CLASS_ID = #{classEntity.classID} </ if > </set> WHERE STUDENT_TBL.STUDENT_ID = #{studentID}; </update> |
四 trim标签
trim是更灵活的去处多余关键字的标签,他可以实践where和set的效果,where例子的等效trim语句
1
2
3
4
5
6
7
8
9
10
11
|
<select id= "getStudentListWhere" parameterType= "StudentEntity" resultMap= "studentResultMap" > SELECT * from STUDENT_TBL ST <trim prefix= "WHERE" prefixOverrides= "AND|OR" > < if test= "studentName!=null and studentName!='' " > ST.STUDENT_NAME LIKE CONCAT(CONCAT( '%' , #{studentName}), '%' ) </ if > < if test= "studentSex!= null and studentSex!= '' " > AND ST.STUDENT_SEX = #{studentSex} </ if > </trim> </select> |
set例子的等效trim语句
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<update id= "updateStudent" parameterType= "StudentEntity" > UPDATE STUDENT_TBL <trim prefix= "SET" suffixOverrides= "," > < if test= "studentName!=null and studentName!='' " > STUDENT_TBL.STUDENT_NAME = #{studentName}, </ if > < if test= "studentSex!=null and studentSex!='' " > STUDENT_TBL.STUDENT_SEX = #{studentSex}, </ if > < if test= "studentBirthday!=null " > STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday}, </ if > < if test= "classEntity!=null and classEntity.classID!=null and classEntity.classID!='' " > STUDENT_TBL.CLASS_ID = #{classEntity.classID} </ if > </trim> WHERE STUDENT_TBL.STUDENT_ID = #{studentID}; </update> |
五 choose (when, otherwise)
有时候并不想应用所有的条件,而只是想从多个选项中选择一个。MyBatis提供了choose 元素,按顺序判断when中的条件出否成立,如果有一个成立,则choose结束。当choose中所有when的条件都不满则时,则执行 otherwise中的sql。类似于Java 的switch语句,choose为switch,when为case,otherwise则为default。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<select id= "getStudentListChooseEntity" parameterType= "StudentEntity" resultMap= "studentResultMap" > SELECT * from STUDENT_TBL ST <where> <choose> <when test= "studentName!=null and studentName!='' " > ST.STUDENT_NAME LIKE CONCAT(CONCAT( '%' , #{studentName}), '%' ) </when> <when test= "studentSex!= null and studentSex!= '' " > AND ST.STUDENT_SEX = #{studentSex} </when> <when test= "studentBirthday!=null" > AND ST.STUDENT_BIRTHDAY = #{studentBirthday} </when> <when test= "classEntity!=null and classEntity.classID !=null and classEntity.classID!='' " > AND ST.CLASS_ID = #{classEntity.classID} </when> <otherwise></otherwise> </choose> </where> </select> |
六 foreach
对于动态SQL 非常必须的,主是要迭代一个集合,通常是用于IN 条件。List实例将使用“list”做为键,数组实例以“array”做为键。
1 参数为list实例的写法
SqlMapper.xml
1
2
3
4
5
6
7
|
<select id= "getStudentListByClassIDs" resultMap= "studentResultMap" > SELECT * FROM STUDENT_TBL ST WHERE ST.CLASS_ID IN <foreach collection= "list" item= "classList" open= "(" separator= "," close= ")" > #{classList} </foreach> </select> |
Java
1
2
3
4
5
6
7
|
List<String> classList = new ArrayList<String>(); classList.add( "20000002" ); classList.add( "20000003" ); List<StudentEntity> studentList = studentMapper.getStudentListByClassIDs(classList); for (StudentEntity entityTemp : studentList){ System.out.println(entityTemp.toString()); } |
2 参数为Array实例的写法
SqlMapper.xml
1
2
3
4
5
6
7
|
<select id= "getStudentListByClassIDs" resultMap= "studentResultMap" > SELECT * FROM STUDENT_TBL ST WHERE ST.CLASS_ID IN <foreach collection= "array" item= "ids" open= "(" separator= "," close= ")" > #{ids} </foreach> </select> |
Java
1
2
3
4
5
6
7
|
String[] ids = new String[ 2 ]; ids[ 0 ] = "20000002" ; ids[ 1 ] = "20000003" ; List<StudentEntity> studentList = studentMapper.getStudentListByClassIDs(ids); for (StudentEntity entityTemp : studentList){ System.out.println(entityTemp.toString()); } |
mybatis动态SQL语句的更多相关文章
- mybatis实战教程(mybatis in action)之八:mybatis 动态sql语句
mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类:1. if 语句 (简单的条件判断)2. c ...
- Mybatis 动态Sql语句《常用》
MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦.拼接的时候要确保不能忘了必要的空格,还要注意省掉 ...
- mybatis 动态sql语句(3)
mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类: 1. if 语句 (简单的条件判断) 2. ...
- mybatis 动态sql语句(1)
mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类: 1. if 语句 (简单的条件判断) 2. ...
- 【转】mybatis实战教程(mybatis in action)之八:mybatis 动态sql语句
转自:除非申明,文章均为一号门原创,转载请注明本文地址,谢谢! 转载地址:http://blog.csdn.net/kutejava/article/details/9164353#t5 1. if ...
- 7. mybatis实战教程(mybatis in action)之八:mybatis 动态sql语句
转自:http://www.kyjszj.com/htzq/79.html 1. if 语句 (简单的条件判断) 2. choose (when,otherwize) ,相当于java 语言中的 sw ...
- MyBatis学习总结(11)——MyBatis动态Sql语句
MyBatis中对数据库的操作,有时要带一些条件,因此动态SQL语句非常有必要,下面就主要来讲讲几个常用的动态SQL语句的语法 MyBatis中用于实现动态SQL的元素主要有: if choose(w ...
- mybatis动态sql语句学习(一)
动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦.拼接的时候要确保不能忘了必要的空格 ...
- mybatis 动态sql语句(2)
什么是动态SQL MyBatis的一个强大特性之一通常是它的动态SQL能力.如果你有使用JDBC或其他相似框架的经验,你就明白条件串联SQL字符串在一起是多么地痛苦,确保不能忘了空格或者在列表的最后的 ...
随机推荐
- js-新兴的API,最佳实践,离线应用于客户端存储
离线应用于客户端存储: 1.离线检测:online以及offline事件,都是在window对象上触发 navigator.online为true的时候是表示设备能够上网 2.使用一个描述文件(man ...
- 获取datable中某行某列的数据
假设该DataTable有5行和两个字段“Name”,“Phone”, 我如何访问第3行的“Phone”字段的值. DataTable.Rows[2][1].ToString() DataTable. ...
- iOS KVC valueForKeyPath学习心得
#import <Foundation/Foundation.h> @interface myClass1 : NSObject @property (nonatomic, copy) N ...
- Android自动化测试-Robotium(一)简介
一.Robotium原理 Robotium是一款Android自动化测试框架,主要针对Android平台的应用进行黑盒自动化测试,它提供了模拟各种手势操作(点击.长按.滑动等).查找和断言机制的API ...
- Java NIO示例:多人网络聊天室
一个多客户端聊天室,支持多客户端聊天,有如下功能: 功能1: 客户端通过Java NIO连接到服务端,支持多客户端的连接 功能2:客户端初次连接时,服务端提示输入昵称,如果昵称已经有人使用,提示重新输 ...
- three.js光源
在Threejs中,光源用Light表示,它是所有光源的基类.它的构造函数是: THREE.Light ( hex ) 它有一个参数hex,接受一个16进制的颜色值.例如要定义一种红色的光源,我们可以 ...
- Apache ActiveMQの版本更迭和Apache ActiveMQの故障转移
本文描述apache activemq 版本更迭的原因以及Apache ActiveMQのThe Failover Transport new features in 5.2.0 1.对信息的传输/ ...
- 【原】iOS学习46之第三方CocoaPods的安装和使用(通用方法)
本文主要说明CocoaPods的安装步骤.使用说明和常见的报错即解决方法. 1. CocoaPods 1> CocoaPods简介 CocoaPods是一个用来帮助我们管理第三方依赖库的工具. ...
- 163源报错Hash Sum mismatch 解决方法
Ubuntu server 用的163的源,报错: W: Failed to fetch http://mirrors.163.com/ubuntu/dists/precise-updates/mai ...
- 解决iPhone中overflow:scroll;滑动速度慢或者卡的问题
在移动端html中经常出现横向/纵向滚动的效果,但是在iPhone中滚动速度很慢,感觉不流畅,有种卡卡的感觉,但是在安卓设备上没有这种感觉; 要解决这个问题很简单: 一行代码搞定 -webkit-ov ...