mybatis 动态SQL配置

来源:互联网 发布:windows rt怎么换系统 编辑:程序博客网 时间:2024/05/29 10:41
MyBatis 的动态SQL 是基于OGNL 表达式的,它可以帮助我们方便的在SQL 语句中实现某些逻辑。

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

if
choose (when ,otherwise )
trim
where
set
foreach
if

if 就是简单的条件判断,利用if 语句我们可以实现某些简单的条件选择。先来看如下一个例子:

delete from user <where> <if test="id != null"> and id= #{id} </if> <if test="code!= null"> and code= #{code} </if> </where> 

if语句可以方便条件不同的SQL重用,但当where后面的if全部不满足时,就会导致空条件而删除或更新整个表。

可以在语句里面加上这样一个语句,避免全表更新或者删除。
delete from user <where> <if test="id == null and code = null"> and exception </if> <if test="id != null"> and id= #{id} </if> <if test="code!= null"> and code= #{code} </if> </where> 

choose

choose 元素的作用就相当于 JAVA 中的 switch 语句,基本上跟 JSTL 中的 choose 的作用和用法是一样的,通常都是与 when 和 otherwise 搭配的。看如下一个例子:

<choose> <when test="title != null"> and title = #{title} </when> <when test="content != null"> and content = #{content} </when> <otherwise> and owner = "owner1" </otherwise> </choose> 

when 元素表示当 when 中的条件满足的时候就输出其中的内容,跟 JAVA 中的 switch 效果差不多的是按照条件的顺序,当 when 中有条件满足的时 候,就会跳出 choose ,即所有的 when 和 otherwise 条件中,只有一个会输出,当所有的我很条件都不满足的时候就输出 otherwise 中的 内容。

where

where 语句的作用主要是简化 SQL 语句中 where 中的条件判断的,先看一个例子

<where> <if test="title != null"> and title = #{title} </if> <if test="content != null"> and content = #{content} </if> <if test="owner != null"> and owner = #{owner} </if> </where> 

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

注意,当 Where 元素后面跟着 <foreach open="AND"> ,并不能去除 foreach 元素中的 AND ,这时可以用trim来去除。

trim

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

<trim prefix="where" prefixOverrides="and">
......
</trim>
trim元素后面跟着 <foreach open="AND"> ,可以去除 foreach 元素中的 AND 。

<update id="dynamicSetTest" parameterType="Blog"> update t_blog <trim prefix="set" suffixOverrides=","> <if test="title != null"> title = #{title}, </if> <if test="content != null"> content = #{content}, </if> </trim> where id = #{id} </update> 

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"> content = #{content}, </if> </set> where id = #{id} </update> 
foreach

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

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

<select id="dynamicForeachTest" parameterType="java.util.List" resultType="Blog"> select * from t_blog where id in <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select> 

要注意的是,无论你JAVA代码传入的list名字是什么,在collection中名字必须为list。

2、如果传入的参数是多个的时候,我们就需要把它们封装成一个Map 了,当然单参数也可以封装成map ,实际上如果你在传入参数的时候,在MyBatis 里面也是会把它封装成一个Map 的,map 的key 就是参数名,所以这个时候collection 属性值就是传入的List 或array 对象在自己封装的map 里面的key
<select id="exists" parameterType="java.util.HashMap" resultType="java.lang.Integer"> select count(*) from TEST test <WHERE> <if test="code != null"> AND test.CODE = #{code} </if> <if test="idList !=null "> and test.id in( <foreach item="guard" index="index" collection="idList" separator=","> #{guard} </foreach> ) </if> </WHERE> </select> 

3、 JAVA对象中的list。和map写法一样,只是传入参数时不同
<select id="exists" parameterType="TestDto" resultType="java.lang.Integer"> select count(*) from TEST test <WHERE> <if test="code != null"> AND test.CODE = #{code} </if> <if test="idList !=null "> and test.id in( <foreach item="guard" index="index" collection="idList" separator=","> #{guard} </foreach> ) </if> </WHERE> </select> 

注意这里的parameterType如果是JAVA对象的话,必须先注入进去,或者加上包的路径。

注入时 配置如下:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="typeAliasesPackage" value="com.test.model,com.test.dto/> <property name="configLocation" value="classpath:mybatis.xml" /> <!-- mapper和resultmap配置路径 --> <property name="mapperLocations"> <list> <!-- 表示在mapper包所有目录中,以-Mapper.xml结尾所有文件 --> <value>classpath:/mapper/*/*Mapper.xml</value> </list> </property> </bean>


0 0
原创粉丝点击