本篇文章为大家展示了Mybatis中怎么批量插入数据,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

成都创新互联为客户提供专业的成都做网站、网站建设、程序、域名、空间一条龙服务,提供基于WEB的系统开发. 服务项目涵盖了网页设计、网站程序开发、WEB系统开发、微信二次开发、手机网站开发等网站方面业务。
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);
}测试代码:
//java项目www.fhadmin.org
@SpringBootTest
class DemoApplicationTests {
@Resource
private StudentMapper studentMapper;
@Test
public void testInsert(){
//数据生成
List studentList = createData(100);
//循环插入
long start = System.currentTimeMillis();
studentList.stream().forEach(student -> studentMapper.insert(student));
System.out.println(System.currentTimeMillis() - start);
}
private List createData(int size){
List 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;
}
} 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 studentList);
} 测试代码:
//java项目www.fhadmin.org
@SpringBootTest
class DemoApplicationTests {
@Resource
private StudentMapper studentMapper;
@Test
public void testInsertByForeachTag(){
//数据生成
List studentList = createData(100);
//使用foreach标签,拼接SQL插入
long start = System.currentTimeMillis();
studentMapper.insertBatch(studentList);
System.out.println(System.currentTimeMillis() - start);
}
private List createData(int size){
List 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;
}
} 测试代码:
//java项目www.fhadmin.org
@SpringBootTest
class DemoApplicationTests {
@Autowired
private SqlSessionFactory sqlSessionFactory;
@Test
public void testInsertBatch(){
//数据生成
List 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 createData(int size){
List 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
其他依赖版本如下:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.4.4 com.buhe demo 0.0.1-SNAPSHOT demo Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test mysql mysql-connector-java 5.1.41 org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.1 org.springframework.boot spring-boot-maven-plugin src/main/java **/*.xml
三种插入方式在不同数据量下的表现,测试结果:
| 插入方式 | 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中怎么批量插入数据,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。