easyexcel上传校验的方法封装

easyexcel版本3.1.5

使用自定义注解的方式来定义校验的类型,避免冗余代码。

//校验value不能为空,且长度最大为30
@RowCheck(value = {RowCheckType.EMPTY,RowCheckType.LENGTH},max = 30)
private String value;

 具体代码

首先定义校验类型枚举RowCheckType:

package com.zhou.util.easyexcel;/*** 校验类型枚举* @author lang.zhou*/
public enum RowCheckType {EMPTY, //非空EMAIL, //邮件PATTERN, //正则LENGTH, //长度DATE, //日期ENUM //枚举
}

 定义校验不通过默认的提示语模板:

package com.zhou.zhou.util.easyexcel;/*** @author lang.zhou*/
public class CheckMessage {public static final String DEFAULT = "default";public static final String EMPTY = "第{n}行{c}不能为空";public static final String EMAIL = "第{n}行{c}邮箱格式错误";public static final String PATTERN = "第{n}行{c}格式错误";public static final String LENGTH = "第{n}行{c}长度超过{len}";public static final String DATE = "第{n}行{c}日期格式错误";public static final String ENUM = "第{n}行{c}不在枚举中";
}

定义校验的接口类ValueChecker :

package com.zhou.util.easyexcel.checker;import com.zhou.util.easyexcel.RowCheckType;/*** @author lang.zhou*/
public interface ValueChecker {/*** 实现校验逻辑*/boolean check(Object value);/*** 获得校验类型*/RowCheckType getType();/*** 获得默认校验提示*/String getDefaultMessage();
}

校验工厂类CheckFactory:

package com.zhou.util.easyexcel.checker;import com.zhou.util.easyexcel.RowCheck;
import com.zhou.util.easyexcel.RowCheckType;/*** @author lang.zhou*/
public class CheckerFactory {public static ValueChecker createChecker(RowCheckType type, RowCheck check){ValueChecker checker;if(type == RowCheckType.EMPTY){checker = new EmptyChecker();}else if(type == RowCheckType.EMAIL){checker = new EmailChecker();}else if(type == RowCheckType.PATTERN){checker = new PatternChecker(check.format());}else if(type == RowCheckType.LENGTH){checker = new LengthChecker(check.max());}else if(type == RowCheckType.DATE){checker = new DateChecker(check.format());}else if(type == RowCheckType.ENUM){checker = new EnumChecker(check.format());}else{checker = new EmptyChecker();}return checker;}
}

 封装的注解@RowCheck:

package com.zhou.util.easyexcel;import java.lang.annotation.*;/*** @author lang.zhou*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RowCheck {/*** 校验类型*/RowCheckType[] value() default {};String[] msg() default {CheckMessage.DEFAULT};/*** 正则校验/日期格式/枚举json*/String format() default "";/*** 最大长度校验*/int max() default 0;}

再定义一个easyexcel导入的映射实体类和excel模板如下:

package com.zhou.util.easyexcel.model;import com.alibaba.excel.annotation.ExcelProperty;
import com.zhou.util.easyexcel.RowCheck;
import com.zhou.util.easyexcel.RowCheckType;
import lombok.Data;import java.io.Serializable;/*** @author lang.zhou*/
@Data
public class ImportModel implements Serializable {private static final long serialVersionUID = 1L;@ExcelProperty(index = 0, value = "用户名")@RowCheck(value = {RowCheckType.EMPTY,RowCheckType.LENGTH},max = 30)private String username;@ExcelProperty(index = 1, value = "年龄")@RowCheck(value = RowCheckType.PATTERN,msg = "第{n}行年龄必须是整数")private String age;@ExcelProperty(index = 2, value = "邮箱")@RowCheck(value = {RowCheckType.EMPTY,RowCheckType.EMAIL,RowCheckType.LENGTH},msg = {CheckMessage.DEFAULT,CheckMessage.DEFAULT,CheckMessage.DEFAULT},max = 3000)private String email;}

实现校验的核心逻辑:

