mybatis扩展-批量操作

批量操作的关键,就是 opensession 中的 带有 ExecutorType.BATCH,除此之外,和一般的操作没有区别。

一、mybatis独立环境中

@Test
public void testBatch() throws IOException{
	SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
		
	//可以执行批量操作的sqlSession
	SqlSession openSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
	long start = System.currentTimeMillis();
	try{
		EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
		for (int i = 0; i < 10000; i++) {
			mapper.addEmp(new Employee(UUID.randomUUID().toString().substring(0, 5), "b", "1"));
		}
		openSession.commit();
		long end = System.currentTimeMillis();
		//批量:(预编译sql一次==>设置参数===>10000次===>执行(1次))
		//Parameters: 616c1(String), b(String), 1(String)==>4598
		//非批量:(预编译sql=设置参数=执行)==》10000    10200
		System.out.println("执行时长:"+(end-start));
	}finally{
		openSession.close();
	}
		
}

上面的代码如果是:下面这样的,那么sqlsession 就是非批量的,执行速度就慢了。

SqlSession openSession = sqlSessionFactory.openSession();

二、mybatis与spring整合【即SSM框架】环境

需要在spring-ioc容器配置文件(一般取名叫: applicationContext.xml)中添加带有ExecutorType.BATCH的 sqlSession对象。

<!--配置一个可以进行批量执行的sqlSession  -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
	<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"></constructor-arg>
	<constructor-arg name="executorType" value="BATCH"></constructor-arg>
</bean>

然后就可以在service中,自动注入 sqlSession

举例:EmployeeService.java

package com.test.springmvc.service;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.test.springmvc.bean.Employee;
import com.test.springmvc.dao.EmployeeMapper;

@Service
public class EmployeeService {
	
	//employeeMapper 是靠 applicationContext.xml的IOC容器,自动创建并赋值到这里的。
	@Autowired
	private EmployeeMapper employeeMapper;

    public List<Employee> getEmps(){
		
		return employeeMapper.getEmps();
	}
	
	//sqlSession 是为了从 applicationContext.xml的IOC容器中 获取批量处理的 sqlSession 对象,并赋值给自己
	@Autowired
	private SqlSession sqlSession;
	
	public List<Employee> getEmpsBatch(){
		EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);		
		return mapper.getEmps();
	}
	
	
}

这样 控制器 controller 的写法可以写成这样了:

package com.test.springmvc.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.test.springmvc.bean.Employee;
import com.test.springmvc.service.EmployeeService;

@Controller
public class EmployeeController {
	
	@Autowired
	EmployeeService employeeService;
	
	@RequestMapping("/getemps")
	public String emps(Map<String,Object> map){

                //没有采用批量处理的方法
                //List<Employee> emps = employeeService.getEmps();
		
                //采用批量处理的方法
                List<Employee> emps = employeeService.getEmpsBatch();
		map.put("allEmps", emps);
		return "list";
	}

}

 

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments