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

java多線程批量拆分List導(dǎo)入數(shù)據(jù)庫(kù)的實(shí)現(xiàn)過(guò)程

 更新時(shí)間:2021年10月30日 14:56:34   作者:AresCarry  
這篇文章主要給大家介紹了關(guān)于java多線程批量拆分List導(dǎo)入數(shù)據(jù)庫(kù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

一、前言

前兩天做了一個(gè)導(dǎo)入的功能,導(dǎo)入開(kāi)始的時(shí)候非常慢,導(dǎo)入2w條數(shù)據(jù)要1分多鐘,后來(lái)一點(diǎn)一點(diǎn)的優(yōu)化,從直接把list懟進(jìn)Mysql中,到分配把list導(dǎo)入Mysql中,到多線程把list導(dǎo)入Mysql中。時(shí)間是一點(diǎn)一點(diǎn)的變少了。非常的爽,最后變成了10s以內(nèi)。下面就展示一下過(guò)程。

二、直接把list懟進(jìn)Mysql

使用mybatis的批量導(dǎo)入操作:

  @Transactional(rollbackFor = Exception.class)
    public int addFreshStudentsNew2(List<FreshStudentAndStudentModel> list, String schoolNo) {
        if (list == null || list.isEmpty()) {
            return 0;
        }
        List<StudentEntity> studentEntityList = new LinkedList<>();
        List<EnrollStudentEntity> enrollStudentEntityList = new LinkedList<>();
        List<AllusersEntity> allusersEntityList = new LinkedList<>();

        for (FreshStudentAndStudentModel freshStudentAndStudentModel : list) {

            EnrollStudentEntity enrollStudentEntity = new EnrollStudentEntity();
            StudentEntity studentEntity = new StudentEntity();
            BeanUtils.copyProperties(freshStudentAndStudentModel, studentEntity);
            BeanUtils.copyProperties(freshStudentAndStudentModel, enrollStudentEntity);
            String operator = TenancyContext.UserID.get();
            String studentId = BaseUuidUtils.base58Uuid();
            enrollStudentEntity.setId(BaseUuidUtils.base58Uuid());
            enrollStudentEntity.setStudentId(studentId);
            enrollStudentEntity.setIdentityCardId(freshStudentAndStudentModel.getIdCard());
            enrollStudentEntity.setOperator(operator);
            studentEntity.setId(studentId);
            studentEntity.setIdentityCardId(freshStudentAndStudentModel.getIdCard());
            studentEntity.setOperator(operator);
            studentEntityList.add(studentEntity);
            enrollStudentEntityList.add(enrollStudentEntity);

            AllusersEntity allusersEntity = new AllusersEntity();
            allusersEntity.setId(enrollStudentEntity.getId());
            allusersEntity.setUserCode(enrollStudentEntity.getNemtCode());
            allusersEntity.setUserName(enrollStudentEntity.getName());
            allusersEntity.setSchoolNo(schoolNo);
            allusersEntity.setTelNum(enrollStudentEntity.getTelNum());
            allusersEntity.setPassword(enrollStudentEntity.getNemtCode());  //密碼設(shè)置為考生號(hào)
            allusersEntityList.add(allusersEntity);
        }
            enResult = enrollStudentDao.insertAll(enrollStudentEntityList);
            stuResult = studentDao.insertAll(studentEntityList);
            allResult = allusersFacade.insertUserList(allusersEntityList);

        if (enResult > 0 && stuResult > 0 && allResult) {
            return 10;
        }
        return -10;
    }

Mapper.xml

  <insert id="insertAll" parameterType="com.dmsdbj.itoo.basicInfo.entity.EnrollStudentEntity">
        insert into tb_enroll_student
        <trim prefix="(" suffix=")" suffixOverrides=",">
                id,  
                remark,  
                nEMT_aspiration,  
                nEMT_code,  
                nEMT_score,  
                student_id,  
                identity_card_id,  
                level,  
                major,  
                name,  
                nation,  
                secondary_college,  
                operator,  
                sex,  
                is_delete,  
                account_address,  
                native_place,  
                original_place,  
                used_name,  
                pictrue,  
                join_party_date,  
                political_status,  
                tel_num,  
                is_registry,  
                graduate_school,  
                create_time,  
                update_time        </trim>        
        values
        <foreach collection="list" item="item" index="index" separator=",">
        (
                #{item.id,jdbcType=VARCHAR},
                #{item.remark,jdbcType=VARCHAR},
                #{item.nemtAspiration,jdbcType=VARCHAR},
                #{item.nemtCode,jdbcType=VARCHAR},
                #{item.nemtScore,jdbcType=VARCHAR},
                #{item.studentId,jdbcType=VARCHAR},
                #{item.identityCardId,jdbcType=VARCHAR},
                #{item.level,jdbcType=VARCHAR},
                #{item.major,jdbcType=VARCHAR},
                #{item.name,jdbcType=VARCHAR},
                #{item.nation,jdbcType=VARCHAR},
                #{item.secondaryCollege,jdbcType=VARCHAR},
                #{item.operator,jdbcType=VARCHAR},
                #{item.sex,jdbcType=VARCHAR},
                0,
                #{item.accountAddress,jdbcType=VARCHAR},
                #{item.nativePlace,jdbcType=VARCHAR},
                #{item.originalPlace,jdbcType=VARCHAR},
                #{item.usedName,jdbcType=VARCHAR},
                #{item.pictrue,jdbcType=VARCHAR},
                #{item.joinPartyDate,jdbcType=VARCHAR},
                #{item.politicalStatus,jdbcType=VARCHAR},
                #{item.telNum,jdbcType=VARCHAR},
                #{item.isRegistry,jdbcType=TINYINT},
                #{item.graduateSchool,jdbcType=VARCHAR},
                now(),
                now()        
        )   
        </foreach>                
  </insert> 

代碼說(shuō)明:

底層的mapper是通過(guò)逆向工程來(lái)生成的,批量插入如下,是拼接成類似: insert into tb_enroll_student()values (),()…….() ;

這樣的缺點(diǎn)是,數(shù)據(jù)庫(kù)一般有一個(gè)默認(rèn)的設(shè)置,就是每次sql操作的數(shù)據(jù)不能超過(guò)4M。這樣插入,數(shù)據(jù)多的時(shí)候,數(shù)據(jù)庫(kù)會(huì)報(bào)錯(cuò)Packet for query is too large (6071393 > 4194304). You can change this value on the server by setting the max_allowed_packet' variable.,雖然我們可以通過(guò)

類似 修改 my.ini 加上 max_allowed_packet =67108864

67108864=64M

默認(rèn)大小4194304 也就是4M

修改完成之后要重啟mysql服務(wù),如果通過(guò)命令行修改就不用重啟mysql服務(wù)。

完成本次操作,但是我們不能保證項(xiàng)目單次最大的大小是多少,這樣是有弊端的。所以可以考慮進(jìn)行分組導(dǎo)入。

三、分組把list導(dǎo)入Mysql中

同樣適用mybatis批量插入,區(qū)別是對(duì)每次的導(dǎo)入進(jìn)行分組計(jì)算,然后分多次進(jìn)行導(dǎo)入:

 @Transactional(rollbackFor = Exception.class)
    public int addFreshStudentsNew2(List<FreshStudentAndStudentModel> list, String schoolNo) {
        if (list == null || list.isEmpty()) {
            return 0;
        }
        List<StudentEntity> studentEntityList = new LinkedList<>();
        List<EnrollStudentEntity> enrollStudentEntityList = new LinkedList<>();
        List<AllusersEntity> allusersEntityList = new LinkedList<>();

        for (FreshStudentAndStudentModel freshStudentAndStudentModel : list) {

            EnrollStudentEntity enrollStudentEntity = new EnrollStudentEntity();
            StudentEntity studentEntity = new StudentEntity();
            BeanUtils.copyProperties(freshStudentAndStudentModel, studentEntity);
            BeanUtils.copyProperties(freshStudentAndStudentModel, enrollStudentEntity);
            String operator = TenancyContext.UserID.get();
            String studentId = BaseUuidUtils.base58Uuid();
            enrollStudentEntity.setId(BaseUuidUtils.base58Uuid());
            enrollStudentEntity.setStudentId(studentId);
            enrollStudentEntity.setIdentityCardId(freshStudentAndStudentModel.getIdCard());
            enrollStudentEntity.setOperator(operator);
            studentEntity.setId(studentId);
            studentEntity.setIdentityCardId(freshStudentAndStudentModel.getIdCard());
            studentEntity.setOperator(operator);
            studentEntityList.add(studentEntity);
            enrollStudentEntityList.add(enrollStudentEntity);

            AllusersEntity allusersEntity = new AllusersEntity();
            allusersEntity.setId(enrollStudentEntity.getId());
            allusersEntity.setUserCode(enrollStudentEntity.getNemtCode());
            allusersEntity.setUserName(enrollStudentEntity.getName());
            allusersEntity.setSchoolNo(schoolNo);
            allusersEntity.setTelNum(enrollStudentEntity.getTelNum());
            allusersEntity.setPassword(enrollStudentEntity.getNemtCode());  //密碼設(shè)置為考生號(hào)
            allusersEntityList.add(allusersEntity);
        }

        int c = 100;
        int b = enrollStudentEntityList.size() / c;
        int d = enrollStudentEntityList.size() % c;

        int enResult = 0;
        int stuResult = 0;
        boolean allResult = false;

        for (int e = c; e <= c * b; e = e + c) {
            enResult = enrollStudentDao.insertAll(enrollStudentEntityList.subList(e - c, e));
            stuResult = studentDao.insertAll(studentEntityList.subList(e - c, e));
            allResult = allusersFacade.insertUserList(allusersEntityList.subList(e - c, e));
        }
        if (d != 0) {
            enResult = enrollStudentDao.insertAll(enrollStudentEntityList.subList(c * b, enrollStudentEntityList.size()));
            stuResult = studentDao.insertAll(studentEntityList.subList(c * b, studentEntityList.size()));
            allResult = allusersFacade.insertUserList(allusersEntityList.subList(c * b, allusersEntityList.size()));
        }

        if (enResult > 0 && stuResult > 0 && allResult) {
            return 10;
        }
        return -10;
    }

代碼說(shuō)明:

這樣操作,可以避免上面的錯(cuò)誤,但是分多次插入,無(wú)形中就增加了操作實(shí)踐,很容易超時(shí)。所以這種方法還是不值得提倡的。

再次改進(jìn),使用多線程分批導(dǎo)入。

四、多線程分批導(dǎo)入Mysql

依然使用mybatis的批量導(dǎo)入,不同的是,根據(jù)線程數(shù)目進(jìn)行分組,然后再建立多線程池,進(jìn)行導(dǎo)入。

  @Transactional(rollbackFor = Exception.class)
    public int addFreshStudentsNew(List<FreshStudentAndStudentModel> list, String schoolNo) {
        if (list == null || list.isEmpty()) {
            return 0;
        }
        List<StudentEntity> studentEntityList = new LinkedList<>();
        List<EnrollStudentEntity> enrollStudentEntityList = new LinkedList<>();
        List<AllusersEntity> allusersEntityList = new LinkedList<>();

        list.forEach(freshStudentAndStudentModel -> {
            EnrollStudentEntity enrollStudentEntity = new EnrollStudentEntity();
            StudentEntity studentEntity = new StudentEntity();
            BeanUtils.copyProperties(freshStudentAndStudentModel, studentEntity);
            BeanUtils.copyProperties(freshStudentAndStudentModel, enrollStudentEntity);
            String operator = TenancyContext.UserID.get();
            String studentId = BaseUuidUtils.base58Uuid();
            enrollStudentEntity.setId(BaseUuidUtils.base58Uuid());
            enrollStudentEntity.setStudentId(studentId);
            enrollStudentEntity.setIdentityCardId(freshStudentAndStudentModel.getIdCard());
            enrollStudentEntity.setOperator(operator);
            studentEntity.setId(studentId);
            studentEntity.setIdentityCardId(freshStudentAndStudentModel.getIdCard());
            studentEntity.setOperator(operator);
            studentEntityList.add(studentEntity);
            enrollStudentEntityList.add(enrollStudentEntity);

            AllusersEntity allusersEntity = new AllusersEntity();
            allusersEntity.setId(enrollStudentEntity.getId());
            allusersEntity.setUserCode(enrollStudentEntity.getNemtCode());
            allusersEntity.setUserName(enrollStudentEntity.getName());
            allusersEntity.setSchoolNo(schoolNo);
            allusersEntity.setTelNum(enrollStudentEntity.getTelNum());
            allusersEntity.setPassword(enrollStudentEntity.getNemtCode());  //密碼設(shè)置為考生號(hào)
            allusersEntityList.add(allusersEntity);
        });


        int nThreads = 50;

        int size = enrollStudentEntityList.size();
        ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
        List<Future<Integer>> futures = new ArrayList<Future<Integer>>(nThreads);

        for (int i = 0; i < nThreads; i++) {
            final List<EnrollStudentEntity> EnrollStudentEntityImputList = enrollStudentEntityList.subList(size / nThreads * i, size / nThreads * (i + 1));
            final List<StudentEntity> studentEntityImportList = studentEntityList.subList(size / nThreads * i, size / nThreads * (i + 1));
            final List<AllusersEntity> allusersEntityImportList = allusersEntityList.subList(size / nThreads * i, size / nThreads * (i + 1));

           Callable<Integer> task1 = () -> {
          studentSave.saveStudent(EnrollStudentEntityImputList,studentEntityImportList,allusersEntityImportList);
               return 1;
            };
          futures.add(executorService.submit(task1));
        }
        executorService.shutdown();
        if (!futures.isEmpty() && futures != null) {
            return 10;
        }
        return -10;
    }

代碼說(shuō)明:

上面是通過(guò)應(yīng)用ExecutorService 建立了固定的線程數(shù),然后根據(jù)線程數(shù)目進(jìn)行分組,批量依次導(dǎo)入。一方面可以緩解數(shù)據(jù)庫(kù)的壓力,另一個(gè)面線程數(shù)目多了,一定程度會(huì)提高程序運(yùn)行的時(shí)間。缺點(diǎn)就是要看服務(wù)器的配置,如果配置好的話就可以開(kāi)多點(diǎn)線程,配置差的話就開(kāi)小點(diǎn)。

五、小結(jié)

通過(guò)使用這個(gè)操作真是不斷的提高了,項(xiàng)目使用技巧也是不錯(cuò)。加油~~ 多線程哦~~

到此這篇關(guān)于java多線程批量拆分List導(dǎo)入數(shù)據(jù)庫(kù)的文章就介紹到這了,更多相關(guān)java多線程批量拆分List內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring?Cloud?Hystrix原理與注意事項(xiàng)小結(jié)

    Spring?Cloud?Hystrix原理與注意事項(xiàng)小結(jié)

    本文介紹了Hystrix的基本概念、工作原理以及其在實(shí)際開(kāi)發(fā)中的應(yīng)用方式,通過(guò)對(duì)Hystrix的深入學(xué)習(xí),開(kāi)發(fā)者可以在分布式系統(tǒng)中實(shí)現(xiàn)精細(xì)的錯(cuò)誤處理機(jī)制,并能夠及時(shí)響應(yīng)系統(tǒng)中的異常,避免服務(wù)的連鎖崩潰,感興趣的朋友一起看看吧
    2025-03-03
  • mybatis日志打印的兩款I(lǐng)DEA插件推薦

    mybatis日志打印的兩款I(lǐng)DEA插件推薦

    這篇文章主要給大家推薦介紹了關(guān)于mybatis日志打印的兩款I(lǐng)DEA插件,文中通過(guò)圖文以及實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用mybatis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2023-04-04
  • springbean的加載過(guò)程及應(yīng)用場(chǎng)景分析

    springbean的加載過(guò)程及應(yīng)用場(chǎng)景分析

    Bean已經(jīng)被實(shí)例化、屬性注入、初始化,并且注冊(cè)到容器中,可以被其他Bean或應(yīng)用程序使用,這篇文章主要介紹了springbean的加載過(guò)程以及應(yīng)用場(chǎng)景,需要的朋友可以參考下
    2024-04-04
  • JAVA文件讀寫操作詳解

    JAVA文件讀寫操作詳解

    這篇文章主要為大家詳細(xì)介紹了JAVA文件讀寫操作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-02-02
  • UniApp?+?SpringBoot?實(shí)現(xiàn)微信支付和退款功能

    UniApp?+?SpringBoot?實(shí)現(xiàn)微信支付和退款功能

    這篇文章主要介紹了UniApp?+?SpringBoot?實(shí)現(xiàn)微信支付和退款功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • Spring?@Scheduled定時(shí)器注解使用方式

    Spring?@Scheduled定時(shí)器注解使用方式

    這篇文章主要介紹了Spring?@Scheduled定時(shí)器注解使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • 淺談MyBatis-plus入門使用

    淺談MyBatis-plus入門使用

    這幾天本人了解到了MyBatis-plus,一個(gè) Mybatis 增強(qiáng)工具包.經(jīng)過(guò)一番研究,發(fā)現(xiàn)這玩意真的好用,不用寫任何 xml ,內(nèi)置通用的 Mapper,而且完全是面向?qū)ο缶幊?文檔給的示例代碼,跟之前用過(guò)的 sequelize (Node.js 的 ORM)非常像,因此本人也嘗試了一把, 需要的朋友可以參考下
    2021-05-05
  • 解決nacos報(bào)錯(cuò)java.lang.ClassNotFoundException: com.netflix.config.DynamicPropertyFactory的問(wèn)題

    解決nacos報(bào)錯(cuò)java.lang.ClassNotFoundException: com.netflix.

    這篇文章主要介紹了解決nacos報(bào)錯(cuò)java.lang.ClassNotFoundException: com.netflix.config.DynamicPropertyFactory的問(wèn)題,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Spring Cloud gateway 網(wǎng)關(guān)如何攔截Post請(qǐng)求日志

    Spring Cloud gateway 網(wǎng)關(guān)如何攔截Post請(qǐng)求日志

    這篇文章主要介紹了Spring Cloud gateway 網(wǎng)關(guān)如何攔截Post請(qǐng)求日志的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java經(jīng)典設(shè)計(jì)模式之適配器模式原理與用法詳解

    Java經(jīng)典設(shè)計(jì)模式之適配器模式原理與用法詳解

    這篇文章主要介紹了Java經(jīng)典設(shè)計(jì)模式之適配器模式,簡(jiǎn)單說(shuō)明了適配器模式的概念、原理,并結(jié)合實(shí)例形式分析了java適配器模式的用法與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-08-08

最新評(píng)論

开原市| 绥滨县| 郧西县| 日喀则市| 辽阳市| 庆安县| 廊坊市| 公主岭市| 台州市| 星座| 蓬溪县| 富平县| 两当县| 娱乐| 浠水县| 襄垣县| 柘城县| 舒兰市| 余江县| 富川| 浦北县| 定兴县| 夏津县| 瑞丽市| 龙岩市| 泾阳县| 兰考县| 都江堰市| 绥芬河市| 资源县| 黎平县| 灌南县| 新蔡县| 化州市| 资中县| 都匀市| 彝良县| 财经| 新平| 东乌珠穆沁旗| 酉阳|