国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

EasyExcel 批量導(dǎo)入并校驗(yàn)數(shù)據(jù)

這篇具有很好參考價(jià)值的文章主要介紹了EasyExcel 批量導(dǎo)入并校驗(yàn)數(shù)據(jù)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

前言

EasyExcel 批量導(dǎo)入并校驗(yàn)數(shù)據(jù)

一、pom

      <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.7</version>
        </dependency>

二、使用步驟

1.導(dǎo)入對(duì)象

日期形式的字段因?yàn)樾r?yàn)需要,提供了String類型的字段,再轉(zhuǎn)換賦值給真正的數(shù)據(jù)庫(kù)字段對(duì)象,如果不考慮校驗(yàn)問(wèn)題可直接轉(zhuǎn)換@ExcelProperty(value = "處罰信息公示日期", index = 5, converter = LocalDateConverter.class)

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.io.Serializable;
import java.time.LocalDate;

/**
 * 信用信息修復(fù)
 *
 * @author huaiyu.zhang
 * @since 2023-04-19 10:59:49
 */
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("信用信息修復(fù)")
public class CreditInfoExcelInReq implements Serializable {

    private static final long serialVersionUID = 370622351109421619L;


    @ApiModelProperty("${column.comment}")
    @ExcelIgnore
    private String id;

    @ApiModelProperty("企業(yè)名稱")
    @Length(max = 30, message = "企業(yè)名稱:最多可輸入30個(gè)字")
    @NotBlank(message = "企業(yè)名稱不能為空")
    @ExcelProperty(index = 0, value = "企業(yè)名稱")
    private String companyName;

    @ApiModelProperty("統(tǒng)一社會(huì)信用代碼")
    @Length(min = 18, max = 18, message = "統(tǒng)一社會(huì)信用代碼必須18位")
    @NotBlank(message = "統(tǒng)一社會(huì)信用代碼不能為空")
    @ExcelProperty(index = 1, value = "統(tǒng)一社會(huì)信用代碼")
    private String creditCode;

    @ExcelProperty(index = 2, value = "行政區(qū)劃--地市")
    @NotBlank(message = "行政區(qū)劃--地市不能為空")
    private String districtCodeCity;

    @ExcelProperty(index = 3, value = "行政區(qū)劃--區(qū)/縣")
    private String districtCodeCountry;

    @ApiModelProperty("行政區(qū)劃")
    @NotBlank(message = "行政區(qū)劃不能為空")
    @ExcelIgnore
    private String districtCode;

    @NotBlank(message = "失信行為類別不能為空")
    @ExcelProperty(index = 4, value = "失信行為類別")
    private String typeCodeName;

    @ApiModelProperty("失信行為類別 0一般失信行為 1-嚴(yán)重失信行為")
    @NotBlank(message = "失信行為類別不能為空")
    @ExcelIgnore
    private String typeCode;

    @ExcelProperty(index = 5, value = "處罰信息公示日期")
    @Pattern(regexp = "[0-9]{4}-[0-9]{2}-[0-9]{2}", message = "處罰信息公示日期格式必須為yyyy-MM-dd")
    private String punishTimeOri;

    @ApiModelProperty("處罰信息公示日期")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @ExcelIgnore
    private LocalDate punishTime;

    @ApiModelProperty("信用修復(fù)部門(mén)")
    @Length(max = 20, message = "信用修復(fù)部門(mén):最多可輸入20個(gè)字")
    @ExcelIgnore
    private String repairDepartment;

    @ApiModelProperty("信用修復(fù)完成日期")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @ExcelIgnore
    private LocalDate repairTime;

    @ApiModelProperty("狀態(tài) 0-未修復(fù) 1-已修復(fù)")
    @ExcelIgnore
    private String status;

    @ApiModelProperty("備注")
    @Length(max = 300, message = "備注:最多300字")
    @ExcelIgnore
    private String mark;

}

2.讀入數(shù)據(jù)并保存

讀取數(shù)據(jù)后Validation校驗(yàn),校驗(yàn)通過(guò)直接保存(如果數(shù)據(jù)已經(jīng)存在,則copy excel外其他字段后刪除原數(shù)據(jù),導(dǎo)入新數(shù)據(jù)),校驗(yàn)失敗則返回失敗行數(shù)(這里也可以導(dǎo)出校驗(yàn)失敗詳情)

默認(rèn)規(guī)則:設(shè)置excel最大導(dǎo)入數(shù)據(jù)行數(shù)為LIST_COUNT = 1000;,
如果需要導(dǎo)入更多數(shù)據(jù),改大這個(gè)值即可,也可invoke時(shí)分批讀取數(shù)據(jù)
但是每次執(zhí)行完invoke后都會(huì)執(zhí)行doAfterAllAnalysed下的saveData,那么校驗(yàn)邏輯將只針對(duì)本批次數(shù)據(jù)進(jìn)行校驗(yàn),如校驗(yàn)失敗,會(huì)直接返回給前端。后續(xù)批次由于異常被拋出不會(huì)執(zhí)行(可更改校驗(yàn)邏輯,或錯(cuò)誤信息返回形式)

Listener 端代碼:

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.gsafety.bg.sv.model.dto.req.CreditInfoExcelInReq;
import com.gsafety.bg.sv.model.dto.req.CreditInfoReq;
import com.gsafety.bg.sv.model.dto.resp.BasDistrictResp;
import com.gsafety.bg.sv.model.po.CreditInfoPO;
import com.gsafety.bg.sv.service.CreditInfoService;
import com.gsafety.bg.sv.service.constant.CreditTypeEnum;
import com.gsafety.bg.sv.service.utils.MappingConvertUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import java.time.LocalDate;
import java.util.*;

/**
 * @author huaiyu.zhang
 * @since 2023-6-5 0005 17:10
 */

@Slf4j
public class CreditInfoExcelDataListener extends AnalysisEventListener<CreditInfoExcelInReq> {

    private final Integer LIST_COUNT = 1000;

    List<CreditInfoExcelInReq> list = new ArrayList<>(LIST_COUNT);

    // 由于監(jiān)聽(tīng)器只能通過(guò)new的方式創(chuàng)建,所以可以通過(guò)構(gòu)造器傳入dao層對(duì)象
    private final CreditInfoService service;

    public CreditInfoExcelDataListener(CreditInfoService service) {
        this.service = service;
    }

    @Override
    public void invoke(CreditInfoExcelInReq req, AnalysisContext analysisContext) {
        //每讀取一行數(shù)據(jù)都會(huì)調(diào)用一次
        list.add(req);
        if (list.size() >= LIST_COUNT) {
            throw new ExcelAnalysisException("當(dāng)前excel數(shù)據(jù)量不得大于" + LIST_COUNT + "條!");
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        //所有數(shù)據(jù)解析完畢執(zhí)行該方法
        // 防止導(dǎo)入空的Excel
        if (analysisContext.readRowHolder().getRowIndex() <= 0) {
            throw new ExcelAnalysisException("當(dāng)前excel無(wú)數(shù)據(jù)!");
        }
        saveData();
    }

    protected void saveData() {
        Set<String> errorRow = new HashSet<>();
        List<BasDistrictResp> districtList = MappingConvertUtil.getDistrictList();
        list.forEach(l -> {
            Integer row = list.indexOf(l) + 2;
            if (ObjectUtils.isEmpty(l.getDistrictCodeCountry())) {
                Optional<BasDistrictResp> opt = districtList.stream().filter(d -> l.getDistrictCodeCity().equals(d.getDistName())).findFirst();
                if (!opt.isPresent()) {
                    errorRow.add(row.toString());
                } else {
                    l.setDistrictCode(opt.get().getDistCode());
                }
            } else {
                String parentCode = districtList.stream().filter(d -> l.getDistrictCodeCity().equals(d.getDistName())).findFirst()
                        .orElse(BasDistrictResp.builder().distCode("").build()).getDistCode();
                Optional<BasDistrictResp> opt = districtList.stream().filter(d -> parentCode.equals(d.getParentCode()) && l.getDistrictCodeCountry().equals(d.getDistName())).findFirst();
                if (!opt.isPresent()) {
                    errorRow.add(row.toString());
                } else {
                    l.setDistrictCode(opt.get().getDistCode());
                }
            }
            l.setTypeCode(CreditTypeEnum.getCode(l.getTypeCodeName()));
            Set<ConstraintViolation<Object>> validate = Validation.buildDefaultValidatorFactory().getValidator().validate(l);
            //用于存儲(chǔ)驗(yàn)證后的錯(cuò)誤信息
            if (validate.size() > 0) {
                errorRow.add(row.toString());
                //防止相同數(shù)據(jù)indexof定位錯(cuò)誤
                l.setId(UUID.randomUUID().toString());
            } else {
                //日期格式校驗(yàn)成功后再轉(zhuǎn)換punishTimeOri,否則直接報(bào)錯(cuò)
                l.setPunishTime(LocalDate.parse(l.getPunishTimeOri()));
                CreditInfoReq req = new CreditInfoReq();
                //構(gòu)造新數(shù)據(jù)覆蓋舊數(shù)據(jù)
                BeanUtils.copyProperties(l, req);
                Optional<CreditInfoPO> opt = service.loadByCreditCode(l.getCreditCode());
                if (opt.isPresent()) {
                    req.setRepairTime(opt.get().getRepairTime());
                    req.setStatus(opt.get().getStatus());
                    req.setMark(opt.get().getMark());
                    req.setRepairDepartment(opt.get().getRepairDepartment());
                    //刪除舊數(shù)據(jù)
                    service.delete(opt.get().getId());
                }
                l.setId(service.add(req));
            }
            if (row - 1 == list.size() && errorRow.size() > 0) {
                throw new ExcelAnalysisException("部分導(dǎo)入成功,其中第" + String.join(",", errorRow) + "行導(dǎo)入失??!", null);
            }
        });
    }
}

service 端代碼:

    public String importData(MultipartFile file) {
        CreditInfoExcelDataListener listener = new CreditInfoExcelDataListener(this);
        InputStream inputStream;
        try {
            inputStream = file.getInputStream();
            EasyExcel.read(inputStream, CreditInfoExcelInReq.class,
                    listener).sheet().doRead();
            return "全部導(dǎo)入成功!";
        } catch (IOException e) {
            throw new BusinessCheckException("Excel 文件流讀取失敗");
        } catch (ExcelAnalysisException e) {
            return e.getMessage();
        } catch (Exception e) {
            throw new BusinessException("數(shù)據(jù)導(dǎo)入失敗", e);
        }
    }

EasyExcel 批量導(dǎo)入并校驗(yàn)數(shù)據(jù)

EasyExcel 批量導(dǎo)入并校驗(yàn)數(shù)據(jù)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-479936.html

到了這里,關(guān)于EasyExcel 批量導(dǎo)入并校驗(yàn)數(shù)據(jù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • EasyExcel復(fù)雜表頭數(shù)據(jù)導(dǎo)入

    EasyExcel復(fù)雜表頭數(shù)據(jù)導(dǎo)入

    參考文章:EasyExcel動(dòng)態(tài)復(fù)雜表頭導(dǎo)出方法

    2024年02月06日
    瀏覽(19)
  • SpringBoot使用EasyExcel批量導(dǎo)出500萬(wàn)數(shù)據(jù)

    SpringBoot使用EasyExcel批量導(dǎo)出500萬(wàn)數(shù)據(jù)

    記錄下學(xué)習(xí)SpringBoot使用EasyExcel批量導(dǎo)出百萬(wàn)數(shù)據(jù)。留著以后備用。 本地環(huán)境mysql安裝的5.7版本,項(xiàng)目使用jdk1.8版本,項(xiàng)目使用的mysql驅(qū)動(dòng)版本為8.0版本。 這一篇博客內(nèi)容代碼基于我的這篇博客: SpringBoot使用mybatis批量新增500萬(wàn)數(shù)據(jù)到mysql數(shù)據(jù)庫(kù)Demo,在自己這篇博客的代碼上做

    2024年02月12日
    瀏覽(19)
  • 數(shù)據(jù)導(dǎo)入導(dǎo)出(POI以及easyExcel)

    數(shù)據(jù)導(dǎo)入導(dǎo)出(POI以及easyExcel)

    ????????將一些數(shù)據(jù)庫(kù)信息導(dǎo)出為Excel表格 ????????將Excel表格數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù) ? ? ? ? 大量數(shù)據(jù)的導(dǎo)入導(dǎo)出操作 常?的解決?案為: Apache POI 與阿?巴巴 easyExcel Apache POI 是基于 Office Open XML 標(biāo)準(zhǔn)( OOXML )和 Microsoft 的 OLE 2 復(fù)合?檔 格式( OLE2 )處理各種?件格式的

    2024年02月13日
    瀏覽(22)
  • 基于EasyExcel的數(shù)據(jù)導(dǎo)入導(dǎo)出(復(fù)制可用)

    基于EasyExcel的數(shù)據(jù)導(dǎo)入導(dǎo)出(復(fù)制可用)

    目錄 ? 前言: 新建SpringBoot項(xiàng)目,引入下面的依賴 數(shù)據(jù)導(dǎo)入導(dǎo)出執(zhí)行原理和思路: 用戶端邏輯: 后臺(tái)開(kāi)發(fā)邏輯: 代碼實(shí)現(xiàn) 下拉框策略 批注策略 數(shù)據(jù)讀取監(jiān)聽(tīng) Excel工具類 創(chuàng)建導(dǎo)入數(shù)據(jù)模板類 創(chuàng)建數(shù)據(jù)導(dǎo)出模板 Web接口 結(jié)果展示 模板下載 數(shù)據(jù)導(dǎo)入 數(shù)據(jù)導(dǎo)出 ? 代碼復(fù)制粘貼

    2024年02月05日
    瀏覽(26)
  • 使用EasyExcel實(shí)現(xiàn)模板下載、數(shù)據(jù)導(dǎo)入功能

    使用EasyExcel實(shí)現(xiàn)模板下載、數(shù)據(jù)導(dǎo)入功能

