您的位置:首页 > 数据库

MyBatis动态SQL

2015-06-14 13:43 393 查看
近日在使用MyBatis框架,感慨于他的灵活多变的SQL方式。这种动态是基于OGNL表达式的。在这里有必要总结一番。
MyBatis实现动态SQL主要有一下元素:


if

choose(when,otherwise)

where

trim

set

foreach

在这里先创建一个简单的表User,有属性 id,name,password

1)if

条件判断,实现条件选择。

例如:

<select id="selectUser" parameterType="map" resultType="map">
select *
from user
where 1 = 1
<if test="id != null">
and id=#{id}
</if>
<if test="name != null">
and name=#{name}
</if>
<if test="password != null">
and password=#{password}
</if>
</select>


这条语句说的就是满足if条件就将该if里的内容拼接上去。

2)choose

也是条件判断,但与if不同,它是只取其一。相当于java中的switch。通常和when,otherwise搭配。when元素表示当when中的条件满足的时候就输出其中的内容,跟JAVA中的switch效果差不多的是按照条件的顺序,当when中有条件满足的时候,就会跳出choose,即所有的when和otherwise条件中,只有一个会输出,当所有的我很条件都不满足的时候就输出otherwise中的内容。

例如:

<select id="selectUser" parameterType="map" resultType="map">
select *
from user
where 1=1
<choose>
<when test="id != null">
and id=#{id}
</when>
<when test="name != null">
and name =#{name }
</when>
<otherwise>
and id='id1'
</otherwise>
</select>


上面说的是,如果满足id!= null  就输出 and id=#{id},否则 看是否满足 name != null ,满足就 输出 and name=#{name}。没有满足条件的就执行otherwise中的内容。


3)where

简化SQL语句中的where判断。 where元素的作用是会在写入where元素的地方输出一个where,另外一个好处是你不需要考虑where元素里面的条件输出是什么样子的,MyBatis会智能的帮你处理,如果所有的条件都不满足那么MyBatis就会查出所有的记录,如果输出后是and 开头的,MyBatis会把第一个and忽略,当然如果是or开头的,MyBatis也会把它忽略;此外,在where元素中你不需要考虑空格的问题,MyBatis会智能的帮你加上。

例如:

<select id="selectUser" parameterType="map" resultType="map">
select *
from user
<where>
<if test="id != null">
and id=#{id}
</if>
<if test="name != null">
and name=#{name}
</if>
<if test="password != null">
and password=#{password}
</if>
</where>
</select>


上面的例子中,MyBatis会将首个 and 去掉。这就是MyBatis的智能之处。

4)trim

主要功能是可以在自己包含的内容前加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是prefix和suffix;可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容覆盖,对应的属性是prefixOverrides和suffixOverrides。也可以非常简单的利用trim来代替where元素的功能。

例如:

<select id="selectUser" parameterType="map" resultType="map">
select *
from user
<trim prefix="where" prefixOverrides="and |or">
<if test="id != null">
id = #{id }
</if>
<if test="name != null">
and name= #{name}
</if>
<if test="password != null">
or password = #{password}
</if>
</trim>
</select>


5)set

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

<update id="updateUser" parameterType="map" resultType="map">
update user
<set>
<if test="id != null">
id = #{id }
</if>
<if test="name != null">
and name= #{name}
</if>
<if test="password != null">
or password = #{password}
</if>
</set>
</update>


如果上面set中一个条件都不满足,即set内容为空时就会报错。

6)foreach

主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。foreach元素的属性主要有item,index,collection,open,separator,close。item表示集合中每一个元素进行迭代时的别名,index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔符,close表示以什么结束,在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的。

<select id="selectUser" parameterType="map" resultType="type">
select *
from user
where  1=a
<if test="id != null">
and id in
<foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
</select>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: