动态sql主要用于解决查询条件不确定的情况。也就是说:在实际开发中,经常需要根据用户是否输入了某个值,来确定是否需要这个条件。
MyBatis中用于动态sql的元素主要有:if、where、trim、set、foreach、choose等
1.if+where标签
XML文件代码
<select id="listEmp" parameterType="Emp" resultType="Emp">
select * from emp
<where>
<if test="job!=null and job!=''">
and job like concat('%',#{job},'%')
</if>
<if test="deptno!=null and deptno!=''">
and deptno = #{deptno}
</if>
</where>
order by empno
</select>
调用方法
Emp emp = new Emp();
//emp.setJob("经");
//emp.setDeptno(10); //注意:deptno属性必须为Integer类型
List<Emp> list = mapper.listEmp(emp);
for(Emp e : list) {
System.out.println(e);
}
if+where会实现以下功能:
- 自动添加where
- 不需要考虑where后是否加and,mybatis会自动处理
- 不需要考虑是否加空格,mybatis会自动处理
- 没有 else 标签,也没有 else if 标签。
注意: job!='' 此处只可以判断是否为空,不能判断是否为某个值。也就是说:job!='经理' 是不好使的。
2.choose标签
XML文件代码
<select id="listEmp" parameterType="Emp" resultType="Emp">
select * from emp
<where>
<choose>
<when test="job!=null and job!=''">
and job like concat('%',#{job},'%')
</when>
<when test="deptno!=null and deptno!=''">
and deptno = #{deptno}
</when>
<otherwise></otherwise>
</choose>
</where>
order by empno
</select>
调用方法
Emp emp = new Emp();
emp.setJob("职员");
emp.setDeptno(10);
List<Emp> list = mapper.listEmp(emp);
for(Emp e : list) {
System.out.println(e);
}
choose会实现如下功能:
- 多个 when 标签中,只能执行一个。也就是说:当一个 when 条件满足并执行后,其它的 when 将不再执行。
- 当所有 when 都不满足条件时,执行 otherwise 标签。
if 与 choose 的区别:if 相当于java中的if语句; choose相当于java中的switch语句
3.trim标签.
trim标签可以在自己包含的内容中加上某些前缀或后缀,与之对应的属性是:prefix、suffix。 还可以把包含内容的开始内容覆盖,即忽略。也可以把结束的某些内容覆盖,对应的属性是:prefixOverrides、suffixOverrides
XML文件代码
<insert id="insertEmp1" parameterType="Emp">
insert into emp
<trim prefix="(" suffix=")" suffixOverrides=",">
ename,deptno,
<if test="job!=null and job!=''">
job,
</if>
<if test="hiredate!=null and hiredate!=''">
hiredate,
</if>
</trim>
<trim prefix="values(" suffix=")" suffixOverrides=",">
#{ename},#{deptno},
<if test="job!=null and job!=''">
#{job},
</if>
<if test="hiredate!=null and hiredate!=''">
#{hiredate},
</if>
</trim>
</insert>
调用方法
SqlSession sqlSession = Util.getSqlSessionFactory().openSession();
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
Emp emp = new Emp();
emp.setEname("张三");
emp.setJob("职员");
//emp.setHiredate("2020-09-04");
emp.setDeptno(10);
int result = mapper.insertEmp1(emp);
sqlSession.commit();
System.out.println(result);
- prefix(前缀)与suffix(后缀)可以在sql语句中拼接出一对小括号。
- suffixOberrides可以将最后一个逗号去掉。(prefixOberrides(前缀去除))
4.set标签
set标签主要用于更新操作时使用
XML文件代码
<update id="updateEmp" parameterType="Emp">
update emp
<set>
<if test="job!=null and job!=''">
job=#{job},
</if>
<if test="sal!=null and sal!=''">
sal=#{sal},
</if>
</set>
where empno=#{empno}
</update>
调用方法
Emp emp = new Emp();
emp.setEmpno(7943);
//emp.setJob("职员1");
emp.setSal(3000.0);
int result = mapper.updateEmp(emp);
sqlSession.commit();
System.out.println(result);
注意:
- set可以在sql语句中自动加上set关键词。
- 可以自动将最后的逗号去掉
- 上面写法中,必须要保证有一个if成立
5.foreach标签
foreach标签可以在sql中迭代一个集合或数组,主要用于拼接in条件。下面是一个批量删除示例:
XML文件代码
<delete id="deleteEmp" parameterType="int">
delete from emp where empno in
<foreach collection="array" item="empno" open="(" close=")" separator=",">
#{empno}
</foreach>
</delete>
调用方法
int[] arr = {7941,7942,7943};
int result = mapper.deleteEmp(arr); //注意:此接口参数应为int数组
sqlSession.commit();
System.out.println(result);
foreach标签的属性:
- collection:需要遍历的类型,值有:list、array
- item:表示遍历出来的对象
- open:表示语句的开始部分
- close:表示语句的结束部分
- separator:表示每次迭代之间以什么符号为间隔
- index:每次迭代的位置索引,就是循环变量