    1.在你的工程下添加模板文件 2.編寫(xiě)代碼實(shí)現(xiàn)下載功能 controller serviceImpl 好了,到這里,一個(gè)簡(jiǎn)單的下載模板功能就實(shí)現(xiàn)了。不過(guò)我在項(xiàng)目運(yùn)行中遇到了一些坑,下面記錄一下。 3.項(xiàng)目中遇到的坑:excel文件在springboot的maven項(xiàng)目下打了jar包后損壞 4.解決辦法:試了很多種,主要

    2024年02月13日
    瀏覽(98)
  • EasyExcel導(dǎo)出帶下拉選數(shù)據(jù)的Excel數(shù)據(jù)導(dǎo)入模板

    EasyExcel導(dǎo)出帶下拉選數(shù)據(jù)的Excel數(shù)據(jù)導(dǎo)入模板

    #因?yàn)轫?xiàng)目中需要導(dǎo)入一些信息,但是這些信息比較不常見(jiàn),且在項(xiàng)目字典數(shù)據(jù)中維護(hù)有這些數(shù)據(jù),所以在導(dǎo)出模板的時(shí)候,把這些數(shù)據(jù)一并導(dǎo)出,可以減少用戶的編寫(xiě),避免在導(dǎo)入的時(shí)候因?yàn)閿?shù)據(jù)錯(cuò)誤,發(fā)生一些業(yè)務(wù)問(wèn)題 直接開(kāi)始 1、以崗位類型為例,展示數(shù)據(jù)的實(shí)現(xiàn)方式

    2024年02月03日
    瀏覽(32)
  • 【二十四】springboot使用EasyExcel和線程池實(shí)現(xiàn)多線程導(dǎo)入Excel數(shù)據(jù)

    【二十四】springboot使用EasyExcel和線程池實(shí)現(xiàn)多線程導(dǎo)入Excel數(shù)據(jù)

    ??springboot篇章整體欄目:? 【一】springboot整合swagger(超詳細(xì) 【二】springboot整合swagger(自定義)(超詳細(xì)) 【三】springboot整合token(超詳細(xì)) 【四】springboot整合mybatis-plus(超詳細(xì))(上) 【五】springboot整合mybatis-plus(超詳細(xì))(下) 【六】springboot整合自定義全局異常

    2023年04月08日
    瀏覽(23)
  • java實(shí)現(xiàn)excel的導(dǎo)入導(dǎo)出(帶參數(shù)校驗(yàn):非空校驗(yàn)、數(shù)據(jù)格式校驗(yàn))

    java實(shí)現(xiàn)excel的導(dǎo)入導(dǎo)出(帶參數(shù)校驗(yàn):非空校驗(yàn)、數(shù)據(jù)格式校驗(yàn))

    本次封裝引入阿里開(kāi)源框架EasyExcel,EasyExcel是一個(gè)基于Java的簡(jiǎn)單、省內(nèi)存的讀寫(xiě)Excel的開(kāi)源項(xiàng)目。在盡可能節(jié)約內(nèi)存的情況下支持讀寫(xiě)百M(fèi)的Excel。 github地址:GitHub - alibaba/easyexcel: 快速、簡(jiǎn)潔、解決大文件內(nèi)存溢出的java處理Excel工具 。 64M內(nèi)存20秒讀取75M(46W行25列)的Excel(3.0.2

    2024年02月01日
    瀏覽(43)
  • Elasticsearch 批量導(dǎo)入數(shù)據(jù)

    **Elasticsearch**是一款非常高效的全文檢索引擎。 **Elasticsearch**可以非常方便地進(jìn)行數(shù)據(jù)的多維分析,所以大數(shù)據(jù)分析領(lǐng)域也經(jīng)常會(huì)見(jiàn)到它的身影,生產(chǎn)環(huán)境中絕大部分新產(chǎn)生的數(shù)據(jù)可以通過(guò)應(yīng)用直接導(dǎo)入,但是歷史或初始數(shù)據(jù)可能會(huì)需要單獨(dú)處理,這種情況下可能遇到需要導(dǎo)

    2023年04月11日
    瀏覽(18)
  • 【大數(shù)據(jù)】Hive 中的批量數(shù)據(jù)導(dǎo)入

    在博客【大數(shù)據(jù)】Hive 表中插入多條數(shù)據(jù) 中,我簡(jiǎn)單介紹了幾種向 Hive 表中插入數(shù)據(jù)的方法。然而更多的時(shí)候,我們并不是一條數(shù)據(jù)一條數(shù)據(jù)的插入,而是以批量導(dǎo)入的方式。在本文中,我將較為全面地介紹幾種向 Hive 中批量導(dǎo)入數(shù)據(jù)的方法。 overwrite :表示覆蓋表中已有數(shù)

    2024年02月11日
    瀏覽(18)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包