Mybatis动态SQL
# Mybatis动态SQL
# if
<!--
if标签:判断;
test:判断条件; java代码怎么写,它怎么写
where标签:解决 where 后面 语法错误问题(多and、or, 无任何条件多where)~
-->
<select id="queryEmpByNameAndSalary" resultType="com.joe.entity.Emp">
select * from t_emp
<where>
<if test="name != null">
emp_name= #{name}
</if>
<if test="salary != null">
and emp_salary = #{salary};
</if>
</where>
</select>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# where
<!--
if标签:判断;
test:判断条件; java代码怎么写,它怎么写
where标签:解决 where 后面 语法错误问题(多and、or, 无任何条件多where)~
-->
<select id="queryEmpByNameAndSalary" resultType="com.joe.entity.Emp">
select * from t_emp
<where>
<if test="name != null">
emp_name= #{name}
</if>
<if test="salary != null">
and emp_salary = #{salary};
</if>
</where>
</select>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# set
<!--
set:和where一样,解决语法错误问题。
update t_emp where id=1
-->
<update id="updateEmp">
update t_emp
<set>
<if test="empName != null">
emp_name = #{empName},
</if>
<if test="empSalary != null">
emp_salary = #{empSalary},
</if>
<if test="age!=null">
age = #{age}
</if>
</set>
where id = #{id}
</update>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# trim
trim 可以 实现 set 去掉多余逗号,where 去掉多余and/or 的功能, 不过写起来比较麻烦
<!-- trim版本实现where
prefix:前缀 ; 如果标签体中有东西,就给它们拼一个前缀
suffix:后缀
prefixOverrides:前缀覆盖; 标签体中最终生成的字符串,如果以指定前缀开始,就覆盖成空串
suffixOverrides:后缀覆盖
-->
<select id="queryEmpByNameAndSalary" resultType="com.joe.entity.Emp">
select * from t_emp
<trim prefix="where" prefixOverrides="and || or">
<if test="name != null">
emp_name= #{name}
</if>
<if test="salary != null">
and emp_salary = #{salary}
</if>
</trim>
</select>
<!--
trim: 版本实现 set
suffix="where id = #{id}"
-->
<update id="updateEmp">
update t_emp
<trim prefix="set" suffixOverrides="," >
<if test="empName != null">
emp_name = #{empName},
</if>
<if test="empSalary != null">
emp_salary= #{empSalary},
</if>
<if test="age!=null">
age = #{age}
</if>
</trim>
where id = #{id}
</update>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# choose when otherwise
在多个分支条件中,仅执行一个
<select id="queryEmpByNameAndSalaryWhen" resultType="com.joe.entity.Emp">
select * from t_emp
<where>
<choose>
<when test="name != null">
emp_name= #{name}
</when>
<when test="salary > 3000">
emp_salary = #{salary}
</when>
<otherwise>
id = 1
</otherwise>
</choose>
</where>
</select>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# foreach
<!-- for(Integer id :ids)
foreach: 遍历List,Set,Map,数组
collection:指定要遍历的集合名
item:将当前遍历出的元素赋值给指定的变量
separator:指定在每次遍历时,元素之间拼接的分隔符
open:遍历开始前缀; 不开始遍历就不会有这个
close:遍历结束后缀
-->
<select id="getEmpsByIdIn" resultType="com.joe.entity.Emp">
select id,emp_name empName,age,emp_salary empSalary
from t_emp
<if test="ids != null">
where id IN
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</if>
</select>
<!--foreach 批量插入-->
<insert id="addEmps">
insert into t_emp(emp_name,age,emp_salary)
values
<foreach collection="emps" item="emp" separator=",">
(#{emp.empName},#{emp.age},#{emp.empSalary})
</foreach>
</insert>
<!--foreach 批量更新-->
批量多个SQL需要添加allowMultiQueries=true
jdbc:mysql:///mybatis-example?allowMultiQueries=true
<update id="updateBatchEmp">
<foreach collection="emps" item="e" separator=";">
update t_emp
<set>
<if test="e.empName != null">
emp_name = #{e.empName},
</if>
<if test="e.empSalary != null">
emp_salary = #{e.empSalary},
</if>
<if test="e.age!=null">
age = #{e.age}
</if>
</set>
where id=#{e.id}
</foreach>
</update>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# sql片段
抽取可以复用的SQL片段
<sql id="empColumn">
emp_id,emp_name,emp_age,emp_salary,emp_gender
</sql>
<select id="getEmp" resultType="com.joe.entity.Employee">
select
<include refid="empColumn"/>
from `t_emp` where id = #{id}
</select>
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 特殊字符
在xml中,以下字符需要用转义字符,不能直接写
| 原始字符 | 转义字符 |
|---|---|
| & | & |
| < | < |
| > | > |
| " | " |
| ' | ' |
Last Updated: 2025/12/02, 11:22:00