首页 > 数据库技术 > 详细

mybatis动态sql

时间:2015-12-11 02:17:33      阅读:357      评论:0      收藏:0      [点我收藏+]

1:简单的if

?

<select id="dynamicIfTest" parameterType="Model" resultType="Model">  
	select * from t_blog where 11 = 1  
	<if test="title != null">  
		and title = #{title}  
	</if>  
	<if test="content != null">  
		and content = #{content}  
	</if>  
	<if test="owner != null">  
		and owner = #{owner}  
	</if>  
</select>

简述,这里的title != <if test="title != null"> 也支持 title != <if test="title != ‘ ‘ ">?

?

?

2:choose语句

?

<select id="dynamicChooseTest" parameterType="Model" resultType="Model">  
	select * from t_blog where 11 = 1   
	<choose>  
		<when test="title != null">  
			and title = #{title}  
		</when>  
		<when test="content != null">  
			and content = #{content}  
		</when>  
		<otherwise>  
			and owner = "owner1"  
		</otherwise>  
	</choose>  
</select>  

?简述,当when中有条件满足的时候,就会跳出choose(和if else if else 差不多)如果两个when都不满足,则<otherwise>代码块就会执行。

?

3:where 标签

?

<select id="dynamicWhereTest" parameterType="Blog" resultType="Blog">  
    select * from t_blog   
    <where>  
        <if test="title != null">  
            title = #{title}  
        </if>  
        <if test="content != null">  
            and content = #{content}  
        </if>  
        <if test="owner != null">  
            and owner = #{owner}  
        </if>  
    </where>  
</select> 

?简述,这个<where>标签的好处就是自动只能的给你添加 and 和 or 关键字,比如tile == null 而 content !=null 就会输出 select * from t_blog? where content = #{content} 自动去掉 and 很只能吧!

?

这些基本就够用了!

?

?

mybatis动态sql

原文:http://zliguo.iteye.com/blog/2262911

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!