动态 SQL,通过 MyBatis 提供的各种标签对条件作出判断以实现动态拼接 SQL 语句。这里的条件判断使用的表达式为 OGNL 表达式。常用的动态 SQL 标签有、、、等。MyBatis 的动态 SQL 语句,与 JSTL 中的语句非常相似。

动态 SQL,主要用于解决查询条件不确定的情况:在程序运行期间,根据用户提交的查询条件进行查询。提交的查询条件不同,执行的 SQL 语句不同。若将每种可能的情况均逐一列出,对所有条件进行排列组合,将会出现大量的 SQL 语句。此时,可使用动态 SQL 来解决这样的问题.

mybatis动态sql_集合类型

1. <if>

对于该标签的执行,当 test 的值为 true 时,会将其包含的 SQL 片断拼接到其所在的 SQL 语句中。

语法:<if test=”条件”> sql 语句的部分 </if>

2. <where>

标签的中存在一个比较麻烦的地方:需要在 where 后手工添加 1=1 的子句。因为,若 where 后的所有条件均为 false,而 where 后若又没有 1=1 子句,则 SQL 中就会只剩下一个空的 where,SQL
出错。所以,在 where 后,需要添加永为真子句 1=1,以防止这种情况的发生。但当数据量很大时,会严重影响查询效率。

使用标签,在有查询条件时,可以自动添加上 where 子句;没有查询条件时,不会添加where 子句。需要注意的是,第一个标签中的 SQL 片断,可以不包含 and。不过,写上 and 也不错,系统会将多出的 and 去掉。但其它中 SQL 片断的 and,必须要求写上。否则 SQL 语句将拼接出错。

语法:<where> 其他动态 sql </where>

接口方法:
List<User> selectUsersByIf(User user);

mapper文件:

<select id="selectUsersByIf" resultMap="user">
        select * from user
        <where>
            <if test="no == 1">
                uname = #{name}
            </if>
            <if test="no == 2">
                AND uname = '黄大大'
            </if>
        </where>
    </select>

3. <foreach>

<foreach/>标签用于实现对于数组与集合的遍历。对其使用,需要注意:
➢ collection 表示要遍历的集合类型, list ,array 等。
➢ open、close、separator 为对遍历内容的 SQL 拼接。
语法:

<foreach collection="集合类型" open="开始的字符" close="结束的字符"
item="集合中的成员" separator="集合成员之间的分隔符">
 #{item 的值}
</foreach>

接口方法:
List<User> selectUsers(List<String> names);

mapper文件

<select id="selectUsers" resultMap="user">
        select * from user where uname in
        <foreach collection="list" item="name" open="(" close=")" separator=",">
            #{name} <!-- 若集合类型为对象此处为#{对象名.属性名} -->
        </foreach>
    </select>

4. 动态sql片段

标签用于定义 SQL 片段,以便其它 SQL 标签复用。而其它标签使用该 SQL 片段,需要使用子标签。该标签可以定义 SQL 语句中的任何部分,所以子标签可以放在动态 SQL的任何位置。

<sql id="studentSql">
 select id,name,email,age from student
</sql>
<select id="selectStudentSqlFragment"
resultType="com.bjpowernode.domain.Student">
 <!-- 引用 sql 片段 -->
 <include refid="studentSql"/>
 <if test="list !=null and list.size > 0 ">
 where id in
 <foreach collection="list" open="(" close=")"
item="stuobject" separator=",">
 #{stuobject.id}
 </foreach>
 </if>
</select>