id="BAIDU_DUP_fp_iframe" src="//pos.baidu.com/wh/o.htm?ltr=" style="width: 0px; height: 0px; display: none; visibility: hidden;">
关闭
分类:
作者同类文章
X
版权声明:本文为博主原创文章,未经博主允许不得转载。
动态SQL
MyBatis的动态SQL,解决了SQL字符串拼接的痛苦。
1.if
这条一句会提供一个可选的文本查找功能。如果没有传递title,那么所有激活的博客都会被返回。
如果传递了title,那么就会查找相近的title。
2.choose,when,otherwise
注:如果上述条件都没有匹配,则会变成SELECT * FROM BLOG WHERE
如果仅有第二个匹配,则会变成SELECT * FROM BLOG WHERE AND title LIKE somelike
显然这样会查询失败。要解决这个问题,mybatis提供了解决方法。
overrides属性采用管道文本分隔符来覆盖,这里的空白是重要的。它的结果就是移除在InnerText中overrides中指定的内容。
3.set
同上的问题,优化后:
MyBatis的动态SQL,解决了SQL字符串拼接的痛苦。
1.if
- <select id="findActiveBlogWithTitleLike"
- parameterType="Blog" resultType="Blog">
- SELECT * FROM BLOG
- WHERE state = 'ACTIVE'
- <if test="title != null">
- AND title like #{title}
- </if>
- </select>
<select id="findActiveBlogWithTitleLike"
parameterType="Blog" resultType="Blog">
SELECT * FROM BLOG
WHERE state = 'ACTIVE'
<if test="title != null">
AND title like #{title}
</if>
</select>
这条一句会提供一个可选的文本查找功能。如果没有传递title,那么所有激活的博客都会被返回。
如果传递了title,那么就会查找相近的title。
2.choose,when,otherwise
- <select id="findActiveBlogLike"
- parameterType="BLOG" resultType="BLOG">
- SELECT * FROM BLOG
- WHERE
- <choose>
- <when test="title != null">
- AND title like #{title}
- </when>
- <when test="author != null and author.name != null">
- AND title like #{author.name}
- </when>
- <otherwise>
- AND featured = 1
- </otherwise>
- </choose>
- </select>
<select id="findActiveBlogLike"
parameterType="BLOG" resultType="BLOG">
SELECT * FROM BLOG
WHERE
<choose>
<when test="title != null">
AND title like #{title}
</when>
<when test="author != null and author.name != null">
AND title like #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</select>
注:如果上述条件都没有匹配,则会变成SELECT * FROM BLOG WHERE
如果仅有第二个匹配,则会变成SELECT * FROM BLOG WHERE AND title LIKE somelike
显然这样会查询失败。要解决这个问题,mybatis提供了解决方法。
- <select id="findActiveBlogLike"
- parameterType="BLOG" resultType="BLOG">
- SELECT * FROM BLOG
- WHERE
- <trim prefix="WHERE" prefixOverrides="AND |OR ">
- <choose>
- <when test="title != null">
- AND title like #{title}
- </when>
- <when test="author != null and author.name != null">
- AND title like #{author.name}
- </when>
- <otherwise>
- AND featured = 1
- </otherwise>
- </choose>
- </trim>
- </select>
<select id="findActiveBlogLike"
parameterType="BLOG" resultType="BLOG">
SELECT * FROM BLOG
WHERE
<trim prefix="WHERE" prefixOverrides="AND |OR ">
<choose>
<when test="title != null">
AND title like #{title}
</when>
<when test="author != null and author.name != null">
AND title like #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</trim>
</select>
overrides属性采用管道文本分隔符来覆盖,这里的空白是重要的。它的结果就是移除在InnerText中overrides中指定的内容。
3.set
- <update id="updateAuthorIfNecessary"
- parameterType="Author">
- update Author
- <set>
- <if test="username != null">username=#{username},</if>
- <if test="password != null">password=#{password},</if>
- <if test="email != null">email=#{email}</if>
- </set>
- where id=#{id}
- </update>
<update id="updateAuthorIfNecessary"
parameterType="Author">
update Author
<set>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
<if test="email != null">email=#{email}</if>
</set>
where id=#{id}
</update>
同上的问题,优化后:
- <update id="updateAuthorIfNecessary"
- parameterType="Author">
- update Author
- <trim prefix="where" prefixOverrides=",">
- <set>
- <if test="username != null">username=#{username},</if>
- <if test="password != null">password=#{password},</if>
- <if test="email != null">email=#{email}</if>
- </set>
- where id=#{id}
- </trim>
- </update>
<update id="updateAuthorIfNecessary"
parameterType="Author">
update Author
<trim prefix="where" prefixOverrides=",">
<set>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
<if test="email != null">email=#{email}</if>
</set>
where id=#{id}
</trim>
</update>
-
顶
- 0
-
踩
- 0
- 上一篇freemarker应用
- 下一篇织梦数据库设计规则文档
我的同类文章
- •Java自定义注解2012-03-03
- •GridBagLayout参数说明2011-09-19
- •swing 模仿QQ登录界面2011-09-04
- •swing各组件重绘外观2011-08-30
- •MyBatis普通查询与一对一映射查询2011-08-09
- •spring依赖注入(Ioc)控制反转2011-10-14
- •Java2D 图像缓冲与重复绘制2011-09-14
- •swing创建自定义窗体界面2011-08-31
- •Eclipse 常用快捷键2011-08-29
- •Apache Mina自定义编解码案例2011-08-09
width="728" height="90" align="center,center" id="iframeu1607657_0" src="//pos.baidu.com/fcim?sz=728x90&rdid=1607657&dc=2&di=u1607657&dri=0&dis=0&dai=2&ps=3485x287&coa=at%3D3%26rsi0%3D728%26rsi1%3D90%26pat%3D6%26tn%3DbaiduCustNativeAD%26rss1%3D%2523FFFFFF%26conBW%3D1%26adp%3D1%26ptt%3D0%26titFF%3D%2525E5%2525BE%2525AE%2525E8%2525BD%2525AF%2525E9%25259B%252585%2525E9%2525BB%252591%26titFS%3D%26rss2%3D%2523000000%26titSU%3D0%26ptbg%3D90%26piw%3D0%26pih%3D0%26ptp%3D0&dcb=BAIDU_SSP_define&dtm=BAIDU_DUP_SETJSONADSLOT&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr=1464181549248&ti=MyBatis%E5%8A%A8%E6%80%81SQL%20-%20God's%20blog%20-%20%E5%8D%9A%E5%AE%A2%E9%A2%91%E9%81%93%20-%20CSDN.NET&ari=1&dbv=2&drs=3&pcs=1351x661&pss=1351x4065&cfv=11&cpl=17&chi=2&cce=true&cec=UTF-8&tlm=1464152749<u=http%3A%2F%2Fblog.csdn.net%2Fa600423444%2Farticle%2Fdetails%2F6658411&ecd=1&psr=1366x768&par=1366x728&pis=-1x-1&ccd=24&cja=true&cmi=23&col=zh-CN&cdo=-1&tcn=1464181549&qn=5bf5b01977c87ed8&tt=1464181549230.25.178.180" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" vspace="0" hspace="0" style="margin: 0px; border: 0px currentColor; border-image: none; vertical-align: bottom;" allowtransparency="true">
核心技术类目
全部主题
Hadoop
AWS
移动游戏
Java
Android
iOS
Swift
智能硬件
Docker
OpenStack
VPN
Spark
ERP
IE10
Eclipse
CRM
JavaScript
数据库
Ubuntu
NFC
WAP
jQuery
BI
HTML5
Spring
Apache
.NET
API
HTML
SDK
IIS
Fedora
XML
LBS
Unity
Splashtop
UML
components
Windows Mobile
Rails
QEMU
KDE
Cassandra
CloudStack
FTC
coremail
OPhone
CouchBase
云计算
iOS6
Rackspace
Web App
SpringSide
Maemo
Compuware
大数据
aptech
Perl
Tornado
Ruby
Hibernate
ThinkPHP
HBase
Pure
Solr
Angular
Cloud Foundry
Redis
Scala
Django
Bootstrap
- 个人资料
-
- 访问:685167次
- 积分:9391
- 等级:
- 排名:第1166名
- 原创:242篇
- 转载:11篇
- 译文:0篇
- 评论:160条
- 文章搜索
- 文章分类
- 文章存档
- 2014年04月(1)
- 2013年10月(1)
- 2013年09月(1)
- 2013年05月(2)
- 2013年04月(3)
- 2013年03月(3)
- 2013年02月(1)
- 2012年10月(2)
- 2012年09月(1)
- 2012年07月(3)
- 2012年06月(1)
- 2012年05月(3)
- 2012年04月(4)
- 2012年03月(4)
- 2012年01月(4)
- 2011年12月(4)
- 2011年11月(8)
- 2011年10月(5)
- 2011年09月(10)
- 2011年08月(13)
- 2011年07月(6)
- 2011年06月(24)
- 2011年05月(25)
- 2011年04月(6)
- 2011年03月(2)
- 2011年02月(7)
- 2011年01月(18)
- 2010年12月(37)
- 2010年11月(24)
- 2010年10月(9)
- 2010年09月(21)
- 阅读排行
- 分析Redis架构设计(28111)
- Linux GCC编译使用动态、静态链接库(23196)
- MyBatis动态SQL(18198)
- MyBatis普通查询与一对一映射查询(18027)
- Java将Unix时间戳转换成指定格式日期(16412)
- Mysql创建存储过程及遍历查询结果(15873)
- Redis C客户端API(13226)
- Apache Mina自定义编解码案例(12824)
- Windows中搭建PHP扩展开发环境(12201)
- C实现PHP的mysql数据库连接池(11112)
- 评论排行
- 推荐文章
- 最新评论
- Java将Unix时间戳转换成指定格式日期
zhangyd0227:很好用,谢谢
- 队列服务开发
obchi:你好,地址不能访问了,能换个吗?急需你的源码呀,谢谢1873866421@qq.com
- 队列服务开发
obchi:你好,地址不能访问了,能换个吗?急需你的源码呀,谢谢1873866421@qq.com
- Windows中搭建PHP扩展开发环境
qianggetaba:@qq_33604875: http://windows.php.net/downloads/p...
- jquery插件
吴士龙:感谢分享,实战性很强,推荐一下。
- 分析Redis架构设计
辰辰爸的技术博客:学习le!
- 队列服务开发
吴名置禾:求源代码救命!!!!!!!!!邮箱:wulangdejava@163.com。感谢!
- 队列服务开发
qq_31156543:急求求源代码!非常感谢!
- 分析Redis架构设计
gaoxuaiguoyi:为博主的画图软件点赞
- Java将Unix时间戳转换成指定格式日期
SYSGIS:java新手,很有帮助
可能存在类似的问题:
1楼 Wilson_Peng 2013-08-17 17:35发表 [回复] [引用] [举报]-
-
所有的if判断都是判断非空,能否判断是否等于一个特定字符串呢?