Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它存在的意义是为了解决 拼接SQL语句字符串时的痛点问题。
创建DynamicSQLMapper
接口和映射文件,以及测试类
1、if
if标签可通过test属性的表达式进行判断,若表达式的结果为true,则标签中的内容会拼接到SQL语句中;反之标签中的内容不会拼接
DynamicSQLMapper接口
/**
* 多条件查询
*/
List<Emp> getEmpByCondition(Emp emp);
DynamicSQLMapper映射
<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>
测试方法
@Test
public void testGetEmpByCondition(){
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
Emp emp = new Emp(1,"张飞",36,"男","100@163.com",null);
System.out.println(mapper.getEmpByCondition(emp));
}
运行结果
2、where
where和if一般结合使用:
-
a>若where标签中的if条件都不满足,则where标签没有任何功能,即不会添加where关键字
-
b>若where标签中的if条件满足,则where标签会自动添加where关键字,并将条件最前方多余的
and
或者or
去掉 -
注意:where标签不能去掉条件最后多余的
and
或者or
修改DynamicSQLMapper映射
<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>
运行结果
3、trim
trim用于去掉或添加标签中的内容,常用属性:
-
prefix:在trim标签中的内容的前面添加某些内容
-
prefixOverrides:在trim标签中的内容的前面去掉某些内容
-
suffix:在trim标签中的内容的后面添加某些内容
-
suffixOverrides:在trim标签中的内容的后面去掉某些内容
若trim标签中没内容时,trim标签也没有任何效果
修改DynamicSQLMapper映射
<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} or
</if>
<if test="sex != null and sex != ''">
sex = #{sex} and
</if>
<if test="email != null and email != ''">
email = #{email} and
</if>
</trim>
</select>
测试方法
@Test
public void testGetEmpByCondition(){
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
Emp emp = new Emp(1,"张飞",36,"","",null);
System.out.println(mapper.getEmpByCondition(emp));
}
运行结果
4、set
set 元素可以用于动态包含需要更新的列,忽略其它不更新的列。set 元素会动态地在行首插入 SET 关键字,并会删掉额外的逗号(这些逗号是在使用条件语句给列赋值时引入的)。
DynamicSQLMapper接口
/**
* 更新员工信息
*/
int updateEmp(Emp emp);
DynamicSQLMapper映射
<update id="updateEmp">
update t_emp
<set>
<if test="empName != null">emp_name=#{empName},</if>
<if test="age != null">age=#{age},</if>
<if test="sex != null">sex=#{sex},</if>
<if test="email != null">email=#{email}</if>
</set>
where eid=#{eid}
</update>
与 set 元素等价的自定义 trim 元素:
<update id="updateEmp"> update t_emp <trim prefix="set" suffixOverrides=","> <if test="empName != null">emp_name=#{empName},</if> <if test="age != null">age=#{age},</if> <if test="sex != null">sex=#{sex},</if> <if test="email != null">email=#{email}</if> </trim> where eid=#{eid} </update>
测试方法
@Test
public void testUpdateEmp() {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
System.out.println(mapper.updateEmp(new Emp(1, "路飞", 33, "男", "1000@qq.com", null)));
}
运行结果
5、choose、when、otherwise
choose、when、otherwise相当于switch…case…default结构
choose里的标签只会有一个被执行
when至少要有一个,otherwise最多只能有一个
DynamicSQLMapper接口
/**
* 测试choose、when、otherwise
*/
List<Emp> getEmpByChoose(Emp emp);
DynamicSQLMapper映射
<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>
did = 1
</otherwise>
</choose>
</where>
</select>
测试方法
@Test
public void testGetEmpByChoose(){
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
Emp emp = new Emp(1,"张飞",36,"男","",null);
System.out.println(mapper.getEmpByChoose(emp));
}
运行结果
6、foreach
foreach元素的功能非常强大,它允许你指定一个集合,声明可以在元素体内使用的集合项(item)和索引(index)变量。它也允许你指定开头与结尾的字符串以及集合项迭代之间的分隔符。这个元素也不会错误地添加多余的分隔符!
属性:
-
collection:设置要循环的数组或集合
-
index:表示集合或数组中的每一个键或序号
-
item:表示集合或数组中的每一个数据
-
separator:设置循环体之间的分隔符
-
open:设置foreach标签中的内容的开始符
-
close:设置foreach标签中的内容的结束符
可以将任何
可迭代对象(如List、Set等)
、Map对象
或者数组对象
作为集合参数传递给 foreach。当使用可迭代对象或者数组时,index 是当前迭代的序号,item 的值是本次迭代获取到的元素。
当使用 Map 对象(或者 Map.Entry 对象的集合)时,index 是键,item 是值。
批量删除
在t_emp
表中添加几条待删数据
DynamicSQLMapper接口
/**
* 通过数组实现批量删除
*/
int deleteEmpMoreByArray(@Param("eids") Integer[] eids);
DynamicSQLMapper映射
<delete id="deleteEmpMoreByArray">
delete from t_emp where eid in
<foreach collection="eids" item="eid" separator="," open="(" close=")">
#{eid}
</foreach>
</delete>
写法二:
<delete id="deleteEmpMoreByArray"> delete from t_emp where <foreach collection="eids" item="eid" separator="or"> eid = #{eid} </foreach> </delete>
测试方法
@Test
public void testDeleteEmpMoreByArray() {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
System.out.println(mapper.deleteEmpMoreByArray(new Integer[]{6, 7, 8}));
}
运行结果
批量添加
DynamicSQLMapper接口
/**
* 通过List实现批量添加
*/
int insertEmpMoreByList(@Param("emps") List<Emp> emps);
DynamicSQLMapper映射
<insert id="insertEmpMoreByList">
insert into t_emp values
<foreach collection="emps" item="emp" separator=",">
(#{emp.eid},#{emp.empName},#{emp.age},#{emp.sex},#{emp.email},null)
</foreach>
</insert>
测试方法
@Test
public void testInsertEmpMoreByList() {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
List<Emp> emps = Arrays.asList(
new Emp(null, "曹操", 42, "男", "666@qq.com", null),
new Emp(null, "关羽", 40, "男", "666@qq.com", null),
new Emp(null, "诸葛亮", 36, "男", "666@qq.com", null));
System.out.println(mapper.insertEmpMoreByList(emps));
}
运行结果
7、SQL片段
sql片段,可以记录一段公共sql片段,在使用的地方通过include标签进行引入
如修改id="getEmpByChoose"
的映射:
<sql id="empColumns">
eid,emp_name,age,sex,email,did
</sql>
<select id="getEmpByChoose" resultType="Emp">
select <include refid="empColumns"/> from t_emp
<where>
...
</where>
</select>
参考文档:https://mybatis.net.cn/dynamic-sql.html