if语句
<if>
判断语句,用于单条件分支条件判断
使用<if>
元素对参数username和jobs进行非空判断,并动态组装SQL:
select * from t_person where 1=1
<if test="username !=null and username !=''">
and username like concat('%',#{username}, '%')
</if>
<if test="jobs !=null and jobs !=''">
and jobs = #{jobs}
</if>
choose 、when 、 otherwise 语句
<choose>
用于多条件分支判断,相当于Java
中 switch ... case ... default
使用<choose>
及其子元素依次对条件进行非空判断,并动态组装SQL:
select * from t_person where 1=1
<choose>
<when test="username !=null and username !=''">
and username like concat('%',#{username}, '%')
</when>
<when test="jobs !=null and jobs !=''">
and jobs= #{jobs}
</when>
<when test="phone != null and phone != ''">
and phone= #{phone}
</when>
<otherwise>
1=1
</otherwise>
</choose>
加入了条件“where 1=1”
后,既保证了where
后面的条件成立,又避免了where
后面第一个词是and
或者or
之类的关键词。
当用户一个条件都不选时,可以在<otherwise>中写上1=1
让语法成立,反之,若选择了条件则会返回正常结果
where 、 trim 语句
针对上述情况中“where 1=1”,在MyBatis的SQL中就可以使用<where>
或<trim>
元素进行动态处理
<select id="selectPerson">
select * from t_person
<trim prefix="where" prefixOverrides="and">
<if test="username !=null and username !=''">
and username like concat('%',#{username}, '%')
</if>
<if test="jobs !=null and jobs !=''">
and jobs= #{jobs}
</if>
</trim>
</select>
<select id="selectPerson">
select * from t_person
<where>
<if test="username !=null and username !=''">
and username like concat('%',#{username},'%')
</if>
<if test="jobs !=null and jobs !=''">
and jobs= #{jobs}
</if>
</where>
</select>
trim
标签:prefixOverrides
=“and|or” prefix
=“where” suffixOverrides
=“” suffix
=“”
属性 | 介绍 | 常用值 |
---|---|---|
prefixOverrides | 前缀覆盖 | and或or |
prefix | 前缀 | 一般是where或者set(只执行一次) |
suffixOverrides | 后缀覆盖 | 一般是逗号, |
suffic | 后缀 | 一般是更新语句的查询条件 |
<trim>
标签可以处理<where>
和<set>
标签所需要处理的问题,根据需求使用即可,举个例子,下面通过输入哪些参数,就更新哪些参数:
<update id="">
update t_person
<trim prefix="set" suffix="where id=#{personId}" suffixOverrides="," >
<if test="username !=null and username !=''">
username = #{username}
</if>
<if test="jobs !=null and jobs !=''">
jobs = #{jobs}
</if>
<if test="phone !=null and phone !=''">
phone = #{phone}
</if>
</trim>
</update>
使用和元素对username和jobs进行更新判断,并动态组装SQL。这样就只需要传入想要更新的字段即可
<update id="updatePerson" parameterType="com.ssy.po.Person">
update t_person
<set>
<if test="username !=null and username !=''">
username=#{username},
</if>
<if test="jobs !=null and jobs !=''">
jobs=#{jobs},
</if>
</set>
where id=#{id}
</update>
使用<set>
和<if>
结合的方式来组装update
语句。<set>
(进行更新字段时,要确保更新的字段不能为空)元素会动态前置 SET
关键字,同时也会消除多余的‘,’
。
foreach语句
foreach五大标签:
标签 | 描述 |
---|---|
collection | 该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,默认使用list |
item | 表示集合中每一个元素进行迭代时的别名,随便起的变量名; |
index | 也就是索引,用于表示在迭代过程中,每次迭代到的位置; |
open | 表示该语句以什么开始,常用“(”; |
separator | 表示在每次进行迭代之间以什么符号作为分隔符,常用“,”; |
close | 表示以什么结束,常用“)”; |
对于collection
如果dao层接口没有用@Param
注解时,默认使用list
作为集合,如果是数组则为array
。
如果不在open、close、separate
标签中明确,就得在mapper循环前后加上()
,以及英文逗号,
批量插入操作,传入参数是一个list
,数组中存放的对象,示例:
<insert id="batchInsert">
insert into t_person (a, b, c, d)
values
<foreach collection="list" item="item" separator=",">
(#{item.a,jdbcType=BIGINT}, #{item.b,jdbcType=VARCHAR},
#{item.c,jdbcType=DECIMAL}, #{item.d,jdbcType=DECIMAL})
</foreach>
</insert>
批量查询操作,通常和in
语句已使用,示例:
List<ActSmsSendAudit> batchQueryByIdList(@Param("idList") List<Integer> idList);
<select id="batchQueryByIdList">
select *
from t_person
where id in
<foreach collection="idList" item="item" index="index" open="(" close=")" separator=",">
#{item}
</foreach>
</select>
如果不使用foreach
直接写sql语句:
<select id="batchQueryByIdList">
select *
from t_person
where id in ( , , )
</select>
可以看出,如果使用MyBatis的动态sql可以传任意的数量id,使用传统查询只能固定数量的数据。
批量更新操作
<update id="updateActivityStatus" parameterType="int" >
UPDATE marketing_activity_list SET status=0
WHERE
<foreach collection="list" item="id" separator="or">
activity_seq = #{id}
</foreach>
</update>
灵活运用foreach
可以做到事半功倍的效果!