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字符串在一起是多么地痛苦,确保不能忘了空格或者在列表的最后的 ...
随机推荐
- Markdown语言.md文件
转自:http://www.kuqin.com/shuoit/20141125/343459.html 之前一直在使用github,也在上面分享了不少的项目和Demo,每次创建新项目的时候,使用的都是 ...
- Oracle常用命令
查看数据库锁的情况 SELECT object_name, machine, s.sid, s.serial# FROM gv$locked_object l, dba_objects o, gv$ ...
- Spring的Lifecycle
Lifecycle接口定义了每个对象的重要方法,每个对象都有自己的生命周期需求,如下: public interface Lifecycle { void start(); void stop(); ...
- 门店 车销 批发送货 商超 快销专用扫描打印开单手持PDA移动销售管理系统
门店 车销 批发送货 商超 快销专用扫描打印开单手持PDA移动销售管理系统的详细介绍 一. 以PDA等移动终端为媒介,随时随地掌握门店信息. 二. 后台集成了数据统计.多指标分析.销售.库存.会员管理 ...
- css之overflow:细探之下有意想不到的结果
overflow 是一个非常常用的 CSS 属性,一般来说会认为很简单,其实细究之后就会发现他还有很多小特性或者说意想不到的结果: 下面就介绍下(在浏览器环境下)关于 overflow 的小总结. 哪 ...
- delphi override、overload、reintroduce的区别-0613.txt
http://blog.csdn.net/honglixx/article/details/3624934 1.override overload reintroduce的中文叫法是什么? overr ...
- 【Clr in c#】方法
1. 引用类型(class)与值类型(strust)的构造函数(实例构造器) 1, 创建一个引用类型的实例时,首先为实例的数据字段分配内存,然后初始对象的附加字段,最后调用实例构造器来设置对象的初始 ...
- javaScript封装的各种写法
在javascript的世界里,写法是个神奇的现象,真是百家齐开放啊!每次看到老外写的js组件,思想和写法都怪异,就没看到一个js结构基本相同的代码出来.今天,我就来谈谈js写法,我在开发过程中,也写 ...
- jquerymobile页面跳转和参数传递
http://blog.csdn.net/chen052210123/article/details/7481578 页面跳转: 页面跳转时pagebeforechange事件会被触发两次,通过$(d ...
- 数据结构:后缀自动机 WJMZBMR讲稿的整理和注释
链接放在这里,有点难理解,至少我个人是的. 后缀自动机是一种有限状态自动机,其功能是识别字符串是否是母串的后缀.它能解决的问题当然不仅仅是判断是不是后缀这种事,跟字符串的连续子串有关的问题都可以往这个 ...