首页 文章 精选 留言 我的

精选列表

搜索[泛型],共10000篇文章
优秀的个人博客,低调大师

🏆【Alibaba工具型技术系列】「EasyExcel技术专题」实战技术针对于项目中常用的Excel操作指南

EasyExcel教程 本文使用的技术是Alibaba集团开源的EasyExcel技术,该技术是针对Apache POI技术的封装和优化,主要解决了POI技术的耗内存问题,并且提供了较好的API使用。 使用步骤繁琐 动态写出Excel操作非常麻烦 对于新手来说,很难在短时间内上手 读写时需要占用较大的内容,当数据量大时容器发生OOM Maven依赖 <!-- easyexcel 依赖 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.2.7</version> </dependency> EasyExcel API分析介绍 Ø EasyExcel入口类,用于构建开始各种操作 Ø ExcelReaderBuilder ExcelWriterBuilder 构建出一个 ReadWorkbook WriteWorkbook,可以理解成一个excel对象,一个excel只要构建一个 Ø ExcelReaderSheetBuilder ExcelWriterSheetBuilder 构建出一个 ReadSheet WriteSheet对象,可以理解成excel里面的一页,每一页都要构建一个 Ø ReadListener在每一行读取完毕后都会调用ReadListener来处理数据 Ø WriteHandler在每一个操作包括创建单元格、创建表格等都会调用WriteHandler来处理数据 Ø 所有配置都是继承的,Workbook的配置会被Sheet继承,所以在用EasyExcel设置参数的时候,在EasyExcel…sheet()方法之前作用域是整个sheet,之后针对单个sheet EasyExcel 注解 Ø ExcelProperty 指定当前字段对应excel中的那一列。可以根据名字或者Index去匹配。当然也可以不写,默认第一个字段就是index=0,以此类推。千万注意,要么全部不写,要么全部用index,要么全部用名字去匹配。千万别三个混着用,除非你非常了解源代码中三个混着用怎么去排序的。 Ø ExcelIgnore 默认所有字段都会和excel去匹配,加了这个注解会忽略该字段 Ø DateTimeFormat 日期转换,用String去接收excel日期格式的数据会调用这个注解。里面的value参照java.text.SimpleDateFormat Ø NumberFormat 数字转换,用String去接收excel数字格式的数据会调用这个注解。里面的value参照java.text.DecimalFormat Ø ExcelIgnoreUnannotated 默认不加ExcelProperty 的注解的都会参与读写,加了不会参与 通用参数 Ø ReadWorkbook,ReadSheet 都会有的参数,如果为空,默认使用上级。 Ø converter 转换器,默认加载了很多转换器。也可以自定义。 Ø readListener 监听器,在读取数据的过程中会不断的调用监听器。 Ø headRowNumber 需要读的表格有几行头数据。默认有一行头,也就是认为第二行开始起为数据。 Ø head 与clazz二选一。读取文件头对应的列表,会根据列表匹配数据,建议使用class。 Ø clazz 与head二选一。读取文件的头对应的class,也可以使用注解。如果两个都不指定,则会读取全部数据。 Ø autoTrim 字符串、表头等数据自动trim Ø password 读的时候是否需要使用密码 ReadWorkbook(理解成excel对象)参数 Ø excelType 当前excel的类型 默认会自动判断 Ø inputStream 与file二选一。读取文件的流,如果接收到的是流就只用,不用流建议使用file参数。因为使用了inputStream easyexcel会帮忙创建临时文件,最终还是file Ø file 与inputStream二选一。读取文件的文件。 Ø autoCloseStream 自动关闭流。 Ø readCache 默认小于5M用 内存,超过5M会使用 EhCache,这里不建议使用这个参数。 ReadSheet(就是excel的一个Sheet)参数 Ø sheetNo 需要读取Sheet的编码,建议使用这个来指定读取哪个Sheet Ø sheetName 根据名字去匹配Sheet,excel 2003不支持根据名字去匹配 注解 Ø ExcelProperty index 指定写到第几列,默认根据成员变量排序。value指定写入的名称,默认成员变量的名字,多个value可以参照快速开始中的复杂头 Ø ExcelIgnore 默认所有字段都会写入excel,这个注解会忽略这个字段 Ø DateTimeFormat 日期转换,将Date写到excel会调用这个注解。里面的value参照java.text.SimpleDateFormat Ø NumberFormat 数字转换,用Number写excel会调用这个注解。里面的value参照java.text.DecimalFormat Ø ExcelIgnoreUnannotated 默认不加ExcelProperty 的注解的都会参与读写,加了不会参与 参数 通用参数 Ø WriteWorkbook,WriteSheet ,WriteTable都会有的参数,如果为空,默认使用上级。 Ø converter 转换器,默认加载了很多转换器。也可以自定义。 Ø writeHandler 写的处理器。可以实现WorkbookWriteHandler,SheetWriteHandler,RowWriteHandler,CellWriteHandler,在写入excel的不同阶段会调用 Ø relativeHeadRowIndex 距离多少行后开始。也就是开头空几行 Ø needHead 是否导出头 Ø head 与clazz二选一。写入文件的头列表,建议使用class。 Ø clazz 与head二选一。写入文件的头对应的class,也可以使用注解。 Ø autoTrim 字符串、表头等数据自动trim WriteWorkbook(理解成excel对象)参数 Ø excelType 当前excel的类型 默认xlsx Ø outputStream 与file二选一。写入文件的流 Ø file 与outputStream二选一。写入的文件 Ø templateInputStream 模板的文件流 Ø templateFile 模板文件 Ø autoCloseStream 自动关闭流。 Ø password 写的时候是否需要使用密码 Ø useDefaultStyle 写的时候是否是使用默认头 WriteSheet(就是excel的一个Sheet)参数 Ø sheetNo 需要写入的编码。默认0 Ø sheetName 需要些的Sheet名称,默认同sheetNo WriteTable(就把excel的一个Sheet,一块区域看一个table)参数 Ø tableNo 需要写入的编码。默认0 EasyExcel用法指南 简单的读取excel文件 public void read() { String fileName = "demo.xlsx"; // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭 // 参数一:读取的excel文件路径 // 参数二:读取sheet的一行,将参数封装在DemoData实体类中 // 参数三:读取每一行的时候会执行DemoDataListener监听器 EasyExcel.read(fileName, DemoData.class, new DemoDataListener()).sheet().doRead(); } 简单的写入excel文件 @Test public void simpleWrite() { String fileName = "demo.xlsx"; // 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 // 如果这里想使用03 则 传入excelType参数即可 // 参数一:写入excel文件路径 // 参数二:写入的数据类型是DemoData // data()方法是写入的数据,结果是List<DemoData>集合 EasyExcel.write(fileName, DemoData.class).sheet("模板").doWrite(data()); } Web上传与下载 /** excel文件的下载 */ @GetMapping("download") public void download(HttpServletResponse response) throws IOException { response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); response.setHeader("Content-disposition", "attachment;filename=demo.xlsx"); EasyExcel.write(response.getOutputStream(), DownloadData.class).sheet("模板").doWrite(data()); } /** excel文件的上传 */ @PostMapping("upload") @ResponseBody public String upload(MultipartFile file) throws IOException { EasyExcel.read(file.getInputStream(), DemoData.class, new DemoDataListener()).sheet().doRead(); return "success"; } 详解读取Excel 对象模型 // 如果没有特殊说明,下面的案例将默认使用这个实体类 public class DemoData { /** * 强制读取第三个 这里不建议 index 和 name 同时用,要么一个对象只用index,要么一个对象只用name去匹配 */ @ExcelProperty(index = 2) //我想接收百分比的数字 @NumberFormat("#.##%") @ExcelProperty(value="浮点数标题", converter = CustomStringConverter.class) private Double doubleData; /** * 用名字去匹配,这里需要注意,如果名字重复,会导致只有一个字段读取到数据 */ @ExcelProperty(value="字符串标题", converter = CustomStringConverter.class) // converter属性定义自己的字符串转换器 private String string; @DateTimeFormat("yyyy年MM月dd日 HH时mm分ss秒") @ExcelProperty("日期标题") //这里用string 去接日期才能格式化 private Date date; } 监听器 // 如果没有特殊说明,下面的案例将默认使用这个监听器 public class DemoDataListener extends AnalysisEventListener<DemoData> { List<DemoData> list = new ArrayList<DemoData>(); /** * 如果使用了spring,请使用这个构造方法。每次创建Listener的时候需要把spring管理的类传进来 */ public DemoDataListener() {} /** * 这个每一条数据解析都会来调用 * * @param data * @param context */ @Override public void invoke(DemoData data, AnalysisContext context) { System.out.println("解析到一条数据:{}", JSON.toJSONString(data)); list.add(data); } /** * 所有数据解析完成了 都会来调用 * * @param context */ @Override public void doAfterAllAnalysed(AnalysisContext context) { System.out.println(JSON.toJSONString(list)); } } 代码 @Test public void simpleRead() { // 写法1: String fileName = "demo.xlsx"; // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭 EasyExcel.read(fileName, DemoData.class, new DemoDataListener()).sheet().doRead(); // 写法2: fileName = "demo.xlsx"; ExcelReader excelReader = EasyExcel.read(fileName, DemoData.class, new DemoDataListener()).build(); ReadSheet readSheet = EasyExcel.readSheet(0).build(); excelReader.read(readSheet); // 这里千万别忘记关闭,读的时候会创建临时文件,到时磁盘会崩的 excelReader.finish(); } 读取多个sheet @Test public void repeatedRead() { String fileName = "demo.xlsx"; // 读取全部sheet // 这里需要注意 DemoDataListener的doAfterAllAnalysed 会在每个sheet读取完毕后调用一次。然后所有sheet都会往同一个DemoDataListener里面写 EasyExcel.read(fileName, DemoData.class, new DemoDataListener()).doReadAll(); // 读取部分sheet fileName = "demo.xlsx"; ExcelReader excelReader = EasyExcel.read(fileName).build(); // 这里为了简单 所以注册了 同样的head 和Listener 自己使用功能必须不同的Listener // readSheet参数设置读取sheet的序号 ReadSheet readSheet1 = EasyExcel.readSheet(0).head(DemoData.class).registerReadListener(new DemoDataListener()).build(); ReadSheet readSheet2 = EasyExcel.readSheet(1).head(DemoData.class).registerReadListener(new DemoDataListener()).build(); // 这里注意 一定要把sheet1 sheet2 一起传进去,不然有个问题就是03版的excel 会读取多次,浪费性能 excelReader.read(readSheet1, readSheet2); // 这里千万别忘记关闭,读的时候会创建临时文件,到时磁盘会崩的 excelReader.finish(); } 自定义格式转换 public class CustomStringStringConverter implements Converter<String> { @Override public Class supportJavaTypeKey() { return String.class; } @Override public CellDataTypeEnum supportExcelTypeKey() { return CellDataTypeEnum.STRING; } /** * 这里读的时候会调用 * * @param cellData * NotNull * @param contentProperty * Nullable * @param globalConfiguration * NotNull * @return */ @Override public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { return "自定义:" + cellData.getStringValue(); } /** * 这里是写的时候会调用 不用管 * * @param value * NotNull * @param contentProperty * Nullable * @param globalConfiguration * NotNull * @return */ @Override public CellData convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { return new CellData(value); } } 多行头 @Test public void complexHeaderRead() { String fileName = "demo.xlsx"; // 这里 需要指定读用哪个class去读,然后读取第一个sheet EasyExcel.read(fileName, DemoData.class, new DemoDataListener()).sheet() // 这里可以设置1,因为头就是一行。如果多行头,可以设置其他值。不传入默认1行 .headRowNumber(1).doRead(); } 读取表头数据 覆盖监听器invokeHeadMap方法 /** * 这里会一行行的返回头 * 监听器只需要重写这个方法就可以读取到头信息 * @param headMap * @param context */ @Override public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) { LOGGER.info("解析到一条头数据:{}", JSON.toJSONString(headMap)); } 异常处理 覆盖监听器onException方法 /** * 监听器实现这个方法就可以在读取数据的时候获取到异常信息 */ @Override public void onException(Exception exception, AnalysisContext context) { LOGGER.error("解析失败,但是继续解析下一行:{}", exception.getMessage()); // 如果是某一个单元格的转换异常 能获取到具体行号 // 如果要获取头的信息 配合invokeHeadMap使用 if (exception instanceof ExcelDataConvertException) { ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException)exception; LOGGER.error("第{}行,第{}列解析异常", excelDataConvertException.getRowIndex(), excelDataConvertException.getColumnIndex()); } } 导出指定的列 @Test public void excludeOrIncludeWrite() { String fileName = "excludeOrIncludeWrite" + System.currentTimeMillis() + ".xlsx"; // 忽略 date 不导出 Set<String> excludeColumnFiledNames = new HashSet<String>(); excludeColumnFiledNames.add("date"); // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 EasyExcel.write(fileName, DemoData.class).excludeColumnFiledNames(excludeColumnFiledNames).sheet("忽略date") .doWrite(data()); fileName = "excludeOrIncludeWrite" + System.currentTimeMillis() + ".xlsx"; // 根据用户传入字段 假设我们只要导出 date Set<String> includeColumnFiledNames = new HashSet<String>(); includeColumnFiledNames.add("date"); // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 EasyExcel.write(fileName, DemoData.class).includeColumnFiledNames(includeColumnFiledNames).sheet("导出date") .doWrite(data()); } 调整指定列顺序 public class IndexData { /** * 导出的excel第二列和第四列将空置 */ @ExcelProperty(value = "字符串标题", index = 0) private String string; @ExcelProperty(value = "日期标题", index = 2) private Date date; @ExcelProperty(value = "浮点数标题", index = 4) private Double doubleData; } 复杂头写入 public class ComplexHeadData { /** * 主标题 将整合为一个单元格效果如下: * ————————————————————————— * | 主标题 | * ————————————————————————— * |字符串标题|日期标题|数字标题| * ————————————————————————— */ @ExcelProperty({"主标题", "字符串标题"}) private String string; @ExcelProperty({"主标题", "日期标题"}) private Date date; @ExcelProperty({"主标题", "数字标题"}) private Double doubleData; } 前面属于主标题,后面属于副标题 图片导出 @Data @ContentRowHeight(200) @ColumnWidth(200 / 8) public class ImageData { // 图片导出方式有5种 private File file; private InputStream inputStream; /** * 如果string类型 必须指定转换器,string默认转换成string,该转换器是官方支持的 */ @ExcelProperty(converter = StringImageConverter.class) private String string; private byte[] byteArray; /** * 根据url导出 版本2.1.1才支持该种模式 */ private URL url; } @Test public void imageWrite() throws Exception { String fileName = "imageWrite" + System.currentTimeMillis() + ".xlsx"; // 如果使用流 记得关闭 InputStream inputStream = null; try { List<ImageData> list = new ArrayList<ImageData>(); ImageData imageData = new ImageData(); list.add(imageData); String imagePath = "converter" + File.separator + "img.jpg"; // 放入五种类型的图片 根据实际使用只要选一种即可 imageData.setByteArray(FileUtils.readFileToByteArray(new File(imagePath))); imageData.setFile(new File(imagePath)); imageData.setString(imagePath); inputStream = FileUtils.openInputStream(new File(imagePath)); imageData.setInputStream(inputStream); imageData.setUrl(new URL( "https://raw.githubusercontent.com/alibaba/easyexcel/master/src/test/resources/converter/img.jpg")); EasyExcel.write(fileName, ImageData.class).sheet().doWrite(list); } finally { if (inputStream != null) { inputStream.close(); } } } 列宽、行高 @Data @ContentRowHeight(10) @HeadRowHeight(20) @ColumnWidth(25) public class WidthAndHeightData { @ExcelProperty("字符串标题") private String string; @ExcelProperty("日期标题") private Date date; /** * 宽度为50,覆盖上面的宽度25 */ @ColumnWidth(50) @ExcelProperty("数字标题") private Double doubleData; } @HeadRowHeight(value = 35) // 表头行高 @ContentRowHeight(value = 25) // 内容行高 @ColumnWidth(value = 50) // 列宽 此外还有,自适应宽度,但是这个不是特别精确 @Test void contextLoads() { EasyExcel.write("自适应.xlsx", Student.class) .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) .sheet() .doWrite(getData()); } 动态表头 @Test public void dynamicHeadWrite() { String fileName = "dynamicHeadWrite" + System.currentTimeMillis() + ".xlsx"; EasyExcel.write(fileName) // 这里放入动态头 .head(head()).sheet("模板") // 当然这里数据也可以用 List<List<String>> 去传入 .doWrite(data()); } // 动态表头的数据格式List<List<String>> private List<List<String>> head() { List<List<String>> list = new ArrayList<List<String>>(); List<String> head0 = new ArrayList<String>(); head0.add("字符串" + System.currentTimeMillis()); List<String> head1 = new ArrayList<String>(); head1.add("数字" + System.currentTimeMillis()); List<String> head2 = new ArrayList<String>(); head2.add("日期" + System.currentTimeMillis()); list.add(head0); list.add(head1); list.add(head2); return list; } 合并单元格 @Test public void mergeWrite() { String fileName = "mergeWrite" + System.currentTimeMillis() + ".xlsx"; LoopMergeStrategy loopMergeStrategy = new LoopMergeStrategy(2, 0); EasyExcel.write(fileName, DemoData.class).registerWriteHandler(loopMergeStrategy).sheet("合并单元格") .doWrite(data()); } 每隔2行会合并 把eachColumn 设置成 3 也就是我们数据的长度,所以就第一列会合并。当然其他合并策略也可以自己写 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 web数据写出 @GetMapping("download") public void download(HttpServletResponse response) throws IOException { response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 String fileName = URLEncoder.encode("数据写出", "UTF-8"); response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); EasyExcel.write(response.getOutputStream(), DownloadData.class).sheet("模板").doWrite(data()); } 模板格式导出 如果需要横向填充只需要模板设置好就可以。 简单的Excel模板 public class FillData { private String name; private double number; // getting setting } 实现模板填充 @Test public void simpleFill() { String templateFileName = "simple.xlsx"; // 方案1 根据对象填充 String fileName = System.currentTimeMillis() + ".xlsx"; // 这里 会填充到第一个sheet, 然后文件流会自动关闭 FillData fillData = new FillData(); fillData.setName("知春秋"); fillData.setNumber(25); EasyExcel.write(fileName).withTemplate(templateFileName).sheet().doFill(fillData); // 方案2 根据Map填充 fileName = System.currentTimeMillis() + ".xlsx"; // 这里 会填充到第一个sheet, 然后文件流会自动关闭 Map<String, Object> map = new HashMap<String, Object>(); map.put("name", "知春秋"); map.put("number", 25); EasyExcel.write(fileName).withTemplate(templateFileName).sheet().doFill(map); } 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"{","}"代替 复杂的填充 使用List集合的方法批量写入数据,点表示该参数是集合 @Test public void complexFill() { String templateFileName = "complex.xlsx"; String fileName = System.currentTimeMillis() + ".xlsx"; ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build(); WriteSheet writeSheet = EasyExcel.writerSheet().build(); // 如果数据量大 list不是最后一行 参照下一个 FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build(); excelWriter.fill(data(), fillConfig, writeSheet); excelWriter.fill(data(), fillConfig, writeSheet); // 其他参数可以使用Map封装 Map<String, Object> map = new HashMap<String, Object>(); excelWriter.fill(map, writeSheet); excelWriter.finish(); } 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"{","}"代替 // {} 代表普通变量 {.} 代表是list的变量 // 这里注意 入参用了forceNewRow 代表在写入list的时候不管list下面有没有空行 都会创建一行,然后下面的数据往后移动。默认 是false,会直接使用下一行,如果没有则创建。 // forceNewRow 如果设置了true,有个缺点 就是他会把所有的数据都放到内存了,所以慎用 // 简单的说 如果你的模板有list,且list不是最后一行,下面还有数据需要填充 就必须设置 forceNewRow=true 但是这个就会把所有数据放到内存 会很耗内存 总结一个工具类 public class ExcelUtil { /** * 写出一个 excel 文件到本地 * <br /> * 将类型所有加了 @ExcelProperty 注解的属性全部写出 * * @param fileName 文件名 不要后缀 * @param sheetName sheet名 * @param data 写出的数据 * @param clazz 要写出数据类的Class类型对象 * @param <T> 写出的数据类型 */ public static <T> void writeExcel(String fileName, String sheetName, List<T> data, Class<T> clazz) { writeExcel(null, fileName, sheetName, data, clazz); } /** * 按照指定的属性名进行写出 一个 excel * * @param attrName 指定的属性名 必须与数据类型的属性名一致 * @param fileName 文件名 不要后缀 * @param sheetName sheet名 * @param data 要写出的数据 * @param clazz 要写出数据类的Class类型对象 * @param <T> 要写出的数据类型 */ public static <T> void writeExcel(Set<String> attrName, String fileName, String sheetName, List<T> data, Class<T> clazz) { fileName = StringUtils.isBlank(fileName) ? "学生管理系统" : fileName; sheetName = StringUtils.isBlank(sheetName) ? "sheet0" : sheetName; try(FileOutputStream fos = new FileOutputStream(fileName)) { write(fos,attrName,sheetName,data,clazz); } catch (Exception exception) { exception.printStackTrace(); } } /** * 读取 指定格式的 excel文档 * * @param fileName 文件名 * @param clazz 数据类型的class对象 * @param <T> 数据类型 * @return */ public static <T> List<T> readExcel(String fileName, Class<T> clazz) { return readExcel(fileName, clazz, null); } /** * 取 指定格式的 excel文档 * 注意一旦传入自定义监听器,则返回的list为空,数据需要在自定义监听器里面获取 * * @param fileName 文件名 * @param clazz 数据类型的class对象 * @param readListener 自定义监听器 * @param <T> 数据类型 * @return */ public static <T> List<T> readExcel(String fileName, Class<T> clazz, ReadListener<T> readListener) { try(FileInputStream fis = new FileInputStream(fileName)) { return read(fis,clazz,readListener); } catch (Exception exception) { exception.printStackTrace(); } } /** * 导出 一个 excel * 导出excel所有数据 * @param response * @param fileName 件名 最好为英文,不要后缀名 * @param sheetName sheet名 * @param data 要写出的数据 * @param clazz 要写出数据类的Class类型对象 * @param <T> 要写出的数据类型 */ public static <T> void export(HttpServletResponse response, String fileName, String sheetName, List<T> data, Class<T> clazz) { export(response, null, fileName, sheetName, data, clazz); } /** * 按照指定的属性名进行写出 一个 excel * * @param response * @param attrName 指定的属性名 必须与数据类型的属性名一致 * @param fileName 文件名 最好为英文,不要后缀名 * @param sheetName sheet名 * @param data 要写出的数据 * @param clazz 要写出数据类的Class类型对象 * @param <T> 要写出的数据类型 */ public static <T> void export(HttpServletResponse response, Set<String> attrName, String fileName, String sheetName, List<T> data, Class<T> clazz) { fileName = StringUtils.isBlank(fileName) ? "student-system-manager" : fileName; sheetName = StringUtils.isBlank(sheetName) ? "sheet0" : sheetName; response.setContentType("application/vnd.ms-excel;charset=utf-8"); response.setCharacterEncoding("utf-8"); response.addHeader("Content-disposition", "attachment;filename=" + fileName + ExcelTypeEnum.XLSX.getValue()); try(OutputStream os = response.getOutputStream()) { write(os,attrName,sheetName,data,clazz); } catch (IOException e) { e.printStackTrace(); } } /** * 接收一个excel文件,并且进行解析 * 注意一旦传入自定义监听器,则返回的list为空,数据需要在自定义监听器里面获取 * @param multipartFile excel文件 * @param clazz 数据类型的class对象 * @param readListener 监听器 * @param <T> * @return */ public static <T> List<T> importExcel(MultipartFile multipartFile,Class<T> clazz,ReadListener<T> readListener) { try(InputStream inputStream = multipartFile.getInputStream()) { return read(inputStream,clazz,readListener); } catch (IOException e) { e.printStackTrace(); } } private static <T> void write(OutputStream os, Set<String> attrName, String sheetName, List<T> data, Class<T> clazz) { ExcelWriterBuilder write = EasyExcel.write(os, clazz); // 如果没有指定要写出那些属性数据,则写出全部 if (!CollectionUtils.isEmpty(attrName)) { write.includeColumnFiledNames(attrName); } write.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).sheet(sheetName).doWrite(data); } private static <T> List<T> read(InputStream in,Class<T> clazz, ReadListener<T> readListener) { List<T> list = new ArrayList<>(); Optional<ReadListener> optional = Optional.ofNullable(readListener); EasyExcel.read(in, clazz, optional.orElse(new AnalysisEventListener<T>() { @Override public void invoke(T data, AnalysisContext context) { list.add(data); } @Override public void doAfterAllAnalysed(AnalysisContext context) { System.out.println("解析完成"); } })).sheet().doRead(); return list; } } 参考资料 https://github.com/alibaba/easyexcel/blob/master/docs/API.md 官方api https://github.com/alibaba/easyexcel easyexcel github 地址 https://blog.csdn.net/sinat_32366329/article/details/103109058 easyexcel总结 https://alibaba-easyexcel.github.io/index.html 官方示例

优秀的个人博客,低调大师

用 WorkBuddy 搭建规则型审核风控:把上万条人工驳回记录变成一张“自动化地图”

我在一家网约车平台负责司机入驻审核团队。和大多数内容审核、资质审核团队一样,我们的日常是这样:每天成百上千条入驻申请涌进来,审核员逐条看材料、选驳回原因、点提交。系统里预设了二十多个驳回原因,但没有任何分级、没有自动化规则、也没有风险指标——所有风险判断,100% 靠人。

优秀的个人博客,低调大师

异地多园区大数据机房环境管控:网络型温湿度传感器局域网汇聚联动方案

异地多园区大数据机房,是由多个独立园区算力节点共同组成的分布式算力集群,不同园区之间依靠运营商专线、SD-WAN、IPsec隧道完成广域互联,每个园区内部又包含多栋机房楼宇、多个独立机房、大量高密度服务器机柜。这类场景的核心矛盾,集中在本地实时联动的低时延要求与跨园区广域网链路不稳定之间。很多集成商在方案设计阶段,直接采用中心平台远程下发联动指令的模式,把温湿度采集、逻辑判断、空调调节全部交给远端总部平台,一旦园区和总部之间的广域网链路出现抖动、丢包、断连,精密空调、机柜风机、除湿单元的联动闭环直接失效,极易引发机柜热点、局部高温宕机、湿度过高带来的线路腐蚀、静电风险。

优秀的个人博客,低调大师

Node.js 中使用worker_threads处理CPU密集型任务时,为什么你的主线程还是卡住了?

在 Node.js 服务里做图片压缩、PDF 解析或者加解密时,经常会遇到一个尴尬的情况:明明已经把计算逻辑放进了 worker_threads,接口响应时间还是飙升,主线程的 event loop 延迟照样高得离谱。问题往往不在 worker 本身,而在于线程间通信的方式。很多实现里主线程用 `worker.postMessage` 把大 Buffer 发给 worker,worker 处理完再 `postMessage` 回来,这中间发生了结构化克隆,数据被完整复制了两遍。对于几十 MB 的 Buffer,复制开销直接压在主线程上,event loop 被同步的序列化操作堵死,HTTP 请求只能排队。这篇内容围绕 worker_threads 的数据传递机制,讲清楚什么场景下会触发主线程阻塞,如何用 SharedArrayBuffer 和 transferList 把复制成本降下来,以及在线程池场景下怎么设计消息协议才不拖慢主线程。 ## 先复现那个卡顿 假设有一个图片上传接口,收到文件后交给 worker 做灰度处理。代码大概长这样: ```js // main.js const { Worker } = require('worker_threads'); const worker = new Worker('./gray-worker.js'); app.post('/upload', async (req, res) => { const buffer = await getRawBody(req); // 假设拿到了 20MB 的 Buffer const start = Date.now(); worker.postMessage({ buffer, width, height }); worker.once('message', (result) => { console.log('worker done in', Date.now() - start, 'ms'); res.end(result.buffer); }); }); ``` worker 端: ```js // gray-worker.js const { parentPort } = require('worker_threads'); parentPort.on('message', ({ buffer, width, height }) => { const out = Buffer.alloc(buffer.length); for (let i = 0; i { const body = await getRawBody(req); const arrayBuffer = body.buffer.slice(body.byteOffset, body.byteOffset + body.byteLength); worker.postMessage({ arrayBuffer, width, height }, [arrayBuffer]); // 此处 arrayBuffer 已被 detach,不能再访问 }); ``` worker 端接收到的就是同一个底层内存,不需要复制: ```js parentPort.on('message', ({ arrayBuffer, width, height }) => { const buffer = Buffer.from(arrayBuffer); const out = Buffer.alloc(buffer.length); // ... 处理逻辑 const outArray = out.buffer.slice(out.byteOffset, out.byteOffset + out.byteLength); parentPort.postMessage({ arrayBuffer: outArray }, [outArray]); }); ``` 这里有个细节:Buffer 往往是更大 ArrayBuffer 上的一个视图,直接 transfer `buf.buffer` 会把整个底层内存都转走,可能超出预期。用 `slice` 截出精确范围再转移,能避免把无关数据一起送出去。 ## SharedArrayBuffer 适合双向共享 如果主线程和 worker 需要同时读写同一块内存,比如流式处理或者多个 worker 协作,那就用 SharedArrayBuffer。它不涉及所有权转移,双方拿到的是同一块内存,同步靠 Atomics。 ```js const shared = new SharedArrayBuffer(1024 * 1024); const flag = new Int32Array(new SharedArrayBuffer(4)); worker.postMessage({ shared, flag }); ``` worker 里写入完成后用 `Atomics.store(flag, 0, 1)` 通知,主线程用 `Atomics.wait` 或者轮询检查。注意主线程上 `Atomics.wait` 会阻塞,一般放在 worker 里等,主线程侧用 `Atomics.load` 配合 `setImmediate` 轮询更稳妥。SharedArrayBuffer 在浏览器里需要跨源隔离头,Node.js 里没有这个限制,但内存生命周期要自己管,别让 worker 持有引用导致无法回收。 ## 线程池下的消息协议设计 单个 worker 好办,做成池子后问题会放大。常见的错误是每个请求都 `new Worker`,启动开销不说,消息通道也没复用。更合理的做法是维护固定数量的 worker,主线程维护一个任务队列,每个任务带上递增 id,worker 回传时带上同一个 id 做匹配。 ```js class WorkerPool { constructor(size) { this.workers = []; this.pending = new Map(); this.seq = 0; for (let i = 0; i { const resolve = this.pending.get(id); if (resolve) { this.pending.delete(id); resolve(arrayBuffer); } }); this.workers.push(w); } } run(payload, transferList) { return new Promise((resolve) => { const id = this.seq++; this.pending.set(id, resolve); const worker = this.workers[id % this.workers.length]; worker.postMessage({ id, ...payload }, transferList); }); } } ``` worker 端回传时要把 id 原样带回。这样主线程只负责轻量的消息分发,重活都在 worker 里,Buffer 通过 transferList 转移,往返都不产生主线程上的大块复制。 还有一点容易忽略:worker 里如果用了 `Buffer.alloc` 分配输出内存,那部分内存的创建发生在 worker 线程,不会阻塞主线程,这是好事。但如果 worker 回传时用默认 postMessage 而不是转移,复制又回到了主线程。所以输出侧同样要走 transferList。 ## 什么情况下别用 worker 任务本身很小,比如几 KB 的数据做一次 hash,开 worker 的消息往返开销比直接算还大。worker 适合单次计算耗时在几十毫秒以上、且数据量足够大到复制成本明显的场景。另外 worker 之间不共享 JS 堆,每个 worker 有独立的 V8 isolate,内存占用会随线程数线性增长,池子大小要结合机器核数和单任务内存来定,别盲目开几十个。 调试时可以用 `perf_hooks` 的 event loop 延迟监控来确认主线程是否真的被阻塞: ```js const { monitorEventLoopDelay } = require('perf_hooks'); const h = monitorEventLoopDelay({ resolution: 20 }); h.enable(); setInterval(() => { console.log('p99 lag', h.percentile(99) / 1e6, 'ms'); h.reset(); }, 5000); ``` 如果 p99 延迟在请求高峰期明显上升,而 CPU 占用又没有打满,大概率就是主线程在做消息序列化这类同步工作,回头检查 postMessage 有没有带 transferList 基本就能定位到。 推荐查看天气:http://www.52cha.cn/hanzi/5596.html 推荐查看天气:http://www.52cha.cn/hanzi/5597.html 推荐查看天气:http://www.52cha.cn/hanzi/5598.html 推荐查看天气:http://www.52cha.cn/hanzi/5599.html 推荐查看天气:http://www.52cha.cn/hanzi/5600.html 推荐查看天气:http://www.52cha.cn/hanzi/5601.html 推荐查看天气:http://www.52cha.cn/hanzi/5602.html 推荐查看天气:http://www.52cha.cn/hanzi/5603.html 推荐查看天气:http://www.52cha.cn/hanzi/5604.html 推荐查看天气:http://www.52cha.cn/hanzi/5605.html 推荐查看天气:http://www.52cha.cn/hanzi/5606.html 推荐查看天气:http://www.52cha.cn/hanzi/5607.html 推荐查看天气:http://www.52cha.cn/hanzi/5608.html 推荐查看天气:http://www.52cha.cn/hanzi/5609.html 推荐查看天气:http://www.52cha.cn/hanzi/5610.html 推荐查看天气:http://www.52cha.cn/hanzi/5611.html 推荐查看天气:http://www.52cha.cn/hanzi/5612.html 推荐查看天气:http://www.52cha.cn/hanzi/5613.html 推荐查看天气:http://www.52cha.cn/hanzi/5614.html 推荐查看天气:http://www.52cha.cn/hanzi/5615.html 推荐查看天气:http://www.52cha.cn/hanzi/5616.html 推荐查看天气:http://www.52cha.cn/hanzi/5617.html 推荐查看天气:http://www.52cha.cn/hanzi/5618.html 推荐查看天气:http://www.52cha.cn/hanzi/5619.html 推荐查看天气:http://www.52cha.cn/hanzi/5620.html 推荐查看天气:http://www.52cha.cn/hanzi/5621.html 推荐查看天气:http://www.52cha.cn/hanzi/5622.html 推荐查看天气:http://www.52cha.cn/hanzi/5623.html 推荐查看天气:http://www.52cha.cn/hanzi/5624.html 推荐查看天气:http://www.52cha.cn/hanzi/5625.html 推荐查看天气:http://www.52cha.cn/hanzi/5626.html 推荐查看天气:http://www.52cha.cn/hanzi/5627.html 推荐查看天气:http://www.52cha.cn/hanzi/5628.html 推荐查看天气:http://www.52cha.cn/hanzi/5629.html 推荐查看天气:http://www.52cha.cn/hanzi/5630.html 推荐查看天气:http://www.52cha.cn/hanzi/5631.html 推荐查看天气:http://www.52cha.cn/hanzi/5632.html 推荐查看天气:http://www.52cha.cn/hanzi/5633.html 推荐查看天气:http://www.52cha.cn/hanzi/5634.html 推荐查看天气:http://www.52cha.cn/hanzi/5635.html 推荐查看天气:http://www.52cha.cn/hanzi/5636.html 推荐查看天气:http://www.52cha.cn/hanzi/5637.html 推荐查看天气:http://www.52cha.cn/hanzi/5638.html 推荐查看天气:http://www.52cha.cn/hanzi/5639.html 推荐查看天气:http://www.52cha.cn/hanzi/5640.html 推荐查看天气:http://www.52cha.cn/hanzi/5641.html 推荐查看天气:http://www.52cha.cn/hanzi/5642.html 推荐查看天气:http://www.52cha.cn/hanzi/5643.html 推荐查看天气:http://www.52cha.cn/hanzi/5644.html 推荐查看天气:http://www.52cha.cn/hanzi/5645.html 推荐查看天气:http://www.52cha.cn/hanzi/5646.html 推荐查看天气:http://www.52cha.cn/hanzi/5647.html 推荐查看天气:http://www.52cha.cn/hanzi/5648.html 推荐查看天气:http://www.52cha.cn/hanzi/5649.html 推荐查看天气:http://www.52cha.cn/hanzi/5650.html 推荐查看天气:http://www.52cha.cn/hanzi/5651.html 推荐查看天气:http://www.52cha.cn/hanzi/5652.html 推荐查看天气:http://www.52cha.cn/hanzi/5653.html 推荐查看天气:http://www.52cha.cn/hanzi/5654.html 推荐查看天气:http://www.52cha.cn/hanzi/5655.html 推荐查看天气:http://www.52cha.cn/hanzi/5656.html 推荐查看天气:http://www.52cha.cn/hanzi/5657.html 推荐查看天气:http://www.52cha.cn/hanzi/5658.html 推荐查看天气:http://www.52cha.cn/hanzi/5659.html 推荐查看天气:http://www.52cha.cn/hanzi/5660.html 推荐查看天气:http://www.52cha.cn/hanzi/5661.html 推荐查看天气:http://www.52cha.cn/hanzi/5662.html 推荐查看天气:http://www.52cha.cn/hanzi/5663.html 推荐查看天气:http://www.52cha.cn/hanzi/5664.html 推荐查看天气:http://www.52cha.cn/hanzi/5665.html 推荐查看天气:http://www.52cha.cn/hanzi/5666.html 推荐查看天气:http://www.52cha.cn/hanzi/5667.html 推荐查看天气:http://www.52cha.cn/hanzi/5668.html 推荐查看天气:http://www.52cha.cn/hanzi/5669.html 推荐查看天气:http://www.52cha.cn/hanzi/5670.html 推荐查看天气:http://www.52cha.cn/hanzi/5671.html 推荐查看天气:http://www.52cha.cn/hanzi/5672.html 推荐查看天气:http://www.52cha.cn/hanzi/5673.html 推荐查看天气:http://www.52cha.cn/hanzi/5674.html 推荐查看天气:http://www.52cha.cn/hanzi/5675.html 推荐查看天气:http://www.52cha.cn/hanzi/5676.html 推荐查看天气:http://www.52cha.cn/hanzi/5677.html 推荐查看天气:http://www.52cha.cn/hanzi/5678.html 推荐查看天气:http://www.52cha.cn/hanzi/5679.html 推荐查看天气:http://www.52cha.cn/hanzi/5680.html 推荐查看天气:http://www.52cha.cn/hanzi/5681.html 推荐查看天气:http://www.52cha.cn/hanzi/5682.html 推荐查看天气:http://www.52cha.cn/hanzi/5683.html 推荐查看天气:http://www.52cha.cn/hanzi/5684.html 推荐查看天气:http://www.52cha.cn/hanzi/5685.html 推荐查看天气:http://www.52cha.cn/hanzi/5686.html 推荐查看天气:http://www.52cha.cn/hanzi/5687.html 推荐查看天气:http://www.52cha.cn/hanzi/5688.html 推荐查看天气:http://www.52cha.cn/hanzi/5689.html 推荐查看天气:http://www.52cha.cn/hanzi/5690.html 推荐查看天气:http://www.52cha.cn/hanzi/5691.html 推荐查看天气:http://www.52cha.cn/hanzi/5692.html 推荐查看天气:http://www.52cha.cn/hanzi/5693.html 推荐查看天气:http://www.52cha.cn/hanzi/5694.html 推荐查看天气:http://www.52cha.cn/hanzi/5695.html 推荐查看天气:http://www.52cha.cn/hanzi/5696.html 推荐查看天气:http://www.52cha.cn/hanzi/5697.html 推荐查看天气:http://www.52cha.cn/hanzi/5698.html 推荐查看天气:http://www.52cha.cn/hanzi/5699.html 推荐查看天气:http://www.52cha.cn/hanzi/5700.html 推荐查看天气:http://www.52cha.cn/hanzi/5701.html 推荐查看天气:http://www.52cha.cn/hanzi/5702.html 推荐查看天气:http://www.52cha.cn/hanzi/5703.html 推荐查看天气:http://www.52cha.cn/hanzi/5704.html 推荐查看天气:http://www.52cha.cn/hanzi/5705.html 推荐查看天气:http://www.52cha.cn/hanzi/5706.html 推荐查看天气:http://www.52cha.cn/hanzi/5707.html 推荐查看天气:http://www.52cha.cn/hanzi/5708.html 推荐查看天气:http://www.52cha.cn/hanzi/5709.html 推荐查看天气:http://www.52cha.cn/hanzi/5710.html 推荐查看天气:http://www.52cha.cn/hanzi/5711.html 推荐查看天气:http://www.52cha.cn/hanzi/5712.html 推荐查看天气:http://www.52cha.cn/hanzi/5713.html 推荐查看天气:http://www.52cha.cn/hanzi/5714.html 推荐查看天气:http://www.52cha.cn/hanzi/5715.html 推荐查看天气:http://www.52cha.cn/hanzi/5716.html 推荐查看天气:http://www.52cha.cn/hanzi/5717.html 推荐查看天气:http://www.52cha.cn/hanzi/5718.html 推荐查看天气:http://www.52cha.cn/hanzi/5719.html 推荐查看天气:http://www.52cha.cn/hanzi/5720.html 推荐查看天气:http://www.52cha.cn/hanzi/5721.html 推荐查看天气:http://www.52cha.cn/hanzi/5722.html 推荐查看天气:http://www.52cha.cn/hanzi/5723.html 推荐查看天气:http://www.52cha.cn/hanzi/5724.html 推荐查看天气:http://www.52cha.cn/hanzi/5725.html 推荐查看天气:http://www.52cha.cn/hanzi/5726.html 推荐查看天气:http://www.52cha.cn/hanzi/5727.html 推荐查看天气:http://www.52cha.cn/hanzi/5728.html 推荐查看天气:http://www.52cha.cn/hanzi/5729.html 推荐查看天气:http://www.52cha.cn/hanzi/5730.html 推荐查看天气:http://www.52cha.cn/hanzi/5731.html 推荐查看天气:http://www.52cha.cn/hanzi/5732.html 推荐查看天气:http://www.52cha.cn/hanzi/5733.html 推荐查看天气:http://www.52cha.cn/hanzi/5734.html 推荐查看天气:http://www.52cha.cn/hanzi/5735.html 推荐查看天气:http://www.52cha.cn/hanzi/5736.html 推荐查看天气:http://www.52cha.cn/hanzi/5737.html 推荐查看天气:http://www.52cha.cn/hanzi/5738.html 推荐查看天气:http://www.52cha.cn/hanzi/5739.html 推荐查看天气:http://www.52cha.cn/hanzi/5740.html 推荐查看天气:http://www.52cha.cn/hanzi/5741.html 推荐查看天气:http://www.52cha.cn/hanzi/5742.html 推荐查看天气:http://www.52cha.cn/hanzi/5743.html 推荐查看天气:http://www.52cha.cn/hanzi/5744.html 推荐查看天气:http://www.52cha.cn/hanzi/5745.html 推荐查看天气:http://www.52cha.cn/hanzi/5746.html 推荐查看天气:http://www.52cha.cn/hanzi/5747.html 推荐查看天气:http://www.52cha.cn/hanzi/5748.html 推荐查看天气:http://www.52cha.cn/hanzi/5749.html 推荐查看天气:http://www.52cha.cn/hanzi/5750.html 推荐查看天气:http://www.52cha.cn/hanzi/5751.html 推荐查看天气:http://www.52cha.cn/hanzi/5752.html 推荐查看天气:http://www.52cha.cn/hanzi/5753.html 推荐查看天气:http://www.52cha.cn/hanzi/5754.html 推荐查看天气:http://www.52cha.cn/hanzi/5755.html 推荐查看天气:http://www.52cha.cn/hanzi/5756.html 推荐查看天气:http://www.52cha.cn/hanzi/5757.html 推荐查看天气:http://www.52cha.cn/hanzi/5758.html 推荐查看天气:http://www.52cha.cn/hanzi/5759.html 推荐查看天气:http://www.52cha.cn/hanzi/5760.html 推荐查看天气:http://www.52cha.cn/hanzi/5761.html 推荐查看天气:http://www.52cha.cn/hanzi/5762.html 推荐查看天气:http://www.52cha.cn/hanzi/5763.html 推荐查看天气:http://www.52cha.cn/hanzi/5764.html 推荐查看天气:http://www.52cha.cn/hanzi/5765.html 推荐查看天气:http://www.52cha.cn/hanzi/5766.html 推荐查看天气:http://www.52cha.cn/hanzi/5767.html 推荐查看天气:http://www.52cha.cn/hanzi/5768.html 推荐查看天气:http://www.52cha.cn/hanzi/5769.html 推荐查看天气:http://www.52cha.cn/hanzi/5770.html 推荐查看天气:http://www.52cha.cn/hanzi/5771.html 推荐查看天气:http://www.52cha.cn/hanzi/5772.html 推荐查看天气:http://www.52cha.cn/hanzi/5773.html 推荐查看天气:http://www.52cha.cn/hanzi/5774.html 推荐查看天气:http://www.52cha.cn/hanzi/5775.html 推荐查看天气:http://www.52cha.cn/hanzi/5776.html 推荐查看天气:http://www.52cha.cn/hanzi/5777.html 推荐查看天气:http://www.52cha.cn/hanzi/5778.html 推荐查看天气:http://www.52cha.cn/hanzi/5779.html 推荐查看天气:http://www.52cha.cn/hanzi/5780.html 推荐查看天气:http://www.52cha.cn/hanzi/5781.html 推荐查看天气:http://www.52cha.cn/hanzi/5782.html 推荐查看天气:http://www.52cha.cn/hanzi/5783.html 推荐查看天气:http://www.52cha.cn/hanzi/5784.html 推荐查看天气:http://www.52cha.cn/hanzi/5785.html 推荐查看天气:http://www.52cha.cn/hanzi/5786.html 推荐查看天气:http://www.52cha.cn/hanzi/5787.html 推荐查看天气:http://www.52cha.cn/hanzi/5788.html 推荐查看天气:http://www.52cha.cn/hanzi/5789.html 推荐查看天气:http://www.52cha.cn/hanzi/5790.html 推荐查看天气:http://www.52cha.cn/hanzi/5791.html 推荐查看天气:http://www.52cha.cn/hanzi/5792.html 推荐查看天气:http://www.52cha.cn/hanzi/5793.html 推荐查看天气:http://www.52cha.cn/hanzi/5794.html 推荐查看天气:http://www.52cha.cn/hanzi/5795.html

优秀的个人博客,低调大师

测试用例设计方法六脉神剑——第五剑:化气为型,场景用例破云 | 京东物流技术团队

1 引言 前几篇文章主要针对单点功能的测试用例设计方法展开介绍。然而,当拿到一个测试任务时,并非先关注某个功能的细节测试,而是先要使用场景法对主要业务流程和主要功能展开测试,当业务场景没有问题后,再使用等价类、边界值、判定表等方法对细节功能进行测试(先整体后细节)。 2 概念及原理 2.1 场景法概念 场景法是一种通过使用事件触发流程,对系统的功能点或业务流程进行描述的方法。对于同一事件不同的触发顺序和处理结果, 可以形成不同的场景。 在日常工作中,针对同一业务需求可以模拟出不同场景,测试用例中对所有功能点及业务流程的覆盖,有利于测试人员设计测试用例,从而提高测试效果,使测试用例更容易理解和执行。 2.2 场景法设计层面 业务层面:需熟悉需求业务逻辑,并针对当前需求进行发散性思考。 技术层面:需分析出基本流和备选流,通过遍历所有基本流和备选流,可以覆盖完整的业务场景。 基本流:模拟用户正确的业务操作流程 备选流:模拟用户错误的业务操作流程 2.3 场景法原理介绍 1)场景业务流组成 场景法一般包含基本流和备选流两种业务流。 上图中,经过用例的每条路径都可以使用基本流和备选流表示。其中,绿色主线代表基本流,是经过用例的最简单的路径,一个业务场景只存在一条基本流,基本流仅有一个起点和一个终点。 备选流表示流程存在反复,但经过纠正后依然能达到目标的流程。备选流包含假设、异常操作等多种情况。上图中,备选流1和备选流3均始于基本流,在特定条件下执行后,重新加入基本流;备选流2和备选流4均从其余备选流出发,终止场景而不加入基本流中。 2)场景组合 按上图可组合多个不同的场景: 3)备选流覆盖准则 覆盖每个备选流 覆盖一个循环 4)如何识别基本流与备选流? 基本流只有一个起点和一个终点; 基本流是主流,备选流是支流; 备选流可始于基本流,也可始于其他备选流; 备选流可回到基本流,也可是其他备选流的起点。 3 方法步骤 根据需求文档,梳理业务的流程图; 分析主干业务正常执行的流程——基本流; 分析出分支流程——备选流; 组合基本流、备选流,确定基本场景; 对每一个场景生成相应的测试用例; 对每一个测试用例确定测试数据值。 4 举个栗子 以销售提交报价单审批的流程为例: 销售提交多产品组合报价单至OA审批。业务场景如下: 销售提交多产品组合报价—>一级审批通过—>二级审批通过—>结束 1)业务场景梳理 2)确定基本流: 销售提交多产品组合报价至OA审批—>一级审批通过—>二级审批通过—>结束 3)确定备选流 备选流1:销售提交多产品组合报价单至OA审批,销售撤回报价单; 备选流2:一级审批人审批驳回; 备选流3:一级审批人加签至加签人A,A审批通过; 备选流4:一级审批人加签至加签人A,A审批驳回; 备选流5:二级审批人审批驳回; 备选流6:二级审批人加签至加签人B,B审批通过; 备选流7:二级审批人加签至加签人B,B审批驳回。 4)组合基本流、备选流,确定基本场景 5)对每一个场景生成相应的测试用例 本例中,对于每个测试用例,存在一个测试用例ID、基本场景、测试用例中涉及的所有元素以及预期结果。 通过从确定执行用例场景所需的元素入手构建矩阵;然后针对每个场景,确定包含执行场景所需条件的测试用例。在如下矩阵中,V(有效)表明这个条件必须是有效的;I(无效)表明这种条件下将激活所需备选流;N/A表明这个条件不适用于测试用例。 6)对每一个测试用例确定测试数据值 5 总结 场景法 适用场景 业务流程较清晰的软件系统或功能模块 优点 涉及到业务流程的需求可使用场景法 缺点 只能验证业务流程,不能验证单点功能。一般先采用等价类划分、边界值分析、错误推断法、判定表等方法对单点功能进行验证,验证通过后再采用场景法进行业务流程的验证。 注意事项 (1)主题清晰:了解场景用例需测试的功能,确保测试场景在用户使用过程中会出现; (2)逻辑无误:确保基本功能结合起来无bug; (3)步骤简洁:测试用例的步骤描述需简洁明了,无歧义; (4)场景唯一:确保不同用例可覆盖不同的测试点。 通过本文讲解的场景法,可以对业务流程较为复杂的需求进行用例设计。然而,该方法并不能涵盖一个需求的全部测试点,对于细节功能、业务中的非常规操作等,还需要借助其他测试用例设计方法。如何尽可能多地覆盖业务功能,设计出完美的测试方案?下一篇文章将为您展开详细介绍~ 作者:京东物流 李硕 来源:京东物流 自猿其说Tech 转载请注明来源

