什么是动态SQL

  • 动态SQL(Dynamic SQL)是一种在运行时动态构建和执行SQL语句的技术‌。
  • 与静态SQL(Static SQL)不同,动态SQL允许在程序运行时根据不同的条件和需求生成不同的SQL语句,从而提供更高的灵活性和适应性。
  • 动态SQL是MyBatis的一个强大特性,可以运用动态SQL语句标签方便我们在SQL中实现各种逻辑

动态SQL标签

if标签

if标签用于根据条件判断是否需要执行SQL语句。如果 if 标签内 test 中的表达式 返回为 true 怎会将标签内的语句填入查询语句

1
2
3
4
5
6
<select id="selectUser" resultType="User">
select * from user where 1=1
<if test="name != null">
and name = #{name}
</if>
</select>

where标签

where标签用于自动添加where关键字,并根据条件自动生成where子句。如果where标签内的条件满足条件,则自动添加where关键字,否则不添加。

1
2
3
4
5
6
7
8
9
10
11
<select id="selectUser" resultType="User">
select * from user
<where>
<if test="name != null">
and name = #{name}
</if>
<if test="nickname != null">
and nickname = #{nickname}
</if>
</where>
</select>

choose标签

choose标签用于根据条件选择执行SQL语句。相当于switch语句
choose标签包含一个或多个when标签,以及一个可选的otherwise标签。choose标签会根据条件选择执行when标签中的语句,如果when标签中的条件满足条件,则执行when标签中的语句,否则执行otherwise标签中的语句。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<select id="selectUser" resultType="User">
select * from user
<where>
<choose>
<when test="name != null">
and name = #{name}
</when>
<when test="nickname != null">
and nickname = #{nickname}
</when>
<otherwise>
and id = #{id}
</otherwise>
</choose>
</where>
</select>

set标签

set标签用于解决动态更新语句存在的符号问题。如果set标签内的条件满足条件,则自动生成SET子句,否则不生成。

1
2
3
4
5
6
7
8
9
10
11
<update id="updateUser" parameterType="User">
update user
<set>
<if test="name != null">
name = #{name},
</if>
<if test="nickname != null">
nickname = #{nickname}
</if>
</set>
</update>

trim标签

trim标签用于根据条件添加前缀和后缀,例如添加where关键字,添加逗号等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!--<trim prefix="前缀" suffix="后缀" prefixOverrides="忽略前缀字符" suffixOverrides="忽略后缀字符">-->
<!-- SQL语句-->
<!--</trim>-->

<select id="selectUser" resultType="User">
select * from user
<trim prefix="where" prefixOverrides="and|or">
<if test="name != null">
and name = #{name}
</if>
<if test="nickname != null">
and nickname = #{nickname}
</if>
</trim>
</select>
属性 描述
prefix 为trim标签内的语句前添加前缀
suffix 为trim标签内的语句后添加后缀
prefixOverrides 忽略trim标签内的语句前缀
suffixOverrides 忽略trim标签内的语句后缀

foreach标签

foreach标签用于循环遍历集合,并将集合中的元素添加到SQL语句中。

foreach标签的属性主要有 item,index,collection,open,separator,close。

  • item:表示集合中的元素
  • index:指定一个名字,用于表示在迭代过程中,每次迭代的位置open是前缀,表示该语句以什么开始。
  • separator:表示在每次迭代元素之间以什么符号作为分隔符。
  • close:是后缀,表示以什么结束。
  • collection:指定需要遍历的集合。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!--<foreach item="item" index="index" collection="list|array|map key" open="(" separator="," close=")">-->
<!-- #{item}-->
<!--</foreach>-->

<select id="selectUser" resultType="User">
select * from user
<where>
<foreach
item="item"
index="index"
collection="list"
open="and id in ("
close=")"
separator=",">
#{item}
</foreach>
</where>
</select>

sql标签

sql标签用于定义可重用的SQL语句,方便在多个地方引用。

1
2
3
<sql id="selectUser">
select * from user
</sql>

include标签

include标签用于引用已经定义好的sql标签。

1
2
3
4
5
6
7
8
9
10
<!--<include refid="selectUser"/>-->

<select id="selectUser" resultType="User">
<include refid="selectUser"/>
<where>
<if test="name != null">
and name = #{name}
</if>
</where>
</select>