前言
我们之前都使用过JDBC,根据不同的条件去拼接SQL语句的痛苦不言而喻,拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。虽然使用占位符?
一定限度的简化了拼接难度,但是SQL注入的问题从没有被解决过。在JDBC中使用动态SQL语句难如上青天,但Mybatis极大程度的简化了这个过程。
正文
Mybatis框架提供了很对的元素种类,依赖提供SQL语句的映射。
例如:
- if
- choose (when, otherwise)
- trim (where, set)
- foreach
if
使用动态 SQL 最常见情景是根据条件包含 where 子句的一部分。
例如:
<select id="selectCondition"
resultMap="BrandResult">
SELECT * FROM tb_brand
WHERE companyName = '华为'
<if test="title != null">
AND title like #{title}
</if>
</select>
这条语句提供了可选的查找文本功能。如果不传入“title”,那么<if>
标签中的SQL语句AND title like #
就不会出现。
那么#{title}
是什么呢
这个就相当于Java类的成员方法所需要传入的形参。
choose、when、otherwise
有时候,我们不想使用所有的条件,而只是想从多个条件中选择一个使用。针对这种情况,MyBatis 提供了 choose 元素,它有点像 Java 中的 switch 语句。
choose => switch
when => case
otherwise => default
<select id="selectConditionSingle" resultMap="BrandResult">
select * from tb_brand
<where>
<choose>
<when test="status != null">
status = #{status}
</when>
<when test="companyName != null and companyName != ''">
company_name like #{companyName}
</when>
<when test="brandName != null and companyName != ''">
brand_name like #{brandName}
</when>
<!-- <otherwise>-->
<!-- 1 = 1-->
<!-- </otherwise>-->
</choose>
</where>
</select>
细心的同学可能发现,上方的代码,我们使用了<where>
它是干什么的呢,感觉好像呼之欲出对吧。
trim、where、set
前面几个例子已经合宜地解决了一个臭名昭著的动态 SQL 问题。
<select id="selectByIdBrand" resultMap="BrandResult">
select *
from tb_brand
where id = #{id}
</select>
当我们使用这个SQL映射时,如果不传入id参数它会怎么样呢?
最终这条 SQL 会变成这样:
SELECT * FROM tb_brand
WHERE
这行SQL语句根本不可用。
<select id="selectByCondition" resultMap="BrandResult">
select *
from tb_brand
where status = #{status}
and company_name like #{companyName}
and brand_name like #{brandName}
</select>
同样的我们只匹配第二个条件呢?
这条 SQL 会是这样:
SELECT * FROM tb_brand
WHERE
AND company_name like "%balabala"
额..更糟糕的事情发生了,不用我说,这条SQL语句有一种脑干被挖空的感觉。
Mybatis就提供了这三个标签来解决这个问题
<where>
<!-- 使用where标签,来写动态 SQL语句 -->
<!-- if标签判断是否有这个参数传入-->
<select id="selectCondition" resultMap="BrandResult">
select *
from tb_brand
<where>
<if test="status != null">
and status = #{status}
</if>
<if test="companyName != null and companyName != ''">
and company_name like #{companyName}
</if>
<if test="brandName != null and companyName != ''">
and brand_name like #{brandName}
</if>
</where>
</select>
这个不管参数有没有传入,它映射的SQL语句十分优雅。
<trim>
如果 where 元素与你期望的不太一样,你也可以通过自定义 trim 元素来定制 where 元素的功能。比如,和 where 元素等价的自定义 trim 元素为:
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
</trim>
<set>
用于动态更新语句的类似解决方案叫做 set。set 元素可以用于动态包含需要更新的列,忽略其它不更新的列。比如:
<update id="update">
update tb_brand
<set>
<if test="brandName != null and brandName != ''">
brand_name = #{brandName},
</if>
<if test="companyName != null and companyName != ''">
company_name = #{companyName},
</if>
<if test="ordered != null and ordered != ''">
ordered = #{ordered},
</if>
<if test="description != null and description != ''">
description = #{description},
</if>
<if test="status != null and status != ''">
status = #{status}
</if>
</set>
where id = #{id};
</update>
这个例子中,set 元素会动态地在行首插入 SET 关键字,并会删掉额外的逗号(这些逗号是在使用条件语句给列赋值时引入的)。
当然它也可以用<trim>
标签代替。
<trim prefix="SET" suffixOverrides=",">
...
</trim>
<foreach>
动态 SQL 的另一个常见使用场景是对集合进行遍历(尤其是在构建 IN 条件语句的时候)。比如:
<delete id="deleteByIds">
delete from tb_brand where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
;
</delete>
foreach 元素的功能非常强大,它允许你指定一个集合,声明可以在元素体内使用的集合项(item)和索引(index)变量。它也允许你指定开头与结尾的字符串以及集合项迭代之间的分隔符。这个元素也不会错误地添加多余的分隔符,换句话说这玩意比我聪明。
结语
Mybatis极大简化了动态SQL的编写,大多数情况下,我们只需要更改xml中的SQL标签,就可以更改代码中需要的SQL语句。易于维护,方便修改的优点不言而喻。
参考文档:Mybatis中文网
评论区