`

mybatis学习笔记(六)

 
阅读更多
动态SQL
通常使用动态SQL不可能是独立的一部分,MyBatis当然使用一种强大的动态SQL语言来改进这种情形,这种语言可以被用在任意映射的SQL语句中。
MyBatis采用功能强大的基于OGNL的表达式来消除其他元素。

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

if
在动态SQL中所做的最通用的事情是包含部分where字句的条件。比如:

<select id=”findActiveBlogWithTitleLike”
	parameterType=”Blog” resultType=”Blog”>
		SELECT * FROM BLOG
		WHERE state = ?ACTIVE?
		<if test=”title != null”>
			AND title like #{title}
		</if>
</select>

choose, when, otherwise
和Java中的switch语句相似,MyBatis提供choose元素。
<select id=”findActiveBlogLike”
	parameterType=”Blog” resultType=”Blog”>
		SELECT * FROM BLOG WHERE state = ?ACTIVE?
		<choose>
			<when test=”title != null”>
				AND title like #{title}
			</when>
			<when test=”author != null and author.name != null”>
				AND title like #{author.name}
			</when>
			<otherwise>
				AND featured = 1
			</otherwise>
		</choose>
</select>

trim, where, set
上面的例子中如果把“state = ?ACTIVE?”也设置为动态的话就有可能出现问题。所以,
<select id=”findActiveBlogLike”
		parameterType=”Blog” resultType=”Blog”>
	SELECT * FROM BLOG
	<where>
		<if test=”state != null”>
			state = #{state}
		</if>
		<if test=”title != null”>
			AND title like #{title}
		</if>
		<if test=”author != null and author.name != null”>
			AND title like #{author.name}
		</if>
	</where>
</select>
where元素知道如果由被包含的标记返回任意内容,就仅仅插入“WHERE”。而且,如果以“AND”或“OR”开头的内容,那么就会跳过WHERE不插入。
如果where元素没有做出你想要的,你可以使用trim元素来自定义。比如,和where元素相等的trim元素是:
<trim prefix="WHERE" prefixOverrides="AND |OR ">
…
</trim>
overrides属性采用管道文本分隔符来覆盖,这里的空白也是重要的。它的结果就是移除在overrides属性中指定的内容,插入在with属性中的内容。
和动态更新语句相似的解决方案是set。set元素可以被用于动态包含更新的列,而不包含不需更新的。比如:
<update id="updateAuthorIfNecessary"
		parameterType="domain.blog.Author">
	update Author
	<set>
		<if test="username != null">username=#{username},</if>
		<if test="password != null">password=#{password},</if>
		<if test="email != null">email=#{email},</if>
		<if test="bio != null">bio=#{bio}</if>
	</set>
	where id=#{id}
</update>
这里,set元素会动态前置SET关键字,而且也会消除任意无关的逗号,那也许在应用条件之后来跟踪定义的值。
如果你对和这相等的trim元素好奇,它看起来就是这样的:
<trim prefix="SET" suffixOverrides=",">
…
</trim>
注意这种情况下我们覆盖一个后缀,而同时也附加前缀。

foreach
另外一个动态SQL通用的必要操作是迭代一个集合,通常是构建在IN条件中的。比如:
<select id="selectPostIn" resultType="domain.blog.Post">
	SELECT *
	FROM POST P
	WHERE ID in
	<foreach item="item" index="index" collection="list"
			open="(" separator="," close=")">
		#{item}
	</foreach>
</select>
foreach元素是非常强大的,它允许你指定一个集合,声明集合项和索引变量,它们可以用在元素体内。它也允许你指定开放和关闭的字符串,在迭代之间放置分隔符。这个元素是很智能的,它不会偶然地附加多余的分隔符。
注意:你可以传递一个List实例或者数组作为参数对象传给MyBatis。当你这么做的时候,MyBatis会自动将它包装在一个Map中,用名称作为键。List实例将会以“list”作为键,而数组实例将会以“array”作为键。



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics