正常我們使用mybatis-plus插入的時(shí)候,首先想到的是??saveBatch?方法,不過看了下打印出來的sql和底層代碼,才發(fā)現(xiàn)它并不是真正的批量插入。
IService 中的代碼為
default boolean saveBatch(Collection<T> entityList) { return this.saveBatch(entityList, 1000); }
? ? 實(shí)現(xiàn)層? ?ServiceImpl?中的代碼為
public boolean saveBatch(Collection<T> entityList, int batchSize) { String sqlStatement = this.getSqlStatement(SqlMethod.INSERT_ONE); return this.executeBatch(entityList, batchSize, (sqlSession, entity) -> { sqlSession.insert(sqlStatement, entity); }); }
SqlMethod.INSERT_ONE 的中文枚舉為
INSERT_ONE("insert", "插入一條數(shù)據(jù)(選擇字段插入)", "<script>\nINSERT INTO %s %s VALUES %s\n</script>"),
通過監(jiān)控控制臺發(fā)現(xiàn),它只是循環(huán)每1000條去插入,效率非常低。
?
參考網(wǎng)友的文章,找到一個(gè)支持批量操作的方法,下面直接貼上代碼
1、添加批量操作參數(shù)類??CustomSqlInjector
/** * 支持自定義SQL注入方法 */ public class CustomSqlInjector extends DefaultSqlInjector { @Override public List<AbstractMethod> getMethodList(Class<?> mapperClass) { // 獲取父類SQL注入方法列表 List<AbstractMethod> methodList = super.getMethodList(mapperClass); // 將批量插入方法添加進(jìn)去 methodList.add(new InsertBatchSomeColumn()); return methodList; } }
2、在MybatisPlusConfig中配置?
@Bean public CustomSqlInjector customSqlInjector() { return new CustomSqlInjector(); }
3、添加自定義 Mapper接口?
/** * 自定義Mapper,添加批量插入接口 * @param <T> */ @Mapper public interface CustomMapper<T> extends BaseMapper<T> { /** * 批量插入 * @param entityList 實(shí)體列表 * @return 影響行數(shù) */ Integer insertBatchSomeColumn(Collection<T> entityList); }
4、將原來的Mapper業(yè)務(wù)接口,換成繼承此接口
@Mapper public interface StudentDao extends CustomMapper<Student> { List<Student> query(Student student); }
5、再進(jìn)行測試一下
@Transactional //事務(wù)注解要加上 @Override public void testBatchCreate() { List<Student> list = new ArrayList<>(); int startIndex = this.lambdaQuery().select(Student::getId).count(); for (int i = 1; i <= 1000000; i++) { Student student = new Student(); Long id = SnowFlake.nextId(); student.setId(id); student.setCode("studentCode-" + (startIndex + i)); student.setName("studentName-" + (startIndex + i)); list.add(student); } Long startTime = System.currentTimeMillis(); System.out.println("開始批量操作:" + startTime); int count = this.baseMapper.insertBatchSomeColumn(list); Long endTime = System.currentTimeMillis(); System.out.println("完成批量操作:" + endTime); System.out.println("用時(shí):" + (endTime - startTime)); System.out.println("保存成功:" + count); //this.saveBatch(list); }
測試結(jié)果為,1000000條數(shù)據(jù),用時(shí)大概在 35秒左右
?注意事項(xiàng):
1、mysql會提示超過? max_allowed_packet? 設(shè)置的最大值,到? my.cnf文件(windows為my.ini)修改就行了,我這直接改成? 200M文章來源:http://www.zghlxwxcb.cn/news/detail-615022.html
2、此處為示例,實(shí)際業(yè)務(wù)中循環(huán)不要放在事務(wù)里面文章來源地址http://www.zghlxwxcb.cn/news/detail-615022.html
?
到了這里,關(guān)于mybatis-plus 批量插入示例的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!