标签:一个 声明 sele 字符串 prefix other lis blog post
常用:根据条件包含where子句的一部分
<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<if test="title != null">
AND title like #{title}
</if>
<if test="author != null and author.name != null">
AND author_name like #{author.name}
</if>
</select>
常用:不想使用所有的条件,只从多个条件中选择一个使用,类似于java的switch语句
<select id="findActiveBlogLike"
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 author_name like #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</select>
常用:通过trim元素来定制where元素和set元素的功能
<trim prefix="SET" suffixOverrides=",">
...
</trim>
where:
where元素只会在子元素返回任何内容的情况下才插入“WHERE”子句
若子句的开头为“AND”或 “OR”,where 元素会将它们去除
<select id="findActiveBlogLike"
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 author_name like #{author.name}
</if>
</where>
</select>
set:
<update id="updateAuthorIfNecessary">
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>
常用:对集合进行遍历,尤其是在构建 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>
功能:
使用:可以将任何可迭代对象(List、Set等)、Map对象或者数组对象作为集合参数传递给foreach
标签:一个 声明 sele 字符串 prefix other lis blog post
原文地址:https://www.cnblogs.com/thetree/p/12_mybatis.html