MyBatis 的一个强大的特性之一通常是它的动态 SQL 能力。如果你有使用 JDBC 或其他相似框架的经验,你就明白条件地串联 SQL 字符串在一起是多么的痛苦,确保不能忘了空格或在列表的最后省略逗号。动态 SQL 可以彻底处理这种痛苦。 MyBatis 采用功能强大的基于 OGNL 的表达式来消除其他元素。
test语句为true则加上if标签中sql语句块。(加上1=1是为了避免所有if标签的test语句为false时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>
删除标签中sql语句块最前面的一个and,并加上一个where。
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>
只有一个成立则其他不成立
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>
删除标签中sql语句块最后一个逗号,并在开头加上set关键字。(加上id=#{}是为了方式if标签中test语句都为false时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>
加上或去掉特定字符串
属性
当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>
将传入值变为另一个值(适用于模糊查询时加上%或_)。
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>
循环生成标签中的sql语句块(适用于in查询)
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>
将特定的sql语句起一个别名,以便引用(适用于多列数据的选取)。
<sql id="columns">
select accountNoIn,accountNoOut
sql>
<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>