本篇文章為大家展示了Mybatis中怎么批量插入數(shù)據(jù),內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
成都創(chuàng)新互聯(lián)為客戶提供專業(yè)的成都做網(wǎng)站、網(wǎng)站建設(shè)、程序、域名、空間一條龍服務(wù),提供基于WEB的系統(tǒng)開發(fā). 服務(wù)項目涵蓋了網(wǎng)頁設(shè)計、網(wǎng)站程序開發(fā)、WEB系統(tǒng)開發(fā)、微信二次開發(fā)、手機網(wǎng)站開發(fā)等網(wǎng)站方面業(yè)務(wù)。
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(){ //數(shù)據(jù)生成 ListstudentList = createData(100); //循環(huán)插入 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(ListstudentList); }
測試代碼:
//java項目www.fhadmin.org @SpringBootTest class DemoApplicationTests { @Resource private StudentMapper studentMapper; @Test public void testInsertByForeachTag(){ //數(shù)據(jù)生成 ListstudentList = createData(100); //使用foreach標(biāo)簽,拼接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(){ //數(shù)據(jù)生成 ListstudentList = 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; } }
插入方式 | 10條 | 100條 | 500條 | 1000條 |
---|---|---|---|---|
循環(huán)插入 | 496ms | 3330ms | 15584ms | 33755ms |
foreach標(biāo)簽 | 268ms | 366ms | 392ms | 684ms |
批處理 | 222ms | 244ms | 364ms | 426ms |
三種方式中,批處理的方式效率是最高的,尤其是在數(shù)據(jù)量大的情況下尤為明顯。
其次是foreach標(biāo)簽,foreach標(biāo)簽是通過拼接SQL語句的方式完成批量操作的。但是當(dāng)拼接的SQL過多,導(dǎo)致SQL大小超過了MySQL服務(wù)器中max_allowed_packet變量的值時,會導(dǎo)致操作失敗,拋出PacketTooBigException異常。
上述內(nèi)容就是Mybatis中怎么批量插入數(shù)據(jù),你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。