最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

mybatis-plus 批量插入示例代碼

 更新時(shí)間:2023年07月31日 09:17:07   作者:傾聽-靜軒水月  
正常我們使用mybatis-plus插入的時(shí)候,首先想到的是saveBatch方法,不過看了下打印出來的sql和底層代碼,才發(fā)現(xiàn)它并不是真正的批量插入這篇文章主要介紹了mybatis-plus 批量插入示例,需要的朋友可以參考下

正常我們使用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)控控制臺(tái)發(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會(huì)提示超過max_allowed_packet設(shè)置的最大值,到 my.cnf文件(windows為my.ini)修改就行了,我這直接改成 200M

2、此處為示例,實(shí)際業(yè)務(wù)中循環(huán)不要放在事務(wù)里面

到此這篇關(guān)于mybatis-plus 批量插入示例的文章就介紹到這了,更多相關(guān)mybatis-plus 批量插入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

双城市| 山丹县| 利津县| 淮南市| 定州市| 内乡县| 桂东县| 广南县| 永登县| 乌鲁木齐市| 谢通门县| 井陉县| 泗洪县| 荣成市| 万山特区| 济宁市| 枣庄市| 本溪市| 平顺县| 巴彦淖尔市| 安宁市| 武穴市| 赣州市| 新沂市| 南城县| 普兰店市| 左云县| 华宁县| 九台市| 中方县| 青田县| 开江县| 杭锦后旗| 巴塘县| 乐都县| 淅川县| 台湾省| 鄢陵县| 云南省| 视频| 福建省|