什么是动态SQL

MyBatis的一个强大特性之一通常是它的动态SQL能力。如果你有使用JDBC或其他相似框架的经验,你就明白条件串联SQL字符串在一起是多么地痛苦,确保不能忘了空格或者在列表的最后的省略逗号,动态SQL可以彻底处理这种痛苦。

通常使用动态SQL不可能是独立的一部分,MyBatis当然使用一种强大的动态SQL语言来改进这种情形,这种语言可以被用在任意映射的SQL语句中。

动态SQL元素和使用JSTL或其它相似的基于XML的文本处理器相似,在MyBatis之前的版本中,有很多元素需要了解,MyBatis3大大地提升了它们,现在用不到原先一半的元素就能工作了,MyBatis采用功能强大的基于OGNL的表达式来消除其他元素。

OK,介绍就到这儿,下面来进入动态SQL的学习吧。

if

在动态SQL中所做的最通用的事情就是包含部分where子句的条件,比如:

<select id="selectInCondition" parameterType="student" resultType="student">
select * from student where studentId > #{studentId}
<if test="studentName != null">
and studentName = #{studentName};
</if>
</select> 具体实现不写了,那么如果我这么调用: List<Student> list = StudentOperator.getInstance().selectInCondition(0, "Jack", 0, null); 查询的就是studentId>0且studentName=”Jack”的所有学生信息,如果换一种调用方式: List<Student> list = StudentOperator.getInstance().selectInCondition(0, null, 0, null); 那么查询的就是studentId>0的所有学生信息。 多个where子句也是一样的,比如: <select id="selectInCondition" parameterType="student" resultType="student">
<![CDATA[
select * from student where studentId > #{studentId}
]]>
<if test="studentName != null and studentName != 'Jack' ">
and studentName = #{studentName}
</if>
<if test="studentAge != 0">
and studentAge = #{studentAge};
</if>
</select> 注意一下,能用”<![CDATA[ ... ]]>”尽量还是用,不过只包动态SQL外的内容。 另外,test里面可以判断字符串、整型、浮点型,大胆地写判断条件吧。如果属性是复合类型,则可以使用A.B的方式去获取复合类型中的属性来进行比较。 choose、when、otherwise 有时候我们不想应用所有的应用条件,相反我们想选择很多情况下的一种。和Java中的switch…case…类似,MyBasit提供choose元素。 上面的例子是两种if判断都可能存在,接下来使用choose、when、other做一些修改: <select id="selectInCondition" parameterType="student" resultType="student">
<![CDATA[
select * from student where studentId > #{studentId}
]]>
<choose>
<when test="studentName != null">
and studentName = #{studentName};
</when>
<when test="studentAge != 0">
and studentAge = #{studentAge};
</when>
<otherwise>
or 1 = 1;
</otherwise>
</choose>
</select> 两个when只能满足一个,都不满足则走other。还是注意一下这里的”<![CDATA[ ... ]]>”,不可以包围整个语句。 trim、where、set 第一个例子已经示例了if的用法,但是这种用法有个缺陷—-动态SQL外必须有where子句。 什么意思,因为很多时候我们需要where后面的子句都动态生成,而不是事先有一个where,这样就有问题,比如说: <select id="selectInCondition" parameterType="student" resultType="student">
<![CDATA[
select * from student where
]]>
<if test="studentName != null and studentName != 'Jack' ">
and studentName = #{studentName}
</if>
<if test="studentAge != 0">
and studentAge = #{studentAge};
</if>
</select> 如果所有条件都不匹配,那么生成的SQL语句将是: select * from student where 这将导致查询失败。即使只满足一个查询条件还是有问题,比如满足studentName那个吧,生成的SQL语句将是: select * from student where and studentName = #{studentName}; 这个查询也会失败。 解决办法也有,一个讨巧的办法是用where 1 = 1的方式,即: <select id="selectInCondition" parameterType="student" resultType="student">
<![CDATA[
select * from student where 1 = 1
]]>
<if test="studentName != null and studentName != 'Jack' ">
and studentName = #{studentName}
</if>
<if test="studentAge != 0">
and studentAge = #{studentAge};
</if>
</select> 因为”1 = 1″永远满足,所以相当于给where加了一层true而已,此时动态SQL生成什么where判断条件就是什么。 另外一个解决办法是利用MyBatis中的一个简单处理方式,这在90%情况下都会有用而且。而在不能使用的地方,可以以自定义方式处理。加上一个简单的改变,所有的事情都会顺利进行: <select id="selectInCondition" parameterType="student" resultType="student">
<![CDATA[
select * from student
]]>
<where>
<if test="studentName != null and studentName != 'Jack' ">
and studentName = #{studentName}
</if>
<if test="studentAge != 0">
and studentAge = #{studentAge};
</if>
</where>
</select> where元素知道如果由被包含的标记返回任意内容,就仅仅插入where。而且,如果以”and”或”or”开头的内容,那么就会跳过where不插入。 如果where元素没有做出你想要的,那么可以使用trim元素来自定义。比如,和where元素相等的trim元素是: <trim prefix="WHERE" prefixOverrides="AND |OR ">

