Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它存在的意义是为了解决拼接SQL语句字符串时的痛点问题。
1.if.通过test属性中的表达式判断标签中的内容是否有效(是否会拼接到sql中)
2.where
a.若where标签中有条件成立,会自动生成where关键字
b.会自己将where标签中内容前多余的and去掉,但是其中内容后多余的and无法去掉
c.若where标签中没有任何一个条件成立,则where没有任何功能
3.trim
prefix,suffix:在标签中内容前面或后面添加指定内容
prefixOverrides,suffixOverrides:在标签中内容前面或后面去掉指定内容
4.choose,when,otherwise
相关于java中的if...else if...else
when至少设置一个,otherwise最多设置一个
5.foreach
collection:设置要循环的数组或集合
item:用一个字符串表示数组或集合中的每一个数据
separator:设置每次循环的数据之间的分隔符
open:循环的所有内容以什么开始
close:循环的所有内容以什么结束
6.sql片段
可以记录一段sql,在需要用的地方使用include标签进行引用
emp_id,emp_name,age,gender,dept_id
select
from t_emp
if标签可通过test属性的表达式进行判断,若表达式的结果为true,则标签中的内容会执行;反之 标签中的内容不会执行
mapper接口:
- /**
- * 根据条件查询员工信息
- * @param emp
- * @return
- */
- List
getEmpListByCondition(Emp emp);
mapper映射文件:
- <select id="getEmpListByConditionOne" resultType="Emp">
- select * from t_emp where 1 = 1
- <if test="empName != '' and empName != null">
- and emp_name = #{empName}
- if>
- <if test="age != '' and age != null">
- and age = #{age}
- if>
- <if test="gender != '' and gender != null">
- and gender = #{gender}
- if>
- select>
测试类:
- @Test
- public void testGetEmpByCondition(){
- SqlSession sqlSession = SqlSessionUtil.getSqlSession();
- DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
- Emp emp = new Emp(null, "", 20, "男");
- List
list = mapper.getEmpListByCondition(emp); - list.forEach(System.out::println);
where和if一般结合使用:
a>若where标签中的if条件都不满足,则where标签没有任何功能,即不会添加where关键字 b>若where标签中的if条件满足,则where标签会自动添加where关键字,并将条件最前方多余的 and去掉
注意:where标签不能去掉条件最后多余的and
mapper映射文件:
- <select id="getEmpByConditionTwo" 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="gender != null and gender !=''">
- and gender = #{gender}
- if>
- where>
- select>
测试类:
- @Test
- public void testGetEmpByCondition(){
- SqlSession sqlSession = SqlSessionUtil.getSqlSession();
- DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
- Emp emp = new Emp(null, "", 20, "男");
- List
list = mapper.getEmpListByCondition(emp); - list.forEach(System.out::println);
- Emp emp2 = new Emp(null, "", null, "男");
- List
list2 = mapper.getEmpListByCondition(emp2); - list2.forEach(System.out::println);
- sqlSession.close();
- }
trim用于去掉或添加标签中的内容
常用属性: prefix:在trim标签中的内容的前面添加某些内容
prefixOverrides:在trim标签中的内容的前面去掉某些内容
suffix:在trim标签中的内容的后面添加某些内容
suffixOverrides:在trim标签中的内容的后面去掉某些内容
mapper映射文件:
- <select id="getEmpListByCondition" resultType="Emp">
- select * from t_emp
- <trim prefix="where" suffixOverrides="and">
- <if test="empName != '' and empName != null">
- emp_name = #{empName} and
- if>
- <if test="age != '' and age != null">
- age = #{age} and
- if>
- <if test="gender != '' and gender != null">
- gender = #{gender}
- if>
- trim>
- select>
和上面测试类一样,这几个标签都可以处理多条件查询问题
mapper接口
- /**
- * 使用choose查询员工信息
- * @param emp
- * @return
- */
- List
getEmpByChoose(Emp emp);
mapper映射文件
- <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="gender != null and gender != ''">
- gender = #{gender}
- when>
- choose>
- where>
- select>
测试类:
- @Test
- public void testGetEmpByChoose() {
- SqlSession sqlSession = SqlSessionUtil.getSqlSession();
- DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
- Emp emp = new Emp(null, "张三", 20, "男");
- List
list = mapper.getEmpChoose(emp); - list.forEach(System.out::println);
- }
choose、when、 otherwise相当于if...else if..else
实现批量添加:
为什么推荐使用@Param因为它简单,你不设置可能要想这个东西它如何去获取,如果@Param之后,这个东西就不需要去思考了,直接就是这个注解的value属性值,就一定能访问到它
mapper接口:
- /**
- * 批量添加员工信息
- * @param emps
- */
- void insertMoreEmp(@Param("emps") List
emps) ;
mapper映射文件:
- <insert id="insertMoreEmp" >
- insert into t_emp values
- <foreach collection="emps" item="emp" separator=",">
- (null,#{emp.empName},#{emp.age},#{emp.gender},null)
- foreach>
- insert>
测试类:
- @Test
- public void testInsertMoreEmp(){
- SqlSession sqlSession = SqlSessionUtil.getSqlSession();
- DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
- Emp emp1 = new Emp(null, "猫猫1", 20, "男");
- Emp emp2 = new Emp(null, "猫猫2", 20, "男");
- Emp emp3 = new Emp(null, "猫猫3", 20, "男");
- List
list = Arrays.asList(emp1, emp2, emp3); - mapper.insertMoreEmp(list);
- sqlSession.close();
- }
实现批量删除:
mapper接口:
- /**
- * 批量删除
- * @param empIds
- */
- void deleteMoreEmp(@Param("empIds") Integer[] empIds);
mapper映射文件:
方式一:
-
- <delete id="deleteMoreEmp">
- delete from t_emp where emp_id in
- <foreach collection="empIds" item="empId" separator="," open="(" close=")">
- #{empId}
- foreach>
- delete>
方式二:
-
- <delete id="deleteMoreEmp">
- delete from t_emp where
- <foreach collection="empIds" item="empId" separator="or">
- emp_id = #{empId}
- foreach>
- delete>
测试类:
- @Test
- public void testDeleteMoreEmp(){
- SqlSession sqlSession = SqlSessionUtil.getSqlSession();
- DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
- Integer[] empIds = new Integer[]{6,7};
- mapper.deleteMoreEmp(empIds);
- sqlSession.close();
- }
sql片段,可以记录一段公共sql片段,在使用的地方通过include标签进行引入
- <sql id="empColumns">
- emp_id,emp_name,age,gender,dept_id
- sql>
-
- <select id="getEmpListByCondition" resultType="Emp">
- select <include refig="empColumns">include> from t_emp
- <if test="empName != '' and empName != null">
- and emp_name = #{empName}
- if>
- <if test="age != '' and age != null">
- and age = #{age}
- if>
- <if test="gender != '' and gender != null">
- and gender = #{gender}
- if>
- select>