根据不同的条件需要执行不同的SQL命令,称为动态SQL,用法:动态SQL在mapper.xml中添加逻辑判断等。此处用的是转账记录log表,有id,accin(收款账号),accout(转账账号),money(金额)
if
//代码示例
<select id="selByAccInOutIf" resultType="Log">
select * from log where 1=1
//ONGI表达式,直接写key或对象属性,不需要添加任何特殊字符串
<if test="accin!=null and accin!=''">
and accin=#{accin}
</if>
<if test="accout!=null and accout!=''">
and accout=#{accout}
</if>
</select>
当输入accin和accout时的SQL语句。
当只输入accout,accin为空时的SQL语句。
当只输入accin,accout为空时的SQL语句
当accout和accin都为空时的SQL语句
where
当编写where标签时,如果where内容中第一个是and的话,去掉第一个and。如果有内容则生成where关键字,否则无。
//代码示例 where标签包含if标签
<select id="selWhere" resultType="Log">
select * from log
<where>
<if test="accin!=null and accin!=''">
and accin=#{accin}
</if>
<if test="accout!=null and accout!=''">
and accout=#{accout}
</if>
</where>
当输入accin和accout时的SQL语句。
当只输入accout,accin为空时的SQL语句。
当只输入accin,accout为空时的SQL语句
当accout和accin都为空时的SQL语句
choose、when、otherwise
当只有一个成立时,其他都不执行。where包含choose,choose包含when、otherwise,相当于switch。
//代码示例
<select id="selChoose" resultType="Log">
select * from log
<where>
<choose>
<when test="accin!=null and accin!=''">
and accin=#{accin}
</when>
<when test="accout!=null and accout!=''">
and accout=#{accout}
</when>
</choose>
</where>
</select>
当输入accin和accout时的SQL语句。
当只输入accin,accout为空时的SQL语句
当只输入accout,accin为空时的SQL语句。
当accout和accin都为空时的SQL语句
set
用在update的SQL语句中,作用时去掉最后一个逗号。如果set标签里面有内容,则生成set关键字,若没有内容,则SQL语法错误。
<update id="updAccInOut" parameterType="Log">
update log
<set>
id=#{id}, //防止<set>中没有内容,SQL语句错误
<if test="accin!=null and accin!=''">
accin=#{accin},
</if>
<if test="accout!=null and accout!=''">
accout=#{accout},
</if>
</set>
where id=#{id}
</update>
当输入accin和accout时的SQL语句。
当只输入accin,accout为空时的SQL语句
当只输入accout,accin为空时的SQL语句。
当accout和accin都为空时的SQL语句
trim
trim相当于where和set的整合,在最前面加点,或者在最后面去掉点东西,一般遵循规则:先去掉再添加。
//代码示例
<select id="selByLog" resultType="Log" parameterType="Log">
select * from log
<trim prefix="where" prefixOverrides="and">
and accin=#{accin}
</trim>
</select>
SQL语句
使用trim模拟update
//代码示例
<update id="updByTrim" parameterType="Log">
update log
<trim prefix="set" suffixOverrides=",">
a=a,
</trim>
where id=100
</update>
SQL语句
bind
作用:给参数重新赋值,常用在模糊查询上,或者在原内容基础上加点内容
//代码示例
<select id="selByBind" parameterType="Log" resultType="Log">
<bind name="accin" value="'%' +accin+ '%'"/>
select * from log
where accin like #{accin}
</select>
foreach
循环参数内容,作用:可在内容前后添加内容,以及添加分隔符功能。
使用场景:in查询、批量新增中
in查询
collection:要遍历的集合
item:迭代变量
//in查询代码示例
<select id="selByIn" parameterType="list" resultType="Log">
select * from log where id in
<foreach collection="list" item="abc" open="(" close=")" separator=",">
#{abc}
</foreach>
</select>
SQL语句
批量新增
//代码示例
<insert id="insLog" parameterType="list">
insert into log values
<trim suffixOverrides=",">
<foreach collection="list" item="abc">
(default,#{abc},2,2),
</foreach>
</trim>
</insert>
注意
mybatis中批量新增效率很低,如果非要需要批量新增时,要满足以下:
- SQL命令
insert into log values (default,1,2,3),(default,2,2,3),(default,3,2,3)
2.opensession()必须指定
sqlsessionFactory.openssion(ExecutorType.BATCH)
sql和include
某些SQL片段如果希望复用,可使用SQL标签定义这个片段。
//代码示例
<select id="">
select <include refid="log"></include>
from log
</select>
<sql id="log">
id,accout,accin,money
</sql>