【MyBatis】动态sql

5 篇文章 2 订阅

Mybatis动态sql

MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑。

MyBatis中用于实现动态SQL的元素主要有:

If

Choose(when,otherwise)

Trim

Where

Set

Foreach

1、 if

if用于简单的条件判断。

<select id="dynamicIfTest" parameterType="Blog" resultType="Blog">

select * from t_blog where 1=1;

<if test="title != null">

and title =#{title}

</if>

</select>

如果没有title参数,则查询所有的blog,如果有title参数,则查询的结果必须满足title =#{title}.

2、 choose

choose元素的作用就相当于JAVA中的switch语句,通常都是when和otherwise搭配的。

<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">

select * from t_blog where 1=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中的条件满足时就输出其中的内容,按照条件的顺序,当when中只要有条件满足的时候,就会跳出choose,即所有的when和otherwise条件中,只有一个会输出,当条件都不满足时,输出otherwise中的内容。

 

foreach

foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。Foreach元素的主要属性有item,index,collection,open,sperator,close. Item表示集合中每一个元素进行迭代时的别名,index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示语句以什么开始,separator表示在每次进行迭代之间以什符号作为分隔符,close表示以什么结束。Collection属性必须是指定的,但在不同情况下,该属性的值是不一样的,主要有以下3种情况:

(1)如果传入的是单参数且参数类型是一个List的时候,collection的属性值为list

(2)如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array

(3)如果传入的参数是多个的时候,我们就要把他们封装成一个Map

<select id="dynamicForeachTest"  resultType="Blog">

select * from t_blog where title like "%"#{title}"%"

and id in

<foreach collection="ids" item="item">

#{item}

</foreach>

</select>

示例collections的值为ids,是传入的参数Map的key。

3、 where

where元素的作用是会在写入where元素的地方输出一个where。如果输 

出后是and开头的,mybatis会把第一个and忽略。

<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>

</where>

</select>

4、set

set元素主要是用在更新操作的时候,它的主要功能和where元素差不多,主要是在包含的语句前输出一个set,然后如果包含的语句是以逗号结束的话将会把逗号忽略,如果set包含的内容为空的话则会出错。有了set元素我们就可以动态的更新那些修改了的字段。

<update id="dynamicSetTest" parameterType="Blog">

update t_blog

<set>

<if test="title != null">

title=#{title},

</if>

<if test="content != null">

and content = #{content},

</if>

<if test="owner != null">

or owner = #{owner}

</if>

</set>

where id = #{id}

</update>

上述代码中,如果set中一个条件都不满足,即set中包含的内容为空的时候就会报错。

6、trim

trim元素的主要功能是可以在自己包含的内容前加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是prefix和suffix;可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容覆盖,对应的属性是prefixOverrides和suffixOverides;

<select id="dynamicTrimTest" resultType="Blog">

select * from t_blog

<trim prefix="where" prefixOverrides="and|or">

<if test="title != null">

title=#{title}

</if>

<if test="content != null">

and content = #{content}

</if>

<if test="owner != null">

or owner = #{owner}

</if>

</trim>

</select>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SunAlwaysOnline

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值