动态SQL
大约 3 分钟
使用动态SQL,可以完成sql标签中语句的拼接,从而达到动态执行sql的效果。
if 标签
if 标签可以根据传值不同,判断是否拼接标签体的SQL。(纯拼接,不会进行SQL语法检查)
<if test="">
</if>- 在 test 中给出关于传值参数(同占位符的key)的条件判断,不能用 && ||,只能用 and or。
<select id="selectByMultiCondition" resultType="car">
select * from t_car where 1=1
<if test="brand != null and brand != ''">
and brand like #{brand}"%"
<!--加个and,使得拼接后符合sql语法-->
</if>
<if test="guidePrice != null and guidePrice != ''">
and guide_price >= #{guidePrice}
</if>
<if test="carType != null and carType != ''">
and car_type = #{carType}
</if>
</select>
<!--最复杂的sql为:-->
select * from t_car where 1=1
and brand like #{brand}"%"
and guide_price >= #{guidePrice}
and car_type = #{carType}choose 标签
根据传值参数,选择一条分支进行sql拼接(不进行sql语法检查)。
<choose>
<when test="">
...
</when>
<when test="">
...
</when>
...
<otherwise>
...
</otherwise>
</choose>- test 同if
<select id="selectByChoose" resultType="Car">
select * from t_car and
<!--加个and,使得拼接后符合sql语法-->
<choose>
<when test="brand != null and brand != ''">
brand like "%"#{brand}"%"
</when>
<when test="guidePrice != null and guidePrice != ''">
guide_price > #{guidePrice}
</when>
<otherwise>
car_type = #{carType}
</otherwise>
</choose>
</select>where 标签
where标签的作⽤:专门用于where子句生成。
- 所有条件都为空时,where标签保证不会⽣成where⼦句。
- 需要生成where子句时,自动添加 where 关键字。
- ⾃动去除某些条件前⾯多余的and或or。(修正 where and、where or 为 where)
- 常配合 if 标签使用。
- 先解析标签体、子标签,再进行where关键字的拼接。
trim 标签
trim标签的属性:
- prefix:在trim标签中的语句前添加内容
- suffix:在trim标签中的语句后添加内容
- prefixOverrides:前缀覆盖掉(去掉)
- suffixOverrides:后缀覆盖掉(去掉)
<!--prefix="where" 添加前缀 where-->
<!--suffixOverrides="and|or" 去掉后缀 and 或 or-->
<trim prefix="where" suffixOverrides="and|or">
<if test="brand != null and brand != ''">
brand like #{brand}"%" and
</if>
<if test="guidePrice != null and guidePrice != ''">
guide_price >= #{guidePrice} and
</if>
</trim>- 拼接时,若标签体为空,则不会添加 prefix、suffix前后缀(if 都不成立时,不添加 where 前缀)
set 标签
set 标签的作⽤:专门用于 set 子句生成。(updata 语句中)
- 自动添加 set 关键字
- 自动去除 set子句末尾的
,
- 先解析标签体、子标签,再进行set关键字的拼接。
<update id="updateWithSet">
update t_car
<set>
<if test="carNum != null and carNum != ''">car_num = #{carNum},</if>
<if test="brand != null and brand != ''">brand = #{brand},</if>
<if test="guidePrice != null and guidePrice != ''">guide_price = #{guidePrice},</if>
<if test="produceTime != null and produceTime != ''">produce_time = #{produceTime},</if>
<if test="carType != null and carType != ''">car_type = #{carType},</if>
<!--多余的 , 会被抹去-->
</set>
where id = #{id}
</update>foreach 标签
用来循环参数中的数组或集合,动态⽣成sql。如:批量删除、批量添加。
<foreach collection="key" item="ele" separator="">
</foreach>key:同占位符的key,用以从参数中获取集合或数组
ele:集合或数组中的元素,在标签体中可用
separator:每次循环生成的标签体之间的分隔符。
- 需要使用 @Param 注解指定参数为数组或集合
int deleteBatchByForeach2(
@Param("ids") Long[] ids
);动态生成sql:
<delete id="deleteBatchByForeach2">
delete from t_car where
<foreach collection="ids" item="id" separator="or">
id = #{id}
</foreach>
</delete>拼接成:
delete from t_car where
id = 166
or id = 188
...sql、include 标签
sql标签⽤来声明sql⽚段。
include标签⽤来将声明的sql⽚段包含到某个sql语句当中。
- 二者配合,实现sql的复用和易维护。
- 仅进行sql拼接,不进行语法检错。
<!--定义sql标签-->
<sql id="carCols">
id,
car_num carNum,
brand,
guide_price guidePrice,
produce_time produceTime,
car_type carType
</sql>
<select id="selectAllRetMap" resultType="map">
select
<!--使用include 引入sql片段-->
<include refid="carCols"/>
from t_car
</select>拼接为:
select
id,
car_num carNum,
brand,
guide_price guidePrice,
produce_time produceTime,
car_type carType
from t_car