1.动态SQL简介
动态 SQL是MyBatis强大特性之一.
动态 SQL 元素和使用 JSTL 或其他类似基于 XML 的文本处理器相似.
MyBatis 采用功能强大的基于 OGNL 的表达式来简化操作.
2.if
1).实现DynamicSQL
public interface EmployeeMapperDynamicSQL { public ListgetEmpsByCondtionIf(Employee employee); }
// select * from tbl_employee WHERE id=? and last_name like ? and email=? // Employee employee = new Employee(3, "%e%","[email protected]", null); //select * from tbl_employee WHERE id=? and last_name like ? Employee employee = new Employee(null, "%e%",null, null); Listemps = mapper.getEmpsByCondtionIf(employee); for(Employee emp:emps) { System.out.println(emp); }
2.choose
有时候,我们不想使用所有的条件,而只是想从多个条件中选择一个使用.针对这种情况,MyBatis 提供了 choose 元素,它有点像 Java 中的 switch 语句.
public ListgetEmpByConditionChoose(Employee employee);
//测试choose Employee employee = new Employee(3, "%e%",null, null); Listlist = mapper.getEmpByConditionChoose(employee); for(Employee emp:list) { System.out.println(emp); }
3.trim
1).where
public ListgetEmpsByCondtionTrim(Employee employee);
@Test public void testDynamicSql() throws IOException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession openSession = sqlSessionFactory.openSession(); try { EmployeeMapperDynamicSQL mapper = openSession.getMapper(EmployeeMapperDynamicSQL.class); // select * from tbl_employee WHERE id=? and last_name like ? and email=? // Employee employee = new Employee(3, "%e%","[email protected]", null); //select * from tbl_employee WHERE id=? and last_name like ? Employee employee = new Employee(3, "%e%",null, null); Listemps = mapper.getEmpsByCondtionIf(employee); for(Employee emp:emps) { System.out.println(emp); } //查询的时候如果某些条件没带可能sql拼装会有问题 //1、给where后面加上1=1,以后的条件都and xxx. //2、mybatis使用where标签来将所有的查询条件包括在内。 //mybatis就会将where标签中拼装的sql,多出来的and或者or去掉 //where只会去掉第一个多出来的and或者or。 //测试Trim List emps2 = mapper.getEmpsByCondtionTrim(employee); for(Employee emp:emps2) { System.out.println(emp); } } finally { // : handle finally clause openSession.close(); } }
2).set
①.使用set更新
public void updateEmp(Employee employee);
update tbl_employee where id=#{id} last_name=#{lastName}, email=#{email}, gender=#{gender}
//调试set标签 Employee employee = new Employee(1, "Adminn",null, null); mapper.updateEmp(employee); openSession.commit();
②.使用trim拼串更新
public void updateEmp(Employee employee);
update tbl_employee where id=#{id} last_name=#{lastName}, email=#{email}, gender=#{gender}
//调试set标签 Employee employee = new Employee(1, "Adminn",null, null); mapper.updateEmp(employee); openSession.commit();
4.foreach
动态 SQL 的另外一个常用的必要操作是需要对一个集合进行遍历,通常是在构建 IN 条件语句的时候.
当迭代列表、集合等可迭代对象或者数组时;index是当前迭代的次数,item的值是本次迭代获取的元素.
当使用字典(或者Map.Entry对象的集合)时,index是键,item是值.
1).MySQL
(1).遍历记录
//查询员工id'在给定集合中的 public ListgetEmpsByConditionForeach(@Param("ids")List ids);
//测试foreach Listlist = mapper.getEmpsByConditionForeach(Arrays.asList(1,2,3,4)); for(Employee emp : list) { System.out.println(emp); }
(2).批量保存记录1
public void addEmps(@Param("emps")Listemps);
insert into tbl_employee(last_name,email,gender,d_id) values (#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id})
Listemps = new ArrayList<>(); emps.add(new Employee(null, "smith", "[email protected]", "1",new Department(1))); emps.add(new Employee(null, "allen", "[email protected]", "0",new Department(1))); mapper.addEmps(emps); openSession.commit();
(3).批量保存记录2
public void addEmps(@Param("emps")Listemps);
insert into tbl_employee(last_name,email,gender,d_id) values(#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id})
Listemps = new ArrayList<>(); emps.add(new Employee(null, "smith", "[email protected]", "1",new Department(1))); emps.add(new Employee(null, "allen", "[email protected]", "0",new Department(1))); mapper.addEmps(emps); openSession.commit();
2).Oracle
(1).批量保存1
insert into employees( ) select #{emp.lastName} lastName,#{emp.email} email from dual
5.bind
bind 元素可以从 OGNL 表达式中创建一个变量并将其绑定到上下文.
1).bind
若在 mybatis 配置文件中配置了 databaseIdProvider , 则可以使用 “_databaseId”变量,这样就可以根据不同的数据库厂商构建特定的语句.
public ListgetEmpsTestInnerParameter(Employee employee);
@Test public void testInnerParam() throws IOException{ SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession openSession = sqlSessionFactory.openSession(); try{ EmployeeMapperDynamicSQL mapper = openSession.getMapper(EmployeeMapperDynamicSQL.class); Employee employee2 = new Employee(); employee2.setLastName("%e%"); Listlist = mapper.getEmpsTestInnerParameter(employee2); for (Employee employee : list) { System.out.println(employee); } }finally{ openSession.close(); } }
2).SQL片段
public void addEmps(@Param("emps")Listemps);
insert into tbl_employee( ) values (#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id}) employee_id,last_name,email last_name,email,gender,d_id
@Test public void testBatchSave() throws IOException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession openSession = sqlSessionFactory.openSession(); try { EmployeeMapperDynamicSQL mapper = openSession.getMapper(EmployeeMapperDynamicSQL.class); Listemps = new ArrayList<>(); emps.add(new Employee(null, "smith", "[email protected]", "1",new Department(1))); emps.add(new Employee(null, "allen", "[email protected]", "0",new Department(1))); mapper.addEmps(emps); openSession.commit(); }finally { openSession.close(); } }
6.OGNL
参考文档:http://commons.apache.org/proper/commons-ognl/language-guide.html
https://mybatis.org/mybatis-3/zh/dynamic-sql.html