Mybatis框架中的动态SQL
1.主要元素
元素 | 作用 | 备注 |
if | 语句判断 | 单选择 |
choose(when、otherwise) | 语句选择,与case when类似 | 多选择 |
trim(where、set) | 处理语句拼装 | |
foreach | 循环遍历 | 遍历插入、删除、in语句等 |
2. if
用于查询语句中。
注意:where后面跟“1 = 1”,防止语句拼装错误。
<select id="findByMsg" resultMap="bookMap1" parameterType="string">
select <include refid="bookSql"/>
from t_book
where 1 = 1
<if test="name != null and name != '' ">
and name = #{name}
</if>
</select>
用于更新语句中。
注意:set会自动处理语句末尾的逗号。
<update id="updateBook" parameterType="mybatis.domain.Book">
update t_book
set
<if test="author != null and author !=''">
author = #{author},
</if>
<if test="name != null and name !=''">
name = #{name},
</if>
<if test="title != null and title !=''">
title = #{title},
</if>
<if test="price != null and price !=''">
price = #{price},
</if>
<if test="des != null and des !=''">
des = #{des},
</if>
<if test="userId != null and userId !=''">
user_id = #{userId}
</if>
where bid = #{id}
</update>
3.choose(when、otherwise)
用于动态判断和拼装sql,满足不同要求。
<select id="findBook" parameterType="mybatis.domain.Book" resultMap="bookMap1">
select <include refid="bookSql"/>
from t_book
where 1 = 1
<choose>
<when test="name != null and name !='' ">
and name like concat('%',#{name},'%')
</when>
<when test="author != null and author != '' ">
and author = #{author}
</when>
<otherwise>
and name is not null
</otherwise>
</choose>
</select>
4.trim(where、set)
对于
上面特殊语句“1 = 1”,我们可以用where去除。
<select id="findByMsg" resultMap="bookMap1" parameterType="string">
select <include refid="bookSql"/>
from t_book
<where>
<if test="name != null and name != '' ">
and name = #{name}
</if>
</where>
</select>
trme可以调整特殊的sql语句,比如and、or。
prefix | 语句的前缀 |
prefixOverrides | 即将去掉的内部语句的前缀 |
suffix | 语句的后缀 |
suffixOverrides |
<select id="findByMsg" resultMap="bookMap1" parameterType="string">
select <include refid="bookSql"/>
from t_book
<trim prefix="where" prefixOverrides="and" suffix="" suffixOverrides="">
<if test="name != null and name !=''">
and name = #{name}
</if>
</trim>
</select>
<!--set末尾遇到逗号自动去掉 -->
<update id="updateBook" parameterType="mybatis.domain.Book">
update t_book
set
<if test="author != null and author !=''">
author = #{author},
</if>
<if test="name != null and name !=''">
name = #{name},
</if>
<if test="title != null and title !=''">
title = #{title},
</if>
<if test="price != null and price !=''">
price = #{price},
</if>
<if test="des != null and des !=''">
des = #{des},
</if>
<if test="userId != null and userId !=''">
user_id = #{userId}
</if>
where bid = #{id}
</update>
也可用trim元素自动管理后缀的逗号。
<update id="updateBook" parameterType="mybatis.domain.Book">
update t_book
<trim prefix="set" suffixOverrides=",">
***
</trim>
where bid = #{id}
</update>
5.foreach批量操作
collection | 传递到语句中的参数,可以是数组、List、Set等集合 |
item | 循环中当前的元素信息 |
index | 当前元素在集合中的位置下标 |
open/close | 以定义的符号来包裹每一组集合元素 |
separator | 每次遍历元素之后的分隔符号 |
批量插入。
<insert id="batchInsertBook">
insert into t_book(bid, author, name, title, price, des,user_id)
values
<foreach collection="list" item="item" open="" separator="," close="" index="key">
(#{item.id},#{item.author},#{item.name},#{item.title},#{item.price},#{item.des},#{item.userId})
</foreach>
</insert>
批量删除
<delete id="batchDelete">
delete
from t_book
where bid in
<foreach collection="list" item="item" open="(" separator="," close=")" index="key">
#{item}
</foreach>
</delete>
7.bind自定义变量
可以用于不同数据库之间特殊字段的调整与转换。like模糊查询时,在Mysql中使用“%”,但是在Oracle中使用的时“||”,使用bind可以提高移植性。
<!--可以传入多个查询字段,返回一个list-->
<select id="find" resultType="mybatis.domain.Book">
<bind name="nameLike" value=" '%' + name + '%' "/> <!--可以多次定义-->
select <include refid="bookSql"/>
from t_book
where name like #{nameLike}
</select>