# 7.MyBatis-动态SQL
动态的去拼接sql语句
# 1.if
if标签可通过test属性的表达式进行判断,若表达式的结果为true,则标签中的内容会执行;反之标签中的内容不会执行
接口方法
List<Emp> getEmpByCondition(Emp emp);
1
xml
为了防止empName为null直接拼接and导致sql语句报错,可以直接在后面加一个1=1
<select id="getEmpByCondition" resultType="com.demo.pojo.Emp">
select * from t_emp where 1=1
<if test="empName!=null and empName!=''">
emp_name=#{empName}
</if>
<if test="sex!=null and sex!=''">
and sex=#{sex}
</if>
<if test="age!=null and age!=''">
and age=#{age}
</if>
<if test="email!=null and email!=''">
and email=#{email}
</if>
</select>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 2.where
上例也可用where实现
<select id="getEmpByCondition" resultType="com.demo.pojo.Emp">
select * from t_emp
<where>
<if test="empName!=null and empName!=''">
emp_name=#{empName}
</if>
<if test="sex!=null and sex!=''">
and sex=#{sex}
</if>
<if test="age!=null and age!=''">
and age=#{age}
</if>
<if test="email!=null and email!=''">
and email=#{email}
</if>
</where>
</select>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
where会根据是否有满足的条件,自动加where,并且将内容前多余的and和or去掉
注意:where只能去除内容前的and和or,内容后的不能去除
# 3.trim
如果把and或or加在内容后面的话,where实现不了,我们可以用trim实现
<select id="getEmpByCondition" resultType="com.demo.pojo.Emp">
select * from t_emp
<trim prefix="where" suffix="" prefixOverrides="" suffixOverrides="and|or">
<if test="empName!=null and empName!=''">
emp_name=#{empName} and
</if>
<if test="sex!=null and sex!=''">
sex=#{sex} or
</if>
<if test="age!=null and age!=''">
age=#{age} and
</if>
<if test="email!=null and email!=''">
email=#{email}
</if>
</trim>
</select>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
prefix/suffix:在trim标签的前缀/后缀加上指定内容
prefixOverrides/suffixOverrides:在trim标签的前缀或后缀去掉指定内容
当没有条件满足时,也不会添加where
# 4.choose,when,otherwise
choose,when,otherwise相当于if...else if...else
示例
接口方法:
List<Emp> getEmpByConditionChoose(Emp emp);
1
xml
<select id="getEmpByConditionChoose" resultType="com.demo.pojo.Emp" >
select * from t_emp where
<choose>
<when test="empName!=null and empName!=''">
emp_name=#{empName}
</when>
<when test="sex!=null and sex!=''">
sex=#{sex}
</when>
<when test="age!=null and age!=''">
age=#{age}
</when>
<when test="email!=null and email!=''">
email=#{email}
</when>
<otherwise>
did=1
</otherwise>
</choose>
</select>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
注:when标签至少有一个,otherwise标签最多有一个
# 5.foreach
实现批量操作
示例:批量删除操作
接口方法:
int deleteMoreByArray(@Param("eids") int[] eids);
1
xml(方式一:in (1,2,3))
<delete id="deleteMoreByArray">
delete from t_emp where eid in
<foreach collection="eids" item="eid" separator="," open="(" close=")">
#{eid}
</foreach>
</delete>
1
2
3
4
5
6
2
3
4
5
6
(方式二:where eid=1 or eid=2 or eid=3)
<delete id="deleteMoreByArray">
delete from t_emp where
<foreach collection="eids" item="eid" separator="or">
eid=#{eid}
</foreach>
</delete>
1
2
3
4
5
6
2
3
4
5
6
collection:需要处理的集合或数组
item:遍历出来的每一个值
separator:分隔符
open:遍历结束后左边加上指定内容
close:遍历结束后右边加上指定内容
示例2:批量添加
接口方法:
void insertBatchEmp(@Param("emps") List<Emp> emps);
1
xml
<insert id="insertBatchEmp">
insert into t_emp values
<foreach collection="emps " item="emp" separator=",">
(null,#{emp.empName},#{emp.age},#{emp.sex},#{emp.email},null)
</foreach>
</insert>
1
2
3
4
5
6
2
3
4
5
6
# 6.sql标签
sql标签用于将常用的字段提取出来,然后使用时就不用写字段名直接引用即可
示例如下:
<sql id="empColumns">eid,emp_name,age,sex,email</sql>
<select id="getEmpByCondition" resultType="com.demo.pojo.Emp">
select <include refid="empColumns"/> from t_emp
<trim prefix="where" suffix="" prefixOverrides="" suffixOverrides="and|or">
<if test="empName!=null and empName!=''">
emp_name=#{empName} and
</if>
<if test="sex!=null and sex!=''">
sex=#{sex} or
</if>
<if test="age!=null and age!=''">
age=#{age} and
</if>
<if test="email!=null and email!=''">
email=#{email}
</if>
</trim>
</select>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18