优秀的个人博客,低调大师

想成长为一名年薪50万+的实战型架构师?必掌握这7大实战技能经验

想成为一名架构师,但是架构师对应的技能,我应该掌握哪些啊?以及掌握的程度是什么样的?如何成为一名真正的实战性架构师? 我简要分为以下7点来谈谈,从技能的角度抛砖引玉,希望你对你架构师之路有一定的参考。 编程基本功:数据结构和算法 如果你一直关注,会发现我之前分享的阿里面试题系列,有许多关于算法内容,比如:HashMap如何扩容,底层的数据结构发生了怎样的转变。 这就是典型的数据结构基本功,这些掌握不好,在实际编程过程中,对于性能效率你是不清楚的。因此,我一直强调要重视算法,并且认为至少应该掌握下图中的这几种常用算法:数据结构相关的哈希表、链表、二叉树等等。 编程基本功:Java编程 这些都算是编程基本功底:多线程、JVM、并发容器、并发类、NIO等。 Web开发 web开发这个领域,如果你要掌握好了,发展也非常不错。 比如:现在的H5、小程序等,对于掌握前端技术的开发人员,竞争力杠杠。 当然作为后端开发人员,多掌握点前端的技术,那也是必须的。 数据库 重点都圈在下图了,这里涉及的技术还是蛮多的,mysql、oracle、nosql相关的mongodb,以及如今大家都熟知的分库分表、读写分离等技术。 除了大家熟知的部分,我补充几个设计相关的内容:数据库设计范式、索引、事务、数据库相关的慢查询、长事务,这些在高并发的情况下,这些也算是数据库技术的编程功底。 研发相关的框架工具 Java程序员对于下图应该都是比较熟悉的,这些框架与工具都是经常使用。 架构设计 谈到设计,就不得不提到项目中的概要设计和详细设计,这就会涉及到UML。 除此之外,整个项目的设计,这个时候考验你的架构设计能力。 这里的架构设计,包含的系列非常多,特别是Redis、应用程序拆分、拆分后涉及到的RPC微服务等技术,以及后端数据库的拆分技术,以及拆分后的分布式集群部署。 分布式会涉及到:分布式锁、全局唯一ID的生成、分布式Session…这里还有蛮多的,其实大部分我都分享过了,基本都在如下的的分布式系列分享专题资料。 从程序员到架构师,是很大的职业转变,也是不少程序员的职场规划路线。以上是我多年从业经验总结,分享给喜欢Java,喜欢编程,有梦想成为架构师的程序员们,希望能够帮助到你们。 我是mike,10年+程序员。曾先后就职于淘宝、盛大、百度、携程 ,历程高级研发工程师、研发经理、架构师、事业部CTO。坚持写文1年多,原创内容接近400篇,目前粉丝总数7万+,主要分享:BAT面试、架构、招聘等技术内容。 欢迎留言或进我的个人群179961551,本群专用于学习交流技术、分享面试机会,拒绝广告,我也会在群内不定期答题、探讨,一起学习、交流、成长~

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册
修改密码
忘记密码