package com.zhou.util.easyexcel;import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.zhou.util.easyexcel.checker.CheckerFactory;
import com.zhou.util.easyexcel.checker.EnumChecker;
import com.zhou.util.easyexcel.checker.ValueChecker;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.SneakyThrows;import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.*;/*** @author lang.zhou*/
@EqualsAndHashCode(callSuper = true)
@Data
public abstract class AbstractExcelReadListener<T> extends AnalysisEventListener<T> {/*** 每次读取数量*/protected int batchSize = 1000;/*** 数据总理*/protected int dataCount = 0;/*** 回调次数*/protected int times = 0;protected int sheetIndex = 0;protected Class<T> headerClass;/*** 数据读取开始行*/protected int headerLine = 1;protected Map<String, ValueChecker[]> checkTypeMap = new LinkedHashMap<>(0);protected Map<String,RowCheck> checkMap = new LinkedHashMap<>(0);protected Map<String,String> columnDesc = new LinkedHashMap<>(0);/*** 读取的数据缓存*/protected List<T> dataList = new ArrayList<>();public AbstractExcelReadListener(Class<T> headerClass) {this.headerClass = headerClass;}/*** 读取一行后回调* @param data  (列索引0开始 -> 值)*/@Overridepublic void invoke(T data, AnalysisContext context) {dataList.add(data);if(dataList.size() >= batchSize){callback();}}/*** 读取结束回调*/@Overridepublic void doAfterAllAnalysed(AnalysisContext context) {callback();}/*** 读取头信息时初始化校验器*/@Overridepublic void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) {if(headerClass != null){Field[] fields = headerClass.getDeclaredFields();for (Field field : fields) {RowCheck check = field.getAnnotation(RowCheck.class);ExcelProperty ep = field.getAnnotation(ExcelProperty.class);if(check != null){RowCheckType[] checkType = check.value();if(checkType.length > 0){ValueChecker[] valueChecks = new ValueChecker[checkType.length];for (int i = 0; i < checkType.length; i++) {RowCheckType rowCheckType = checkType[i];//拿到校验器ValueChecker checker = CheckerFactory.createChecker(rowCheckType, check);valueChecks[i] = checker;}checkTypeMap.put(field.getName(),valueChecks);checkMap.put(field.getName(),check);}}if(ep != null){columnDesc.put(field.getName(),ep.value()[0]);}}}}private void callback(){if(dataList.size() > 0){dataCount += dataList.size();this.dataCallback(dataList, ++ times);dataList.clear();}}/*** 获取dataList中当前索引在全部批次数据中的行数* @param i     数据在当前批次dataList中的索引*/protected int getRowNum(int i){return (times - 1) * batchSize + i + 1;}/*** 读取batchSize行后回调*/public abstract void dataCallback(List<T> dataList,int times);public void read(InputStream stream){ExcelReader excelReader = EasyExcelFactory.read(stream, this).headRowNumber(headerLine).build();ReadSheet readSheet = EasyExcel.readSheet(sheetIndex).head(headerClass).build();excelReader.read(readSheet);excelReader.finish();}private String getMsg(ValueChecker checker, String[] msg, int i){String err = i < msg.length ? msg[i] : checker.getDefaultMessage();if(Objects.equals(err,CheckMessage.DEFAULT)){err = checker.getDefaultMessage();}return err;}/*** 校验一行数据(性能有待验证)* @param t         数据行* @param errMsg    存放校验错误信息* @return          true-校验通过*/@SneakyThrowsprotected boolean checkRow(T t, Set<String> errMsg){if(checkTypeMap.size() > 0){int dataIndex = dataList.indexOf(t);int rowNum = getRowNum(dataIndex);Field[] fields = headerClass.getDeclaredFields();for (Field field : fields) {RowCheck check = checkMap.get(field.getName());ValueChecker[] checkers = checkTypeMap.get(field.getName());if(checkers != null){String[] msg = check.msg();field.setAccessible(true);Object o = field.get(t);for (int i = 0; i < checkers.length; i++) {ValueChecker checker = checkers[i];if (!checker.check(o)) {String err = getMsg(checker,msg,i);if(StringUtils.isNotBlank(err)){err = err.replace("{n}",String.valueOf(rowNum)).replace("{v}",String.valueOf(o)).replace("{len}",String.valueOf(check.max())).replace("{c}",columnDesc.get(field.getName()));errMsg.add(err);return false;}}if(checker instanceof EnumChecker){Object enumKey = ((EnumChecker) checker).getEnumKey(o);field.set(t,enumKey);}}}}}return true;}}

其他的校验实现类:

@Data
public class EmptyChecker implements ValueChecker {private RowCheckType type = RowCheckType.EMPTY;private String defaultMessage = CheckMessage.EMPTY;@Overridepublic boolean check(Object value) {return !isEmpty(value);}public static boolean isEmpty(Object o){if(o==null){return true;}if(o instanceof String){return StringTool.isBlank(o.toString());}if(o instanceof Collection){return ((Collection<?>) o).isEmpty();}if(o instanceof Map){return ((Map<?,?>) o).isEmpty();}if(o.getClass().isArray()){return Array.getLength(o) == 0;}return false;}}@Data
public class DateChecker implements ValueChecker {private RowCheckType type = RowCheckType.DATE;private String defaultMessage = CheckMessage.DATE;private String format;public DateChecker(String format) {this.format = format;}@Overridepublic boolean check(Object value) {if(value != null){Date date = TimeUtil.formatDate(value.toString(), format);return date != null;}return true;}
}@Data
public class EmailChecker implements ValueChecker {public static final Pattern EMAIL_PATTERN = Pattern.compile("^\\w+([-\\\\.]\\w+)*@\\w+([-\\\\.]\\w+)*\\.\\w+([-\\\\.]\\w+)*$");private RowCheckType type = RowCheckType.EMAIL;private String defaultMessage = CheckMessage.EMAIL;@Overridepublic boolean check(Object value) {if(value != null){String str = value.toString();return StringUtils.isNotBlank(str) && EMAIL_PATTERN.matcher(str).matches();;}return true;}
}/*** @author lang.zhou*/
@Data
public class EnumChecker implements ValueChecker {private RowCheckType type = RowCheckType.ENUM;private String defaultMessage = CheckMessage.ENUM;private Map<String,?> json;public EnumChecker(String format) {try {this.json = reserveMap(JSON.parseObject(format));}catch (Exception e){//ignore}}private Map<String,Object> reserveMap(Map<String, String> enums){Map<String,Object> map = new HashMap<>(enums.size());for (Map.Entry<String, String> entry : enums.entrySet()) {map.put(entry.getValue(),entry.getKey());}return map;}@Overridepublic boolean check(Object value) {if(value != null){return getEnumKey(value) != null;}return true;}public Object getEnumKey(Object value) {if(value != null && json != null){return json.get(value.toString());}return null;}
}@Data
public class LengthChecker implements ValueChecker {private RowCheckType type = RowCheckType.LENGTH;private String defaultMessage = CheckMessage.LENGTH;private int max = 0;public LengthChecker(int max) {this.max = max;}@Overridepublic boolean check(Object value) {if(value != null){return value.toString().length() <= max;}return true;}
}@Data
public class PatternChecker implements ValueChecker {private RowCheckType type = RowCheckType.PATTERN;private String defaultMessage = CheckMessage.PATTERN;private Pattern pattern;public PatternChecker(String format) {pattern = Pattern.compile(format);}@Overridepublic boolean check(Object value) {if(value != null){return pattern.matcher(value.toString()).matches();}return true;}}

调用方式:

        //错误信息Set<String> err = new LinkedHashSet<>();AbstractExcelReadListener<ImportModel> listener = new AbstractExcelReadListener<ImportModel>(ImportModel.class) {@Overridepublic void dataCallback(List<ConsignorinfoImportModel> dataList, int times) {log.info("第{}次读取后保存",times);for (int i = 0; i < dataList.size(); i++) {ImportModel dto = dataList.get(i);Set<String> oneErr = new LinkedHashSet<>();try{//内置校验if(!this.checkRow(dto,oneErr)){continue;}//todo//这里可以进行自定义校验}finally {err.addAll(oneErr);}}}};listener.read(stream);

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/344473.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

缓存学习实战篇

缓存练习题&#xff08;用户查询操作&#xff09; public List<ShopType> queryAllType() throws JsonProcessingException {//从缓存中查数据String shopTypeJson stringRedisTemplate.opsForValue().get("cache:shopType");//如果缓存命中&#xff0c;if (S…

短视频账号矩阵剪辑分发系统无人直播技术开发源头

一、全行业独家源头最全面的核心技术 短视频矩阵新玩法是指利用批量自动混剪系统来处理大量短视频&#xff0c;通过智能算法自动进行视频剪辑、场景切换、特效添加等操作&#xff0c;最终生成高质量、精彩纷呈的混剪视频作品的方法和技术。这一方法的出现使得大规模短视频制作…

LangChain 71 字符串评估器String Evaluation衡量在多样化数据上的性能和完整性

LangChain系列文章 LangChain 60 深入理解LangChain 表达式语言23 multiple chains链透传参数 LangChain Expression Language (LCEL)LangChain 61 深入理解LangChain 表达式语言24 multiple chains链透传参数 LangChain Expression Language (LCEL)LangChain 62 深入理解Lang…

如何让GPT/GPT4成为你的编程助手?

详情点击链接&#xff1a;如何让GPT/GPT4成为你的编程助手&#xff1f; 一OpenAI 1.最新大模型GPT-4 Turbo 2.最新发布的高级数据分析&#xff0c;AI画图&#xff0c;图像识别&#xff0c;文档API 3.GPT Store 4.从0到1创建自己的GPT应用 5. 模型Gemini以及大模型Claude2二…

Django框架完成读者浏览书籍,图书详情页,借阅管理

前情回顾&#xff1a; 使用Django框架实现简单的图书借阅系统——完成图书信息管理 文章目录 1.完成展示图书信息功能1.1django 静态资源管理问题1.2编写图书展示模板HTML 2.完成图书详情页功能2.1从后端获取图书详情信息2.2详情页面展示图书数据 3.完成借阅管理功能3.1管理员…

高压消防泵:科技与安全性的完美结合

在现代社会&#xff0c;随着科技的不断发展&#xff0c;各种高科技设备层出不穷&#xff0c;为我们的生活带来了极大的便利。在森林火灾扑救领域&#xff0c;恒峰智慧科技研发的高压消防泵作为一种高效、节能、绿色、环保的优质设备&#xff0c;将科技与安全性完美地结合在一起…

蓝桥杯练习题(五)

&#x1f4d1;前言 本文主要是【算法】——蓝桥杯练习题&#xff08;五&#xff09;的文章&#xff0c;如果有什么需要改进的地方还请大佬指出⛺️ &#x1f3ac;作者简介&#xff1a;大家好&#xff0c;我是听风与他&#x1f947; ☁️博客首页&#xff1a;CSDN主页听风与他 …

【计算机组成原理】IEEE 754 标准定义的浮点数表示格式

IEEE 754 IEEE 754是一种由美国电气和电子工程师协会&#xff08;IEEE&#xff09;制定的标准&#xff0c;用于定义浮点数的表示和运算。这个标准定义了浮点数的格式、舍入规则、特殊值的处理以及算术操作的执行方式。 IEEE 754浮点数标准主要定义了两种浮点数格式&#xff1…

Windows11 安装MySQL8.0操作

一、从MySQL官网下载MySQL安装包 官网地址&#xff1a; www.mysql.com &#xff08;1&#xff09;首先 选择 DOWNLOADS 下载界面 &#xff08;2&#xff09;其次选择 MySQL 客户端 下载 &#xff08;3&#xff09;选择windows安装MySQL &#xff08;4&#xff09;选择MySQL类型…

Jenkins 问题

从gitlab 仓库拉去代码到Jenkins本地报错 ERROR: Couldn’t find any revision to build. Verify the repository and branch configuration for this job. 问题原因&#xff1a; 创建条目》配置的时候&#xff0c;gitlab仓库不存在master分支 修复后&#xff1a;

分裂联邦学习论文-混合联邦分裂学习GAN驱动的预测性多目标优化

论文标题&#xff1a;《Predictive GAN-Powered Multi-Objective Optimization for Hybrid Federated Split Learning》 期刊&#xff1a;IEEE Transactions on Communications, 2023 一、论文介绍 背景&#xff1a;联邦学习作为一种多设备协同训练的边缘智能算法&#xff0…

使用nginx+HTML2canvas将任意html网页转为png图片自定义张数

文章目录 概述网页的转换html2canvas的使用导入导入HTML2canvas库函数定义 nginx部署编写控制截图网页代码iframe 网页控制代码 测试说明 概述 本文简述如何使用nginxhtml2canvas将任意网页html转为png图片 网页的转换 如果是本地网页&#xff0c;直接进行nginx反向代理就行…