动态SQL
2026/1/15大约 3 分钟MyBatis动态SQL条件查询
动态SQL
MyBatis 框架的动态 SQL 技术是一种根据特定条件动态拼装 SQL 语句的功能,它存在的意义是为了解决拼接 SQL 语句字符串时的痛点问题。
1. if 标签
if 标签可通过 test 属性的表达式进行判断,若表达式的结果为 true,则标签中的内容会执行;反之标签中的内容不会执行。
<select id="getEmpByCondition" resultType="Emp">
select * from t_emp where 1=1
<if test="empName != null and empName != ''">
and emp_name = #{empName}
</if>
<if test="age != null and age != ''">
and age = #{age}
</if>
<if test="sex != null and sex != ''">
and sex = #{sex}
</if>
<if test="email != null and email != ''">
and email = #{email}
</if>
</select>2. where 标签
where 和 if 一般结合使用:
- 若
where标签中的if条件都不满足,则where标签没有任何功能,即不会添加where关键字 - 若
where标签中的if条件满足,则where标签会自动添加where关键字,并将条件最前方多余的and去掉
<select id="getEmpByCondition" resultType="Emp">
select * from t_emp
<where>
<if test="empName != null and empName != ''">
and emp_name = #{empName}
</if>
<if test="age != null and age != ''">
and age = #{age}
</if>
<if test="sex != null and sex != ''">
and sex = #{sex}
</if>
<if test="email != null and email != ''">
and email = #{email}
</if>
</where>
</select>注意
where 标签不能去掉条件最后多余的 and
3. trim 标签
trim 用于去掉或添加标签中的内容。
常用属性
| 属性 | 说明 |
|---|---|
| prefix | 在 trim 标签中的内容的前面添加某些内容 |
| prefixOverrides | 在 trim 标签中的内容的前面去掉某些内容 |
| suffix | 在 trim 标签中的内容的后面添加某些内容 |
| suffixOverrides | 在 trim 标签中的内容的后面去掉某些内容 |
<select id="getEmpByCondition" resultType="Emp">
select * from t_emp
<trim prefix="where" suffixOverrides="and|or">
<if test="empName != null and empName != ''">
emp_name = #{empName} and
</if>
<if test="age != null and age != ''">
age = #{age} and
</if>
<if test="sex != null and sex != ''">
sex = #{sex} and
</if>
<if test="email != null and email != ''">
email = #{email}
</if>
</trim>
</select>4. choose、when、otherwise 标签
choose、when、otherwise 相当于 if...else if...else
<select id="getEmpByChoose" resultType="Emp">
select * from t_emp
<where>
<choose>
<when test="empName != null and empName != ''">
emp_name = #{empName}
</when>
<when test="age != null and age != ''">
age = #{age}
</when>
<when test="sex != null and sex != ''">
sex = #{sex}
</when>
<when test="email != null and email != ''">
email = #{email}
</when>
<otherwise>
emp_id = 1
</otherwise>
</choose>
</where>
</select>5. foreach 标签
属性说明
| 属性 | 说明 |
|---|---|
| collection | 设置要循环的数组或集合 |
| item | 表示集合或数组中的每一个数据 |
| separator | 设置循环体之间的分隔符 |
| open | 设置 foreach 标签中的内容的开始符 |
| close | 设置 foreach 标签中的内容的结束符 |
批量删除
<!-- int deleteMoreByArray(@Param("empIds") Integer[] empIds); -->
<delete id="deleteMoreByArray">
delete from t_emp where emp_id in
<foreach collection="empIds" item="empId" separator="," open="(" close=")">
#{empId}
</foreach>
</delete>批量添加
<!-- int insertMoreByList(@Param("emps") List<Emp> emps); -->
<insert id="insertMoreByList">
insert into t_emp values
<foreach collection="emps" item="emp" separator=",">
(null, #{emp.empName}, #{emp.age}, #{emp.sex}, #{emp.email}, null)
</foreach>
</insert>6. SQL片段
SQL 片段,可以记录一段公共 SQL 片段,在使用的地方通过 include 标签进行引入。
定义 SQL 片段
<sql id="empColumns">
emp_id, emp_name, age, sex, email
</sql>引用 SQL 片段
<select id="getEmpByCondition" resultType="Emp">
select <include refid="empColumns"/> from t_emp
<where>
<if test="empName != null and empName != ''">
and emp_name = #{empName}
</if>
</where>
</select>测试代码
@Test
public void testGetEmpByCondition() {
Emp emp = new Emp(null, "张三", 20, "男", null);
List<Emp> list = mapper.getEmpByCondition(emp);
list.forEach(System.out::println);
}
@Test
public void testDeleteMoreByArray() {
Integer[] empIds = {1, 2, 3};
int result = mapper.deleteMoreByArray(empIds);
System.out.println(result);
}
@Test
public void testInsertMoreByList() {
List<Emp> emps = new ArrayList<>();
emps.add(new Emp(null, "a", 20, "男", "a@qq.com"));
emps.add(new Emp(null, "b", 21, "女", "b@qq.com"));
emps.add(new Emp(null, "c", 22, "男", "c@qq.com"));
int result = mapper.insertMoreByList(emps);
System.out.println(result);
}