04、MyBatis DynamicSQL(Mybatis动态SQL)

1.动态SQL简介

  动态 SQL是MyBatis强大特性之一.

  动态 SQL 元素和使用 JSTL 或其他类似基于 XML 的文本处理器相似.

  MyBatis 采用功能强大的基于 OGNL 的表达式来简化操作.

 04、MyBatis DynamicSQL(Mybatis动态SQL)_第1张图片

2.if

 04、MyBatis DynamicSQL(Mybatis动态SQL)_第2张图片

1).实现DynamicSQL

 04、MyBatis DynamicSQL(Mybatis动态SQL)_第3张图片

public interface EmployeeMapperDynamicSQL {
	public List getEmpsByCondtionIf(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);
			List emps = mapper.getEmpsByCondtionIf(employee);
			for(Employee emp:emps) {
				System.out.println(emp);
			}

  

2.choose

  有时候,我们不想使用所有的条件,而只是想从多个条件中选择一个使用.针对这种情况,MyBatis 提供了 choose 元素,它有点像 Java 中的 switch 语句.

 04、MyBatis DynamicSQL(Mybatis动态SQL)_第4张图片

	public List getEmpByConditionChoose(Employee employee);

  


	 
	 

  

			//测试choose
			Employee employee  = new Employee(3, "%e%",null, null);
			List list = mapper.getEmpByConditionChoose(employee);
			for(Employee emp:list) { 
				System.out.println(emp); 
			}

  

3.trim

1).where

 04、MyBatis DynamicSQL(Mybatis动态SQL)_第5张图片

	public List getEmpsByCondtionTrim(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);
			List emps = 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 
 	 	
	 		
				last_name=#{lastName},
			
			
				email=#{email},
			
			
				gender=#{gender}
			
	 	  
	 	where id=#{id} 
	 

  

			//调试set标签 
			Employee employee = new Employee(1, "Adminn",null, null); 
			mapper.updateEmp(employee);
			openSession.commit();

  

②.使用trim拼串更新

	public void  updateEmp(Employee employee);

  


	 
	 
	 	
		update tbl_employee 
		
			
				last_name=#{lastName},
			
			
				email=#{email},
			
			
				gender=#{gender}
			
		
		where id=#{id} 
	 

  

			//调试set标签 
			Employee employee = new Employee(1, "Adminn",null, null); 
			mapper.updateEmp(employee);
			openSession.commit();

  

4.foreach

 04、MyBatis DynamicSQL(Mybatis动态SQL)_第6张图片

 

  动态 SQL 的另外一个常用的必要操作是需要对一个集合进行遍历,通常是在构建 IN 条件语句的时候.

 04、MyBatis DynamicSQL(Mybatis动态SQL)_第7张图片

  当迭代列表、集合等可迭代对象或者数组时;index是当前迭代的次数,item的值是本次迭代获取的元素.

  当使用字典(或者Map.Entry对象的集合)时,index是键,item是值.

1).MySQL

(1).遍历记录

	//查询员工id'在给定集合中的
	public List getEmpsByConditionForeach(@Param("ids")List ids);

  


	 
	 

  

			//测试foreach
			List list = mapper.getEmpsByConditionForeach(Arrays.asList(1,2,3,4));
			for(Employee emp : list) {
				System.out.println(emp);
			}

  

(2).批量保存记录1

	public void addEmps(@Param("emps")List emps);

  


	 
	 
	 
	 
	 
 	 
	 	insert into tbl_employee(last_name,email,gender,d_id)
		values
		
			(#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id})
		
	  

  

			List emps = 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")List emps);

  


	 
	 
	 
	  
	 	
	 		insert into tbl_employee(last_name,email,gender,d_id)
	 		values(#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id})
	 	
	  

  

			List emps = 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 表达式中创建一个变量并将其绑定到上下文.

 04、MyBatis DynamicSQL(Mybatis动态SQL)_第8张图片

1).bind

  若在 mybatis 配置文件中配置了 databaseIdProvider , 则可以使用 “_databaseId”变量,这样就可以根据不同的数据库厂商构建特定的语句.

	public List getEmpsTestInnerParameter(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%");
			List list = mapper.getEmpsTestInnerParameter(employee2);
			for (Employee employee : list) {
				System.out.println(employee);
			}
		}finally{
			openSession.close();
		}
	}

  

2).SQL片段

	public void addEmps(@Param("emps")List emps);

  


	 
	
		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);
			List emps = 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

 04、MyBatis DynamicSQL(Mybatis动态SQL)_第9张图片

参考文档:http://commons.apache.org/proper/commons-ognl/language-guide.html

https://mybatis.org/mybatis-3/zh/dynamic-sql.html

你可能感兴趣的:(04、MyBatis DynamicSQL(Mybatis动态SQL))