梦想还是要有的,万一忘了咋办?

0%

Mybatis动态Sql

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

if

1
2
3
<if test="title != null">
AND title like #{title}
</if>

choose、when、otherwise

类似Java中的switch语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<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

where

  • 子元素返回任何内容是才会插入”WHERE“子句
  • 去除开头的”AND“、”OR”,意味着你可以任性的这么写了
    1
    2
    3
    4
    5
    6
    7
    <where>
    ...
    <if test="title != null">
    AND title like #{title}
    </if>
    ...
    </where>
    where 等价于
    1
    2
    3
    <trim prefix="WHERE" prefixOverrides="AND |OR ">
    ...
    </trim>
  • set*
  • 忽略结尾额外的逗号
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <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>
    set等价于
    1
    2
    3
    <trim prefix="SET" suffixOverrides=",">
    ...
    </trim>

foreach

  • 构建 IN 条件语句的时常用
  • collection可以是适用任何可迭代对象(list、set、map等)
  • 提供 集合项(item)、索引(index)
  • 设置开头(open)、结尾(close)、分隔符(separator)字符串
  • 当迭代对象是map时,index是map的key,item是map的value
1
2
3
4
5
6
7
8
9
<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>

script

要在带注解的映射器接口类中使用动态 SQL,可以使用 script 元素。比如:

1
2
3
4
5
6
7
8
9
10
11
@Update({"<script>",
"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}",
"</script>"})
void updateAuthorValues(Author author);

bind

bind 元素允许你在 OGNL 表达式以外创建一个变量,并将其绑定到当前的上下文。比如:

1
2
3
4
5
<select id="selectBlogsLike" resultType="Blog">
<bind name="pattern" value="'%' + _parameter.getTitle() + '%'" />
SELECT * FROM BLOG
WHERE title LIKE #{pattern}
</select>

多数据库支持

如果配置了 databaseIdProvider,你就可以在动态代码中使用名为 “_databaseId” 的变量来为不同的数据库构建特定的语句。比如下面的例子:

1
2
3
4
5
6
7
8
9
10
11
insert id="insert">
<selectKey keyProperty="id" resultType="int" order="BEFORE">
<if test="_databaseId == 'oracle'">
select seq_users.nextval from dual
</if>
<if test="_databaseId == 'db2'">
select nextval for seq_users from sysibm.sysdummy1"
</if>
</selectKey>
insert into users values (#{id}, #{name})
</insert>

动态SQL中插入脚本语言

MyBatis3.2+ 开始支持插入脚本语言。

1、实现LanguageDriver接口:

1
2
3
4
5
public interface LanguageDriver {
ParameterHandler createParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql);
SqlSource createSqlSource(Configuration configuration, XNode script, Class<?> parameterType);
SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType);
}

2、mybatis-config.xml设置全局语言

1
2
3
4
5
6
<typeAliases>
<typeAlias type="org.sample.MyLanguageDriver" alias="myLanguage"/>
</typeAliases>
<settings>
<setting name="defaultScriptingLanguage" value="myLanguage"/>
</settings>

3、使用lang属性为特定语句指定语言

1
2
3
<select id="selectBlog" lang="myLanguage">
SELECT * FROM BLOG
</select>

4、使用@Lang注解在Mapper接口上指定语言

1
2
3
4
5
public interface Mapper {
@Lang(MyLanguageDriver.class)
@Select("SELECT * FROM BLOG")
List<Blog> selectBlog();
}

可以使用 Apache Velocity 作为动态语言,更多细节请参考 MyBatis-Velocity 项目。

你前面看到的所有 xml 标签都由默认 MyBatis 语言提供,而它由语言驱动 org.apache.ibatis.scripting.xmltags.XmlLanguageDriver(别名为 xml)所提供