</trim> 即: <select id="selectInCondition" parameterType="student" resultType="student">
select * from student
<trim prefix="WHERE" prefixOverrides="AND |OR ">
<if test="studentName != null and studentName != 'Jack' ">
and studentName = #{studentName}
</if>
<if test="studentAge != 0">
and studentAge = #{studentAge};
</if>
</trim>
</select> 特别要注意,prefixOverrides中的空白也是很重要的。 最后一个小内容,和动态更新语句相似的解决方案是set。set元素可以被用于动态包含更新的列,而不包含不需要更新的。比如: <update id="updateStudentAgeById" parameterType="Student">
<!--update student set studentAge = #{studentAge} where
studentId = #{studentId}; -->
<![CDATA[
update student
]]>
<set>
<if test="studentAge != 0">studentAge = #{studentAge}</if>
</set>
where studentId = #{studentId}
</update> 可以对比一下,注释掉的是原update语句,没有注释的是加入动态SQL之后的语句。 这里,set元素会动态前置set关键字,而且也会消除任意无关的逗号。如果你对和这里对等的trim元素好奇,它看起来是这样的: <trim prefix="SET" prefixOverrides=",">

</trim> 这种时候我们附加一个后缀,同时也附加一个前缀。 foreach 另外一个动态SQL通用的必要操作时迭代一个集合,通常是构建在in条件中的。比如(上面的例子都是我在自己电脑上跑通过的例子,这个例子就直接复制MyBatis官方文档上的内容了): <select id="selectPostIn" resultType="domain.blog.Post">
<![CDATA[
SELECT * FROM POST P WHERE ID in
]]>
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</select> foreach是非常强大的,它允许你指定一个集合,声明集合项和索引变量,它们可以用在元素体内。他也允许你指定开放和关闭字符串,在迭代之间放置分隔符。这个元素是很智能的,它不会偶然地附加多余的分隔符。

  

什么是动态SQL

MyBatis的一个强大特性之一通常是它的动态SQL能力。如果你有使用JDBC或其他相似框架的经验,你就明白条件串联SQL字符串在一起是多么地痛苦,确保不能忘了空格或者在列表的最后的省略逗号,动态SQL可以彻底处理这种痛苦。

通常使用动态SQL不可能是独立的一部分,MyBatis当然使用一种强大的动态SQL语言来改进这种情形,这种语言可以被用在任意映射的SQL语句中。

动态SQL元素和使用JSTL或其它相似的基于XML的文本处理器相似,在MyBatis之前的版本中,有很多元素需要了解,MyBatis3大大地提升了它们,现在用不到原先一半的元素就能工作了,MyBatis采用功能强大的基于OGNL的表达式来消除其他元素。

OK,介绍就到这儿,下面来进入动态SQL的学习吧。

if

在动态SQL中所做的最通用的事情就是包含部分where子句的条件,比如:

<select id="selectInCondition" parameterType="student" resultType="student">

select * from student where studentId > #{studentId}

<if test="studentName != null">

and studentName = #{studentName};

</if>

</select>

具体实现不写了,那么如果我这么调用:

List<Student> list = StudentOperator.getInstance().selectInCondition(0, "Jack", 0, null);

查询的就是studentId>0且studentName=”Jack”的所有学生信息,如果换一种调用方式:

List<Student> list = StudentOperator.getInstance().selectInCondition(0, null, 0, null);

那么查询的就是studentId>0的所有学生信息。

多个where子句也是一样的,比如:

<select id="selectInCondition" parameterType="student" resultType="student">

<![CDATA[

select * from student where studentId > #{studentId}

]]>

<if test="studentName != null and studentName != 'Jack' ">

and studentName = #{studentName}

</if>

<if test="studentAge != 0">

and studentAge = #{studentAge};

</if>

</select>

注意一下,能用”<![CDATA[ ... ]]>”尽量还是用,不过只包动态SQL外的内容。

另外,test里面可以判断字符串、整型、浮点型,大胆地写判断条件吧。如果属性是复合类型,则可以使用A.B的方式去获取复合类型中的属性来进行比较。

choose、when、otherwise

有时候我们不想应用所有的应用条件,相反我们想选择很多情况下的一种。和Java中的switch…case…类似,MyBasit提供choose元素。

上面的例子是两种if判断都可能存在,接下来使用choose、when、other做一些修改:

<select id="selectInCondition" parameterType="student" resultType="student">

<![CDATA[

select * from student where studentId > #{studentId}

]]>

<choose>

<when test="studentName != null">

and studentName = #{studentName};

</when>

<when test="studentAge != 0">

and studentAge = #{studentAge};

</when>

<otherwise>

or 1 = 1;

</otherwise>

</choose>

</select>

两个when只能满足一个,都不满足则走other。还是注意一下这里的”<![CDATA[ ... ]]>”,不可以包围整个语句。

trim、where、set

第一个例子已经示例了if的用法,但是这种用法有个缺陷—-动态SQL外必须有where子句。

什么意思,因为很多时候我们需要where后面的子句都动态生成,而不是事先有一个where,这样就有问题,比如说:

<select id="selectInCondition" parameterType="student" resultType="student">

<![CDATA[

select * from student where

]]>

<if test="studentName != null and studentName != 'Jack' ">

and studentName = #{studentName}

</if>

<if test="studentAge != 0">

and studentAge = #{studentAge};

</if>

</select>

如果所有条件都不匹配,那么生成的SQL语句将是:

select * from student where

这将导致查询失败。即使只满足一个查询条件还是有问题,比如满足studentName那个吧,生成的SQL语句将是:

select * from student where and studentName = #{studentName};

这个查询也会失败。

解决办法也有,一个讨巧的办法是用where 1 = 1的方式,即:

<select id="selectInCondition" parameterType="student" resultType="student">

<![CDATA[

select * from student where 1 = 1

]]>

<if test="studentName != null and studentName != 'Jack' ">

and studentName = #{studentName}

</if>

<if test="studentAge != 0">

and studentAge = #{studentAge};

</if>

</select>

因为”1 = 1″永远满足,所以相当于给where加了一层true而已,此时动态SQL生成什么where判断条件就是什么。

另外一个解决办法是利用MyBatis中的一个简单处理方式,这在90%情况下都会有用而且。而在不能使用的地方,可以以自定义方式处理。加上一个简单的改变,所有的事情都会顺利进行:

<select id="selectInCondition" parameterType="student" resultType="student">

<![CDATA[

select * from student

]]>

<where>

<if test="studentName != null and studentName != 'Jack' ">

and studentName = #{studentName}

</if>

<if test="studentAge != 0">

and studentAge = #{studentAge};

</if>

</where>

</select>

where元素知道如果由被包含的标记返回任意内容,就仅仅插入where。而且,如果以”and”或”or”开头的内容,那么就会跳过where不插入。

如果where元素没有做出你想要的,那么可以使用trim元素来自定义。比如,和where元素相等的trim元素是:

<trim prefix="WHERE" prefixOverrides="AND |OR ">

</trim>

即:

<select id="selectInCondition" parameterType="student" resultType="student">

select * from student

<trim prefix="WHERE" prefixOverrides="AND |OR ">

<if test="studentName != null and studentName != 'Jack' ">

and studentName = #{studentName}

</if>

<if test="studentAge != 0">

and studentAge = #{studentAge};

</if>

</trim>

</select>

特别要注意,prefixOverrides中的空白也是很重要的。

最后一个小内容,和动态更新语句相似的解决方案是set。set元素可以被用于动态包含更新的列,而不包含不需要更新的。比如:

<update id="updateStudentAgeById" parameterType="Student">

<!--update student set studentAge = #{studentAge} where

studentId = #{studentId}; -->

<![CDATA[

update student

]]>

<set>

<if test="studentAge != 0">studentAge = #{studentAge}</if>

</set>

where studentId = #{studentId}

</update>

可以对比一下,注释掉的是原update语句,没有注释的是加入动态SQL之后的语句。

这里,set元素会动态前置set关键字,而且也会消除任意无关的逗号。如果你对和这里对等的trim元素好奇,它看起来是这样的:

<trim prefix="SET" prefixOverrides=",">

</trim>

这种时候我们附加一个后缀,同时也附加一个前缀。

foreach

另外一个动态SQL通用的必要操作时迭代一个集合,通常是构建在in条件中的。比如(上面的例子都是我在自己电脑上跑通过的例子,这个例子就直接复制MyBatis官方文档上的内容了):

<select id="selectPostIn" resultType="domain.blog.Post">

<![CDATA[

SELECT * FROM POST P WHERE ID in

]]>

