动态生成注解形式的select语句
根据Map参数构建动态select语句
在EmployeeDynamicSQLProvider.java
中加入如下方法:
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| public String selectEmployeeWithParamMap(Map<String, Object> param) { return new SQL() { { SELECT("*"); FROM("tb_employee"); if (param.get("id") != null) { WHERE("id=#{id}"); } if (param.get("loginname") != null) { WHERE("loginname=#{loginname}"); } if (param.get("password") != null) { WHERE("password=#{password}"); } if (param.get("name") != null) { WHERE("name=#{name}"); } if (param.get("sex") != null) { WHERE("sex=#{sex}"); } if (param.get("age") != null) { WHERE("age=#{age}"); } if (param.get("phone") != null) { WHERE("phone=#{phone}"); } if (param.get("sal") != null) { WHERE("sal=#{sal}"); } if (param.get("state") != null) { WHERE("state=#{state}"); } } }.toString(); }
|
该方法根据map
的key
来动态生成WHERE
子句。
并在mapper
接口中添加如下方法:
1 2 3 4 5
| @SelectProvider( type = EmployeeDynamicSQLProvider.class, method = "selectEmployeeWithParamMap" ) List<Employee> selectEmployeeWithParamMap(Map<String, Object> param);
|
测试类
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 30 31 32 33 34 35 36 37 38
| package test; import java.util.HashMap; import java.util.List; import org.apache.ibatis.session.SqlSession; import domain.Employee; import fractory.SqlSessionFratoryTools; import mapper.EmployeeMapper; public class TestParmMap { public static void main(String[] args) { SqlSession sqlSession = null; try { sqlSession = SqlSessionFratoryTools.getSqlSession(); EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class); HashMap<String, Object> param = new HashMap<String, Object>();
param.put("sex", "男"); param.put("age", 19); List<Employee> employees = employeeMapper.selectEmployeeWithParamMap(param); employees.forEach(employee -> System.out.println(employee)); sqlSession.commit(); } catch (Exception e) { sqlSession.rollback(); e.printStackTrace(); } finally { if (sqlSession != null) sqlSession.close(); } } }
|
运行测试类,控制台输出如下:
1 2 3 4 5
| DEBUG [main] ==> Preparing: SELECT * FROM tb_employee WHERE (sex=? AND age=?) DEBUG [main] ==> Parameters: 男(String), 19(Integer) DEBUG [main] <== Total: 2 Employee [id=1, loginname=xiaoming, password=xiaoming, name=小明, sex=男, age=19, phone=123456789123, sal=9800.0, state=active] Employee [id=2, loginname=xiaowang, password=xiaowang, name=小王, sex=男, age=19, phone=123456789123, sal=6800.0, state=active]
|
可以看到执行的SQL:
1
| SELECT * FROM tb_employee WHERE (sex=? AND age=?)
|
是根据输入的map参数动态生成的,现在再往map中放入一个id(取消注释),再次运行,控制台输出如下:
1 2 3 4
| DEBUG [main] ==> Preparing: SELECT * FROM tb_employee WHERE (id=? AND sex=? AND age=?) DEBUG [main] ==> Parameters: 1(Integer), 男(String), 19(Integer) DEBUG [main] <== Total: 1 Employee [id=1, loginname=xiaoming, password=xiaoming, name=小明, sex=男, age=19, phone=123456789123, sal=9800.0, state=active]
|
可以看到mybatis
根据提供的id
,sex
,age
三个参数动态的生成了如下SQL
语句:
1
| SELECT * FROM tb_employee WHERE (id=? AND sex=? AND age=?)
|
根据持久化对象参数构建动态select语句
在动态SQL
生成器中添加如下方法:
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| public String selectEmployeeWithParamPO(Employee parm) { return new SQL() { { SELECT("*"); FROM("tb_employee"); if (parm.getId() != null) { WHERE("id=#{id}"); } if (parm.getLoginname() != null) { WHERE("loginname=#{loginname}"); } if (parm.getPassword() != null) { WHERE("password=#{password}"); } if (parm.getName() != null) { WHERE("name=#{name}"); } if (parm.getSex() != null) { WHERE("sex=#{sex}"); } if (parm.getAge() != null) { WHERE("age=#{age}"); } if (parm.getPhone() != null) { WHERE("phone=#{phone}"); } if (parm.getSal() != null) { WHERE("sal=#{sal}"); } if (parm.getState() != null) { WHERE("state=#{state}"); } } }.toString(); }
|
然后在mapper
接口中,添加如下方法:
1 2 3 4 5
| @SelectProvider( type = EmployeeDynamicSQLProvider.class, method = "selectEmployeeWithParamPO" ) List<Employee> selectEmployeeWithParamPO(Employee param);
|
测试类
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 30 31 32 33 34 35 36 37
| package test; import java.util.List; import org.apache.ibatis.session.SqlSession; import domain.Employee; import fractory.SqlSessionFratoryTools; import mapper.EmployeeMapper; public class TestParamPO { public static void main(String[] args) { SqlSession sqlSession = null; try { sqlSession = SqlSessionFratoryTools.getSqlSession(); EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class); Employee param = new Employee(); param.setAge(19); param.setSex("男"); List<Employee> employees = employeeMapper.selectEmployeeWithParamPO(param); employees.forEach(employee -> System.out.println(employee)); sqlSession.commit(); } catch (Exception e) { sqlSession.rollback(); e.printStackTrace(); } finally { if (sqlSession != null) sqlSession.close(); } } }
|
我们在测试类中提供了sex
和age
两个参数。mybatis
将会根据这两个参数生成SQL
语句,运行该测试类,控制台输出如下:
1 2 3 4 5
| DEBUG [main] ==> Preparing: SELECT * FROM tb_employee WHERE (sex=? AND age=?) DEBUG [main] ==> Parameters: 男(String), 19(Integer) DEBUG [main] <== Total: 2 Employee [id=1, loginname=xiaoming, password=xiaoming, name=小明, sex=男, age=19, phone=123456789123, sal=9800.0, state=active] Employee [id=2, loginname=xiaowang, password=xiaowang, name=小王, sex=男, age=19, phone=123456789123, sal=6800.0, state=active]
|
现在取消id
参数前面的注释,再提供一个id
参数,运行测试类,控制台输出如下:
1 2 3 4
| DEBUG [main] ==> Preparing: SELECT * FROM tb_employee WHERE (id=? AND sex=? AND age=?) DEBUG [main] ==> Parameters: 1(Integer), 男(String), 19(Integer) DEBUG [main] <== Total: 1 Employee [id=1, loginname=xiaoming, password=xiaoming, name=小明, sex=男, age=19, phone=123456789123, sal=9800.0, state=active]
|
原文链接: 11.2 基于注解的动态SQL1 select