
<!--配置一个可以执行批量的sqlSession--><bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"><!--SqlSessionTemplate 中的 有参构造方法:public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType) {--><!--引入,注入sqlSessionFactory--><constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/><!--ExecutorType executorType:执行类型,value="BATCH":批量处理--><constructor-arg name="executorType" value="BATCH"/></bean>
测试:
package com.wzy.test;import com.wzy.dao.DepartmentMapper;import com.wzy.dao.EmployeeMapper;import com.wzy.pojo.Department;import com.wzy.pojo.Employee;import org.apache.ibatis.session.SqlSession;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.util.Random;import java.util.UUID;@RunWith(SpringJUnit4ClassRunner.class)//指定使用SpringJUnit4单元测试运行@ContextConfiguration(locations = {"classpath:springapplication.xml"})public class MapperTest {@AutowiredDepartmentMapper departmentMapper;@AutowiredEmployeeMapper employeeMapper;@AutowiredSqlSession sqlSession;@Testpublic void testCRUD(){//1、向 `myb_dept` 部门 表中插入数据/* departmentMapper.insert(new Department(1,"工程部"));departmentMapper.insert(new Department(2,"生产部"));departmentMapper.insert(new Department(3,"外交部"));departmentMapper.insert(new Department(4,"财务部"));*///2、向 `myb_dept` 表中插入数据//employeeMapper.insert(new Employee(null,"张三","男","zhangsan@163.com",1));//3、批量插入EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);Random random = new Random();//性别String gender = "";for (int i = 0; i < 100; i++) {if (i % 2 == 0){gender="男";}else {gender="女";}//名字String name = UUID.randomUUID().toString().substring(0, 5)+i;String email = name+i+"@163.com";int dId = random.nextInt(4)+1;//调用 insertSelective() 方法插入数据mapper.insertSelective(new Employee(null,name,gender,email,dId));}}}