<foreach item="item" index="index" collection="list"

open="(" separator="," close=")">

#{item}

</foreach>

</select>

foreach是非常强大的,它允许你指定一个集合,声明集合项和索引变量,它们可以用在元素体内。他也允许你指定开放和关闭字符串,在迭代之间放置分隔符。这个元素是很智能的,它不会偶然地附加多余的分隔符。

mybatis 动态sql语句(2)的更多相关文章

  1. mybatis实战教程(mybatis in action)之八:mybatis 动态sql语句

    mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类:1. if 语句 (简单的条件判断)2. c ...

  2. Mybatis 动态Sql语句《常用》

    MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦.拼接的时候要确保不能忘了必要的空格,还要注意省掉 ...

  3. mybatis 动态sql语句(3)

    mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类: 1. if 语句 (简单的条件判断) 2. ...

  4. mybatis 动态sql语句(1)

    mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类: 1. if 语句 (简单的条件判断) 2. ...

  5. 【转】mybatis实战教程(mybatis in action)之八:mybatis 动态sql语句

    转自:除非申明,文章均为一号门原创,转载请注明本文地址,谢谢! 转载地址:http://blog.csdn.net/kutejava/article/details/9164353#t5 1. if ...

  6. 7. mybatis实战教程(mybatis in action)之八:mybatis 动态sql语句

    转自:http://www.kyjszj.com/htzq/79.html 1. if 语句 (简单的条件判断) 2. choose (when,otherwize) ,相当于java 语言中的 sw ...

  7. MyBatis学习总结(11)——MyBatis动态Sql语句

    MyBatis中对数据库的操作,有时要带一些条件,因此动态SQL语句非常有必要,下面就主要来讲讲几个常用的动态SQL语句的语法 MyBatis中用于实现动态SQL的元素主要有: if choose(w ...

  8. mybatis动态SQL语句

    一 if标签 ? 1 2 3 4 5 6 <select id=" getStudentListLikeName " parameterType="StudentE ...

  9. mybatis动态sql语句学习(一)

    动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦.拼接的时候要确保不能忘了必要的空格 ...

随机推荐

  1. shell awk使用(转)

    #####awk  " 对每一行操作的代码块"  操作的文件  awk -F:   '{print $1,$2,$3,$4,$5}' helloworld.sh      打印字段 ...

  2. python协程函数应用 列表生成式 生成器表达式

    协程函数应用 列表生成式 生成器表达式   一.知识点整理: 1.可迭代的:对象下有_iter_方法的都是可迭代的对象 迭代器:对象._iter_()得到的结果就是迭代器 迭代器的特性: 迭代器._n ...

  3. 《The Cg Tutorial》阅读笔记——动画 Animation

    这段时间阅读了英文版的NVidia官方的<The Cg Tutorial>,借此来学习基本的图形学知识和着色器编程. 在此做一个阅读笔记. 本文为大便一箩筐的原创内容,转载请注明出处,谢谢 ...

  4. ComBox绑定枚举

    (转自:http://blog.csdn.net/chao88552828/article/details/9903159) /// <summary> /// 参数枚举 /// < ...

  5. C# WinForm开发系列之c# 通过.net自带的chart控件绘制饼图,柱形图和折线图的基础使用和扩展

    一.需要实现的目标是: 1.将数据绑定到pie的后台数据中,自动生成饼图. 2.生成的饼图有详细文字的说明. 1.设置chart1的属性Legends中默认的Legend1的Enable为false: ...

  6. python考试

    py4测试题 1.8<<2等于?322.通过内置函数计算5除以2的余数 divmod(5,2)3.s=[1,"h",2,"e",[1,2,3],&q ...

  7. LeetCode OJ :Unique Binary Search Trees II(唯一二叉搜索树)

    题目如下所示:返回的结果是一个Node的Vector: Given n, generate all structurally unique BST's (binary search trees) th ...

  8. sublime上配置markdown

    等等等等 简书一个不错的教程:Sublime Text3的Markdown配置 补充说明:第一步可以直接找 Tools-->install package control. ^.^ ...

  9. 【转载】VC获取MAC地址的4种方法

    From:http://blog.csdn.net/pdfmaker/article/details/465748 有需求才有创造,有了问题才会想着去解决,那么我这里的获取MAC地址的第4种方法也是在 ...

  10. 【ci框架学习】环境搭建

    系统 -- Ubuntu 14.0(虚拟机linux 实体机Windows) 目标环境 -- lnmp 附加内容: 1.目录共享(方便代码编写) 2.使用secure crt终端软件连接(便于操作,不 ...