菜鸟在MyBatis路上前行-MyBatis动态SQL,注意案例是从菜鸟在MyBatis路上前行-利用SpringMVC和MyBatis实现员工列表显示页面 来的,所以代码基础需要搭建好。那么下面一起来看看动态sql。
Contents
1 if元素使用案例
1.1 问题
使用MyBatis动态SQL的if元素,按部门做条件查询EMP员工信息表。
1.2 方案
if元素使用语法如下:
1 2 3 4 5 6 7 8 9 10 11 |
<select …> SQL语句1 <if test=“条件表达式”> SQL语句2 </if> </select> |
1.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:创建项目
复制项目spring-springmvc-mybatis,创建项目mybatis-dynamic。
步骤二:增加根据部门查询员工的方法
创建封装查询条件的类Condition,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.souvc.entity; import java.util.List; public class Condition { private Integer deptno; private Double salary; private List<Integer> empnos; //get,set } |
在EmpDao接口中增加方法findByDept,代码如下:
1 2 |
List<Emp> findByDept(Condition cond); |
步骤三:实现根据部门查询员工
在EmpMapper.xml中增加根据部门查询员工的SQL,代码如下:
1 2 3 4 5 6 7 8 9 10 11 |
<!-- if --> <!-- 查询部门下的所有员工 --> <select id="findByDept" parameterType="com.souvc.entity.Condition" resultType="com.souvc.entity.Emp"> select * from t_emp <if test="deptno != null"> where deptno=#{deptno} </if> </select> |
步骤四:测试根据部门查询员工的方法
在TestEmpDao中,增加测试方法testFindByDept,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
/** * 根据部门查询员工 */ @Test public void testFindByDept() { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); EmpDAO dao = ctx.getBean(EmpDAO.class); Condition cond = new Condition(); cond.setDeptno(10); List<Emp> list = dao.findByDept(cond); for(Emp e : list) { System.out.println( e.getEmpno() + " " + e.getEname() + " " + e.getJob() ); } |
2 choose元素使用案例
2.1 问题
使用MyBatis动态SQL的choose元素,按工资做条件查询EMP员工信息表。
2.2 方案
choose元素使用语法如下:
1 2 3 4 5 6 7 8 9 10 11 12 |
<select …> SQL语句1 <choose> <when test=”条件表达式”> SQL语句2 </when> <otherwise> SQL语句3 </otherwise> </choose> </select> |
2.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:增加根据工资查询员工的方法
在EmpDao中增加方法findBySalary,代码如下:
1 |
List<Emp> findBySalary(Condition cond); |
步骤二:实现根据工资查询员工
在EmpMapper.xml中增加根据工资查询员工的SQL,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<!-- choose --> <!-- 查询大于当前收入的员工 --> <select id="findBySalary" parameterType="com.souvc.entity.Condition" resultType="com.souvc.entity.Emp"> select * from t_emp <choose> <when test="salary > 3000"> where sal>#{salary} </when> <otherwise> where sal>=3000 </otherwise> </choose> </select> |
在TestEmpDao中增加方法testFindBySalary,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/** * 查询大于当前收入的员工 */ @Test public void testFindBySalary() { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); EmpDAO dao = ctx.getBean(EmpDAO.class); Condition cond = new Condition(); cond.setSalary(4000.0); List<Emp> list = dao.findBySalary(cond); for (Emp e : list) { System.out.println( e.getEmpno() + " " + e.getEname() + " " + e.getJob() ); } } |
3 where元素使用案例
3.1 问题
使用MyBatis动态SQL的where元素,查询当前部门下大于指定工资的员工。
3.2 方案
where元素的使用语法如下:
1 2 3 4 5 6 7 |
<select …> select 字段 from 表 <where> 动态追加条件 </where> </select> |
3.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:增加根据部门和工资查询员工的方法
在EmpDao中增加方法findByDeptAndSalary,代码如下:
1 |
List<Emp> findByDeptAndSalary(Condition cond); |
步骤二:实现根据部门和工资查询员工的方法
在EmpMapper.xml中增加根据部门和工资查询员工的SQL,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<!-- where --> <!-- 查询当前部门下,大于当前收入的员工 --> <select id="findByDeptAndSalary" parameterType="com.souvc.entity.Condition" resultType="com.souvc.entity.Emp"> select * from t_emp <where> <if test="deptno != null"> and deptno=#{deptno} </if> <if test="salary != null"> and sal>#{salary} </if> </where> </select> |
步骤三:测试根据部门和工资查询员工的方法
在TestEmpDao中,增加测试方法testFindByDeptAndSalary,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/** * 查询当前部门下,大于当前收入的员工 */ @Test public void testFindByDeptAndSalary() { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); EmpDAO dao = ctx.getBean(EmpDAO.class); Condition cond = new Condition(); cond.setDeptno(20); cond.setSalary(2000.0); List<Emp> list = dao.findByDeptAndSalary(cond); for(Emp e : list) { System.out.println( e.getEmpno() + " " + e.getEname() + " " + e.getJob() ); } } |
4 set元素使用案例
4.1 问题
使用MyBatis动态SQL的set元素,实现更新员工。
4.2 方案
set元素使用语法如下:
1 2 3 4 5 6 7 |
<update …> update 表 <set> 动态追加更新字段 </set> </update> |
4.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:增加更新员工的方法
在EmpDao中增加更新员工的方法,代码如下:
1 |
void update(Emp emp); |
步骤二:实现更新员工的方法
在EmpMapper.xml中增加更新员工的SQL,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!-- set --> <!-- 更新员工 --> <update id="update" parameterType="com.souvc.entity.Emp"> update t_emp <set> <if test="ename!=null"> ename=#{ename}, </if> <if test="job!=null"> job=#{job}, </if> </set> where empno=#{empno} </update> |
步骤三:测试更新员工的方法
在TestEmpDao中增加测试更新员工的方法,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/** * 更新员工 */ @Test public void testUpdate() { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); EmpDAO dao = ctx.getBean(EmpDAO.class); Emp e = new Emp(); e.setEmpno(14); e.setEname("Leo"); dao.update(e); } |
5 trim元素使用案例
5.1 问题
使用MyBatis动态SQL的trim元素代替where和set,重写findByDeptAndSalary和update方法。
5.2 方案
trim元素使用语法如下:
1 2 3 4 5 6 7 8 9 |
<!– 等价于where元素 --> <trim prefix="WHERE" prefixOverrides="AND |OR "> … </trim> <!– 等价于set元素 --> <trim prefix="SET" suffixOverrides=","> … </trim> |
5.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:重新增加根据部门和工资查询员工的方法,以及更新员工的方法
在EmpDao中增加findByDeptAndSalary2和update2方法,代码如下:
1 2 3 |
List<Emp> findByDeptAndSalary2(Condition cond); void update2(Emp emp); |
步骤二:重新实现根据部门和工资查询员工的方法,以及更新员工的方法
在EmpMapper.xml中,实现findByDeptAndSalary2和update2,代码如下:
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 |
<!-- 查询当前部门下,大于当前收入的员工 --> <select id="findByDeptAndSalary2" parameterType="com.souvc.entity.Condition" resultType="com.souvc.entity.Emp"> select * from t_emp <trim prefix="where" prefixOverrides="and"> <if test="deptno != null"> and deptno=#{deptno} </if> <if test="salary != null"> and sal>#{salary} </if> </trim> </select> <!-- 使用trim代替set --> <update id="update2" parameterType="com.souvc.entity.Emp"> update t_emp <trim prefix="set" suffixOverrides=","> <if test="ename!=null"> ename=#{ename}, </if> <if test="job!=null"> job=#{job}, </if> </trim> where empno=#{empno} </update> |
6 foreach元素使用案例
6.1 问题
使用MyBatis动态SQL的foreach元素,实现根据一组员工ID查询员工。
6.2 方案
foreach元素使用语法如下:
1 2 3 4 5 6 7 8 |
<select …> select 字段 from 表 where 字段 in <foreach collection=“集合” item=“迭代变量" open="(" separator="," close=")"> #{迭代变量} </foreach> </select> |
6.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:增加根据一组员工ID查询员工的方法
在EmpDao中增加根据一组员工ID查询员工的方法,代码如下:
1 |
List<Emp> findByIds(Condition cond); |
步骤二:实现根据一组员工ID查询员工的方法
在EmpMapper.xml中增加根据一组员工ID查询员工的SQL,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<!-- foreach --> <!-- 根据ID查询员工 --> <select id="findByIds" parameterType="com.souvc.entity.Condition" resultType="com.souvc.entity.Emp"> select * from t_emp where empno in <foreach collection="empnos" open="(" close=")" separator="," item="id"> #{id} </foreach> </select> |
步骤三:测试根据一组员工ID查询员工的方法
在TestEmpDao中增加测试findByIds的方法,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
/** * 根据员工ID查询员工 */ @Test public void testFindByIds() { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); EmpDAO dao = ctx.getBean(EmpDAO.class); List<Integer> ids = new ArrayList<Integer>(); ids.add(3); ids.add(7); ids.add(8); Condition cond = new Condition(); cond.setEmpnos(ids); List<Emp> list = dao.findByIds(cond); for(Emp e : list) { System.out.println( e.getEmpno() + " " + e.getEname() + " " + e.getJob() ); } } |
代码参考:mybatis-dynamic
参考文章:
【2】Dynamic SQL
如果您认为本教程质量不错,读后觉得收获很大,预期工资能蹭蹭蹭的往上涨,那么不妨小额赞助我一下,让我有动力继续写出高质量的教程。

