MyBatis-动态SQL应用-mapper.xml
- if语句
select * from user where 1 = 1
<if test="username != null">
and username=#{username}
</if>
<if test="username != null">
and sex=#{sex}
</if>
test 是对条件的验证,可以理解为 if( username != null) 注意变量直接写变量名,在引号里直接处理,MyBatis特有的写法 , 这里为了关联, 用到了 where 1 = 1 ,做恒等,and关联 , 对越where 1 = 1 这样的恒等,MyBatis 有自己的处理方式
- where语句
select * from user
<where>
<if test="username != null">
username=#{username}
</if>
<if test="username != null">
and sex=#{sex}
</if>
</where>
where使用,代替了上面的恒等 1=1 ,而且,where标签,会自动过滤掉 and/or 这样的前缀 , 比如这里 , username一但值为空, 直接输出的条件语句 where and sex = ? 这样是错误的,但是MyBatis 有自己的处理方式 , 会过滤掉多余的前缀, 输出条件语句 where sex = ?
- choose(when,otherwise)语句
select * from user
<where>
<choose>
<when test="id !='' and id != null">
id=#{id}
</when>
<when test="username !='' and username != null">
and username=#{username}
</when>
<otherwise>
and sex=#{sex}
</otherwise>
</choose>
</where>
这里choose / when / when / otherwise 可以理解为 choose 标签内, 是可以选择的条件, when / when/otherwise 做 if (条件)else if (条件)else 先去判断第一个条件 , 如果满足 , 选择,不满足判断下一个,知道when结束,都没有满足条件,会直接进入othersies
- foreach 语句
select * from user
<where>
<foreach collection="ids" item="id" open="and id in (" close=") " separator=",">
#{id}
</foreach>
</where>
这里,ids 可以只array数组,可以是list集合 item 是给ids在本循环里的子单位 ,每一个子元素的别名 ,open 是循环开始标记 cloes 循环结束标记 ,separator 循环中 每次子元素之间的 间隔符 输出的语句应该是这样 select * from user where id in (?,?,?) 数组/集合中有多少子元素循环多少次
- sql 语句 include 嵌入
<sql id="mySQLIf">
<if test="username != null and username != ''">
AND username = #{username}
</if>
<if test="sex != null and sex != ''">
AND sex = #{sex}
</if>
</sql>
<sql id="mySQLList">
username,sex,age
</sql>
可以是逻辑语句,子查询,sql 查询字段等,上面编辑好了,接下来嵌入
select * from user <where> <include refid="mySQLIf"></include> </where>
输出SQL select * from user where username = ? and sex = ?
- set 语句
update user u
<set>
<if test="username != null and username != ''">
u.username = #{username},
</if>
<if test="sex != null and sex != ''">
u.sex = #{sex}
</if>
</set>
<where>
id=#{id}
</where>
用于修改,修改条件 如果变量都不为空, 都生效, 输出SQL update user u set username = ? ,sex = ? where id = ?
- trim 语句
- 案例一:
update user u
<set>
<if test="username != null and username != ''">
u.username = #{username},
</if>
<if test="sex != null and sex != ''">
u.sex = #{sex}
</if>
</set>
where id=#{id}
set语句的写法,下面我用trim重写它
update user u
<trim prefix="set" suffixOverrides=",">
<if test="username != null and username != ''">
u.username = #{username},
</if>
<if test="sex != null and sex != ''">
u.sex = #{sex},
</if>
</trim>
where id=#{id}
效果是一样的 trim 代码块执行前加set 去掉最后一个 ,prefix 前缀 suffixOverrides 后缀去掉(最后一个)
- 案例二:
select * from user <where> <if test="username != null"> username=#{username} </if> <if test="username != null"> and sex=#{sex} </if> </where>
where if 连用的写法 , 下面我用trim替换他
select * from user
<trim prefix="where" prefixOverrides="and | or">
<if test="username != null">
and username=#{username}
</if>
<if test="sex != null">
and sex=#{sex}
</if>
</trim>
前缀 where 后面接trim代码块内的 代码 ,去掉第一个 and 或者 or 这里会去掉 and ,prefixOverrides 去掉最前面的 and 或者 or
今天就说这么多,这就是我给大家分享的 MyBatis动态sql,* 可以忽略不看,这里只是演示方便
随笔记录,方便自己学习
CHenyb 2018-11-16