1.foreach
collection为集合名
item 表示集合中每一个元素 进行迭代时的别名,
index 指定一个名字,用于表示在迭代过程中,每次迭代到的位置,
open 表示该语句以什么开始,
separator 表示在每次进行迭代之间以什么符号作为分隔 符,
close 表示以什么结束。
例如:
<select id="selectByIds" resultType="Customer">
select *
from WXBHPT_V_Customers
<where>
<if test='ids!=null '>
customid in
<foreach collection="ids" item="id" open="(" separator="," close=")" index="index">
#{id}
</foreach>
</if>
</where>
order by customid;
</select>
2.choose
有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个。MyBatis提供了choose 元素,按顺序判断when中的条件出否成立,如果有一个成立,则choose结束。当choose中所有when的条件都不满则时,则执行 otherwise中的sql。类似于Java 的switch 语句,choose为switch,when为case,otherwise则为default。
if是与(and)的关系,而choose是或(or)的关系。
<select id="getStudentListChoose" parameterType="Student" resultMap="BaseResultMap">
SELECT * from STUDENT WHERE 1=1
<where>
<choose>
<when test="Name!=null and student!='' ">
AND name LIKE CONCAT(CONCAT('%', #{student}),'%')
</when>
<when test="hobby!= null and hobby!= '' ">
AND hobby = #{hobby}
</when>
<otherwise>
AND AGE = 15
</otherwise>
</choose>
</where>
</select>
3.where
where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。
<select id="getStudentListWhere" parameterType="Object" resultMap="BaseResultMap">
SELECT * from STUDENT
<where>
<if test="name!=null and name!='' ">
NAME LIKE CONCAT(CONCAT('%', #{name}),'%')
</if>
<if test="hobby!= null and hobby!= '' ">
AND hobby = #{hobby}
</if>
</where>
</select>
4.set
使用set标签可以将动态的配置set关键字,和剔除追加到条件末尾的任何不相关的逗号。
<update id="updateStudent" parameterType="Object">
UPDATE STUDENT
<set>
<if test="name!=null and name!='' ">
NAME = #{name},
</if>
<if test="hobby!=null and hobby!='' ">
MAJOR = #{major},
</if>
<if test="hobby!=null and hobby!='' ">
HOBBY = #{hobby}
</if>
</set>
WHERE ID = #{id};
</update>
5.trim
trim标记是一个格式化的标记,主要用于拼接sql的条件语句(前缀或后缀的添加或忽略),可以完成set或者是where标记的功能。
trim属性主要有以下四个
prefix:前缀覆盖并增加其内容
suffix:后缀覆盖并增加其内容
prefixOverrides:前缀判断的条件
suffixOverrides:后缀判断的条件
如果同时有prefixOverrides,suffixOverrides 表示会用prefix,suffix覆盖Overrides中的内容。
如果只有prefixOverrides,suffixOverrides 表示删除开头的或结尾的xxxOverides指定的内容。
<update id="updateByPrimaryKey" parameterType="Object">
update student set
<trim suffixOverrides="," >
<if test="name != null ">
NAME=#{name},
</if>
<if test="hobby != null ">
HOBBY=#{hobby},
</if>
</trim> where id=#{id}
</update>
如果name和hobby的值都不为空的话,会执行如下语句
update student set NAME=‘XX’,HOBBY=‘XX’ /,/ where id=‘XX’
会忽略最后一个“,” ;
<select id="selectByNameOrHobby" resultMap="BaseResultMap">
select * from student
<trim prefix="WHERE" prefixOverrides="AND | OR">
<if test="name != null and name.length()>0"> AND name=#{name}
</if>
<if test="hobby != null and hobby.length()>0"> AND hobby=#{hobby}
</if>
</trim>
</select>
如果name和hobby的值都不为空的话,会执行如下语句
select * from user WHERE /and/ name = ‘xx’ and hobby= ‘xx’
会为片段添加 “WHERE” 前缀,并忽略第一个 “and” ;
<insert id="insert" parameterType="Object">
insert into student <trim prefix="(" suffix=")" suffixOverrides="," >
<if test="name != null ">
NAME,
</if>
<if test="hobby != null ">
HOBBY,
</if>
</trim> <trim prefix="values(" suffix=")" suffixOverrides="," >
<if test="name != null ">
#{name},
</if>
<if test="hobby != null ">
#{hobby},
</if>
</trim>
</insert>
可以为生成格式正确的insert语句。