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

mybatis-plus 批量插入示例代碼

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

正常我們使用mybatis-plus插入的時候,首先想到的是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)友的文章,找到一個支持批量操作的方法,下面直接貼上代碼

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("用時:" + (endTime - startTime));
        System.out.println("保存成功:" + count);
        //this.saveBatch(list);
    }

測試結(jié)果為,1000000條數(shù)據(jù),用時大概在 35秒左右

注意事項(xiàng):

1、mysql會提示超過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)文章

最新評論

崇明县| 彭泽县| 昆明市| 裕民县| 湘乡市| 乌拉特前旗| 冕宁县| 澜沧| 永吉县| 霞浦县| 隆安县| 凤冈县| 凤翔县| 平利县| 靖边县| 潼关县| 安康市| 泰州市| 云和县| 伊春市| 临江市| 河曲县| 普安县| 孟州市| 泰来县| 清河县| 沙雅县| 富宁县| 治县。| 黄浦区| 六枝特区| 正定县| 波密县| 邓州市| 绥江县| 武夷山市| 白朗县| 松溪县| 肃北| 松桃| 通山县|