1 / 10
文档名称:

mybatis动态sql解析.doc

格式:doc   大小:94KB   页数:10页
下载后只包含 1 个 DOC 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

mybatis动态sql解析.doc

上传人:guoxiachuanyue006 2021/8/14 文件大小:94 KB

下载得到文件列表

mybatis动态sql解析.doc

相关文档

文档介绍

文档介绍:: .
1.
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句 中实现某些逻辑。
MyBatis中用于实现动态SQL的元素主要有:
if
choose (when, otherwise〉 where
• foreach
if就是简单的条件判断,利用辻语句我们可以实现某些简单的条件选择。先来 看如下一个例子:
Xml代码 ☆
1・ vselect id=,,dynamiclfTestH parameterType=,,Blogn resultType=,,Blog,,>
2. select * from t_blog where 11 = 1
3. <if test=,,title != null"〉
4. and title = #{title}
5. </if>
6. vif test=”content != null”〉
7. and content = #{content}
8. </if>
9. <if test="owner != nullH>
10. and owner = #{owner}
11. </if>
12. </select>
这条语句的意思非常简单,如果你提供了 title参数,那么就要满足
title二#{title},同样如果你提供了 Content和Owner的时候,它们也需要满 足相应的条件,之后就是返回满足这些条件的所有Blog,这是非常有用的一个 功能,以往我们使用其他类型框架或者直接使用JDBC的时候,如果我们要达 到同样的选择效果的时候,我们就需要拼SQL语句,这是极其麻烦的,比起来, 上述的动态SQL就要简单多了。
choose元素的作用就相'"l于JAVA中的switch语句,基本上跟JSTL中的choose 的作用和用法是一样的,通常都是与when和otherwise搭配的。看如下一个例 子:
Xml代码 4
1・ vselect id=,ldynamicChooseTest" parameterType=,,Blog,1 resultType=,,Blog,l>
2. select * from t_blog where 11 = 1
3. vchoose〉
4. <when test=,,title != nuHH>
5. and title = #{title}
6. </when>
7. vwhe n test="c on tent != null1、
8. and content = #{content}
9. </when>
10. votherwise>
and owner = "ownerl"
12 ・ </otherwise>
13 ・ v/choose>
14. </select>
when元素表示当when中的条件满足的时候就输出其中的内容,跟JAVA中的 switch效果差不多的是按照条件的顺序,当when中有条件满足的时候,就会 跳出choose,即所有的when和otherwise条件中,只有