c语言sscanf函数的用法是什么
295
2023-01-21
Mybatis三种批量插入数据的方式
1. 循环插入
mapper.xml:
INSERT INTO tb_student (name, age, phone, address, class_id) VALUES (#{name},#{age},#{phone},#{address},#{classId})
mapper接口:
public interface StudentMapper {
int insert(Student student);
}
测试代码:
@SpringBootTest
class DemoApplicationTests {
@Resource
private StudentMapper studentMapper;
@Test
public void testInsert(){
//数据生成
List
//循环插入
long start = System.currentTimeMillis();
studentList.stream().forEach(student -> studentMapper.insert(student));
System.out.println(System.currentTimeMillis() - start);
}
private List
List
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.xml:
INSERT INTO tb_student (name, age, phone, address, class_id) VALUES (#{name},#{age},#{phone},#{address},#{classId})
INSERT INTO tb_student (name, age, phone, address, class_id) VALUES
(#{item.name},#{item.age},#{item.phone},#{item.address},#{item.classId})
mapper接口:
public interface StudentMapper {
int insert(Student student);
int insertBatch(List
}
测试代码:
@SpringBootTest
class DemoApplicationTests {
@Resource
private StudentMapper studentMapper;
@Test
public void testInsertByForeachTag(){
//数据生成
List
//使用foreach标签,拼接SQL插入
long start = System.currentTimeMillis();
studentMapper.insertBatch(studentList);
System.out.println(System.currentTimeMillis() - start);
}
private List
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. 批处理
测试代码:
@SpringBootTest
class DemoApplicationTests {
@Autowired
private SqlSessionFactory sqlSessionFactory;
@Test
public void testInsertBatch(){
//数据生成
List
//使用批处理
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
List
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
其他依赖版本如下:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
三种插入方式在不同数据量下的表现,测试结果:
插入方式
10条
100条
500条
1000条
循环插入
496ms
3330ms
15584ms
33755ms
foreach标签
268ms
366ms
392ms
684ms
批处理
222ms
244ms
364ms
426ms
三种方式中,批处理的方式效率是最高的,尤其是在数据量大的情况下尤为明显。
其次是foreach标签,foreach标签是通过拼接SQL语句的方式完成批量操作的。但是当拼接的SQL过多,导致SQL大小超过了MySQL服务器中max_allowed_packet变量的值时,会导致操作失败,抛出PacketTooBigException异常。
最后是循环插入的方式,这种方式在数据量小的时候可以使用,在数据量大的情况下效率要低很多。
以上就是Mybatis的三种批量插入方式的详细内容,更多关于Mybatis 批量插入的资料请关注我们其它相关文章!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~