2021年4月16日星期五

Mybatis的三种批量插入方式

1. 循环插入

mapper.

<?

mapper接口:

public interface StudentMapper { int insert(Student student);}

测试代码:

@SpringBootTestclass DemoApplicationTests {	@Resource	private StudentMapper studentMapper;	@Test	public void testInsert(){		//数据生成		List<Student> studentList = createData(100);		//循环插入		long start = System.currentTimeMillis();		studentList.stream().forEach(student -> studentMapper.insert(student));		System.out.println(System.currentTimeMillis() - start);	}	private List<Student> createData(int size){		List<Student> studentList = new ArrayList<>();		Student student;		for(int i = 0; i < size; i++){			student = new Student();			student.setName("小王" + i);			student.setAge(18);			student.setClassId(1);			student.setPhone("1585xxxx669");			student.setAddress("未知");			studentList.add(student);		}		return studentList;	}}

2. foreach标签

mapper.

<?

mapper接口:

public interface StudentMapper { int insert(Student student); int insertBatch(List<Student> studentList);}

测试代码:

@SpringBootTestclass DemoApplicationTests {	@Resource	private StudentMapper studentMapper;	@Test	public void testInsertByForeachTag(){		//数据生成		List<Student> studentList = createData(100);		//使用foreach标签,拼接SQL插入		long start = System.currentTimeMillis();		studentMapper.insertBatch(studentList);		System.out.println(System.currentTimeMillis() - start);	}	private List<Student> createData(int size){		List<Student> studentList = new ArrayList<>();		Student student;		for(int i = 0; i < size; i++){			student = new Student();			student.setName("小王" + i);			student.setAge(18);			student.setClassId(1);			student.setPhone("1585xxxx669");			student.setAddress("未知");			studentList.add(student);		}		return studentList;	}}

3. 批处理

测试代码:

@SpringBootTestclass DemoApplicationTests {	@Autowired	private SqlSessionFactory sqlSessionFactory;	@Test	public void testInsertBatch(){		//数据生成		List<Student> studentList = createData(100);    //使用批处理		long start = System.currentTimeMillis();		SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,false);		StudentMapper studentMapperNew = sqlSession.getMapper(StudentMapper.class);		studentList.stream().forEach(student -> studentMapperNew.insert(student));		sqlSession.commit();		sqlSession.clearCache();		System.out.println(System.currentTimeMillis() - start);	}	private List<Student> createData(int size){		List<Student> studentList = new ArrayList<>();		Student student;		for(int i = 0; i < size; i++){			student = new Student();			student.setName("小王" + i);			student.setAge(18);			student.setClassId(1);			student.setPhone("1585xxxx669");			student.setAddress("未知");			studentList.add(student);		}		return studentList;	}}

三种方式的对比

MySQL服务器版本:5.6.4

其他依赖版本如下:

<?

三种插入方式在不同数据量下的表现,测试结果:

插入方式10条100条500条1000条
循环插入496ms3330ms15584ms33755ms
foreach标签268ms366ms392ms684ms
批处理222ms244ms364ms426ms

三种方式中,批处理的方式效率是最高的,尤其是在数据量大的情况下尤为明显。

其次是foreach标签,foreach标签是通过拼接SQL语句的方式完成批量操作的。但是当拼接的SQL过多,导致SQL大小超过了MySQL服务器中max_allowed_packet变量的值时,会导致操作失败,抛出PacketTooBigException异常。

最后是循环插入的方式,这种方式在数据量小的时候可以使用,在数据量大的情况下效率要低很多。









原文转载:http://www.shaoqun.com/a/691745.html

跨境电商:https://www.ikjzd.com/

淘粉吧怎么返利:https://www.ikjzd.com/w/1725

square:https://www.ikjzd.com/w/2106


1.循环插入mapper.<?mapper接口:publicinterfaceStudentMapper{intinsert(Studentstudent);}测试代码:@SpringBootTestclassDemoApplicationTests{ @Resource privateStudentMapperstudentMapper; @Test publicvoidtestInser
邮乐购物商城:https://www.ikjzd.com/w/1776
Sunrate:https://www.ikjzd.com/w/2685
naning9韩国官网:https://www.ikjzd.com/w/2401
父皇和皇兄一起插公主 别说话,含住朕的龙根:http://www.30bags.com/m/a/255125.html
facebook入门级养号和测评攻略:https://www.ikjzd.com/tl/107763
结婚后我才知道老公是富翁:http://www.30bags.com/m/a/252554.html