<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Springboot使用EasyExcel讀取大量數(shù)據(jù)并且將得到的數(shù)據(jù)分批插入到數(shù)據(jù)庫(kù)中

          共 3125字,需瀏覽 7分鐘

           ·

          2020-12-04 15:48

          點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號(hào)”

          優(yōu)質(zhì)文章,第一時(shí)間送達(dá)

          66套java從入門到精通實(shí)戰(zhàn)課程分享

          一、pom文件中添加依賴并編寫相關(guān)的實(shí)體類

          pom文件添加依賴

          ???????
          ????????????com.alibaba
          ????????????easyexcel
          ????????????2.1.6
          ????????


          編寫相關(guān)的實(shí)體類

          同時(shí)如果有Date這樣的特殊類型,可以再添加@DateTimeFormat注解,使傳入的參數(shù)能夠按照需求格式化
          時(shí)間類型格式化

          ????@ExcelProperty(value?=?"BIRTHDAY",?index?=?5)
          ????@DateTimeFormat(pattern?=?"yyyy-MM-dd?HH:mm:ss")
          ????private?Date?birthday;

          二、編寫監(jiān)聽(tīng)器

          //創(chuàng)建的監(jiān)聽(tīng)器必須繼承?AnalysisEventListener,泛型為剛剛創(chuàng)建的實(shí)體類
          public?class?ExcelListener?extends?AnalysisEventListener?{
          //由于數(shù)據(jù)是按行讀取,所以此處需要?jiǎng)?chuàng)建兩個(gè)靜態(tài)變量來(lái)實(shí)現(xiàn)批量插入
          ????public??static??int?vipCount;
          //EasyExcel不能使用Spring注解,所以直接創(chuàng)建一個(gè)對(duì)應(yīng)的Service(這個(gè)Service就是Controller中所調(diào)用的Service)
          ????public?AddVipsServiceTest?addVipsService;
          ????public??static?List?list=new?ArrayList();

          ????public?ExcelListener()?{
          ????}
          //有參構(gòu)造
          ????public?ExcelListener(AddVipsServiceTest?addVipsServiceTest)?{
          ????????this.addVipsService?=?addVipsServiceTest;
          ????}
          ????//此處為得到一行后的數(shù)據(jù)轉(zhuǎn)換為實(shí)體類
          ????@Override
          ????public?void?invoke(Users?users,?AnalysisContext?context)?{
          ????//添加到list中
          ????????list.add(users);
          ????????vipCount++;
          ????????//得到當(dāng)前操作表格的所有行數(shù),由于行數(shù)包含了表頭,所以需要減一為所有數(shù)據(jù)的條目數(shù)
          ????????Integer?a?=context.getTotalCount()-1;
          ????????//讀取的行數(shù)到行尾時(shí),將剩下的數(shù)據(jù)全部插入到數(shù)據(jù)庫(kù)中
          ????????if?(a==vipCount){
          //????????????System.out.println(list);
          ????????????addVipsService.addVip(list);
          ????????}
          ????????//每當(dāng)list中存儲(chǔ)的條目數(shù)達(dá)到2000條時(shí),批量插入到數(shù)據(jù)庫(kù)中,并清空當(dāng)前l(fā)ist的數(shù)據(jù)
          ????????if?(list.size()%2000==0){
          //????????????System.out.println(list);
          ????????????addVipsService.addVip(list);
          //????????????System.out.println(2);
          ????????????list.clear();
          ????????}
          ????}

          ????//最開(kāi)始執(zhí)行的函數(shù),可以返回表頭的數(shù)據(jù)
          ????@Override
          ????public?void?invokeHeadMap(Map?headMap,?AnalysisContext?context)?{
          ????????System.out.println(headMap);
          ????}

          ????//全部讀取完成之后執(zhí)行函數(shù)
          ????@Override
          ????public?void?doAfterAllAnalysed(AnalysisContext?context)?{
          ????????System.out.println("讀取表格內(nèi)容完畢");
          ????}
          }

          編寫對(duì)應(yīng)的Service,執(zhí)行read方法

          創(chuàng)建供Controller調(diào)用的Service

          //此處編寫插入的方法
          @Service
          public?class?AddVipsServiceTest?{
          ????@Autowired
          ???private?VipMapper?vipMapper;
          ????@Transactional(rollbackFor?=?Exception.class)
          ????public?Integer?addVip(List?list)?{
          ????????vipMapper.insertVips(list);
          ????????return?1;
          ????}

          ????public?void?readExcel(String?path,AddVipsServiceTest?addVipsServiceTest){

          ????}
          }

          編寫對(duì)應(yīng)的Service,執(zhí)行read方法

          ?@Service
          public?class?AddVipsService?extends?AddVipsServiceTest{
          //path為讀取文件的路徑,此處需要繼承剛才創(chuàng)建的Service并重寫readExcel方法
          ????@Override
          ????public?void?readExcel(String?path,AddVipsServiceTest?addVipsServiceTest)?{
          ????????EasyExcel.read(path,Users.class,?new?ExcelListener(addVipsServiceTest)).sheet().doRead();
          ????}
          }

          三、編寫controller

          對(duì)應(yīng)的控制器中的方法,此處使用了定時(shí)任務(wù),控制器同理


          ????public?ReturnT?orderStatusClose(String?param){
          ????????XxlJobLogger.log("["+this.name+"]開(kāi)始");
          //????????System.out.println(vipMapper);
          //傳入對(duì)應(yīng)的表格地址
          ????????addVipsService.readExcel("C:\\Users\\Desktop\\VIP.xlsx",addVipsService);
          ????????XxlJobLogger.log("["+this.name+"]結(jié)束");
          ????????return?ReturnT.SUCCESS;
          ????}




          版權(quán)聲明:本文為博主原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接和本聲明。

          本文鏈接:

          https://blog.csdn.net/qq_37135460/article/details/110131008





          粉絲福利:實(shí)戰(zhàn)springboot+CAS單點(diǎn)登錄系統(tǒng)視頻教程免費(fèi)領(lǐng)取

          ???

          ?長(zhǎng)按上方微信二維碼?2 秒
          即可獲取資料



          感謝點(diǎn)贊支持下哈?

          瀏覽 64
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  久久99人妻精品 | 成人高清视频精品在线 | 大香蕉久久久久久久 | 五月天黄色网址 | 日韩高清国产精品 |