1.概述
MyBatis 的一个强大的特性之一通常是它的动态 SQL 能力。如果你有使用 JDBC 或其他相似框架的经验,你就明白条件地串联 SQL 字符串在一起是多么的痛苦,确保不能忘了空格或在列表的最后省略逗号。动态 SQL 可以彻底处理这种痛苦。 MyBatis 采用功能强大的基于 OGNL 的表达式来消除其他元素。
- 可以理解动态sql为,通过逻辑标签来在不同的条件下自动生成不同的sql语句。
2.相关标签
2.1.if
test语句为true则加上if标签中sql语句块。(加上1=1是为了避免所有if标签的test语句为false时sql语法错误)
- 当accountNoIn存在、accountNoOut不存在时,sql相当于:
select * from transferlog where 1=1 and accountNoIn=?
<select id="selectByAccountInAndOut" resultType="com.bear.sxt.pojo.TransferLog">
select * from transferlog where 1=1
<if test="accountNoIn!=null and accountNoIn!=''">
and accountNoIn=#{accountNoIn}
</if>
<if test="accountNoOut!=null and accountNoOut!=''">
and accountNoOut=#{accountNoOut}
</if>
</select>
2.2.where
删除标签中sql语句块最前面的一个and,并加上一个where。
- 当accountNoIn存在、accountNoOut不存在时,sql相当于:
select * from transferlog where accountNoIn=?
<select id="selectByAccountInAndOut" resultType="com.bear.sxt.pojo.TransferLog">
select * from transferlog
<where>
<if test="accountNoIn!=null and accountNoIn!=''">
and accountNoIn=#{accountNoIn}
</if>
<if test="accountNoOut!=null and accountNoOut!=''">
and accountNoOut=#{accountNoOut}
</if>
</where>
</select>
2.3.choose、when、otherwise
只有一个成立则其他不成立
- 当accountNoIn、accountNoOut均存在时,sql相当于:
select * from transferlog where accountNoIn=?
<select id="selectByAccountInAndOut" resultType="com.bear.sxt.pojo.TransferLog">
select * from transferlog
<where>
<choose>
<when test="accountNoIn!=null and accountNoIn!=''">
and accountNoIn=#{accountNoIn}
</when>
<when test="accountNoOut!=null and accountNoOut!=''">
and accountNoOut=#{accountNoOut}
</when>
</choose>
</where>
</select>
2.4.set
删除标签中sql语句块最后一个逗号,并在开头加上set关键字。(加上id=#{}是为了方式if标签中test语句都为false时sql语法错误)
- 当accountNoIn、accountNoOut均存在时,sql相当于:
update transferlog set id=?,accountNoIn=?,accountNoOut=?
<update id="updateTransferLog" parameterType="com.bear.sxt.pojo.TransferLog">
update transferlog
<set>
/*id=#{id}是为了防止set标签中内容为空时产生语法错误*/
id=#{id},
<if test="accountNoIn!=null and accountNoIn!=''">
accountNoIn=#{accountNoIn},
</if>
<if test="accountNoOut!=null and accountNoOut!=''">
accountNoOut=#{accountNoOut},
</if>
</set>
where id=#{id}
</update>
2.5.trim
加上或去掉特定字符串
-
属性
- prefix="":前面加上字符串
- prefixOverrides="":前面去掉字符串
- suffix="":后面加上字符串
- suffixOverrides="":后面去掉字符串
-
当accountNoIn存在、accountNoOut不存在时,sql相当于:
select *from transferlog where accountNoIn=?
<select id="selectByAccountInAndOut" resultType="com.bear.sxt.pojo.TransferLog">
select * from transferlog
<trim prefix="where" prefixOverrides="and">
<if test="accountNoIn!=null and accountNoIn!=''">
and accountNoIn=#{accountNoIn}
</if>
<if test="accountNoOut!=null and accountNoOut!=''">
and accountNoOut=#{accountNoOut}
</if>
</trim>
</select>
2.6.bind
将传入值变为另一个值(适用于模糊查询时加上%或_)。
- 若传入了accountInNo为lily,则sql语句相当于
update transferlog set id=?,accountNoIn=?
参数为:id值,%liliy%
<update id="updateTransferLog" parameterType="com.bear.sxt.pojo.TransferLog">
update transferlog
<bind name="accountNoIn" value="'%'+accountNoIn + '%'"/>
<set>
id=#{id},
<if test="accountNoIn!=null and accountNoIn!=''">
accountNoIn=#{accountNoIn},
</if>
<if test="accountNoOut!=null and accountNoOut!=''">
accountNoOut=#{accountNoOut},
</if>
</set>
where id=#{id}
</update>
2.7.foreach
循环生成标签中的sql语句块(适用于in查询)
- 如传入了一个list,list中包含了几个id号,如[1,2,3,4],则下列sql语句为:
select * from transferlog where id in (1,2,3,4)
<select id="selectById" resultType="com.bear.sxt.pojo.TransferLog">
select * from transferlog where id in
<foreach collection="list" item="item" open="(" close=")" separator=",">
#{item}
</foreach>
</select>
2.8.sql
将特定的sql语句起一个别名,以便引用(适用于多列数据的选取)。
- 将select accountNoIn,accountNoOut语句块起名为columns
<sql id="columns">
select accountNoIn,accountNoOut
</sql>
- 引用columns语句块
<select id="selectById" resultType="com.bear.sxt.pojo.TransferLog">
<include refid="columns"></include>
from transferlog where id in
<foreach collection="list" item="item" open="(" close=")" separator=",">
#{item}
</foreach>
</select>