mybatis动态sql

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 很只能吧!

 

这些基本就够用了!

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值