最近在开发一些通用的excel数据导入的功能,由于涉及到导入的模块很多,所以开发了一个比较通用的excel导入模板类文件。并且使用annotation作为验证数据的配置。
package com.hp.dylan.jv; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface ExcelAnnotation { //excel column name public String importCellName(); //excel cell data weather is required public boolean isRequired(); //excel cell data datalength. public int importDataLen(); //excel cell data type. public String importDataType(); //excel cell mapping index. public int importCellIndex(); //excel data weather eixts in db. public boolean isEixtance(); }
看看需要导入excel的模型类的定义:(AdditionJvImportModel)
package com.hp.dylan.jv; public class AdditionJvImportModel { @ExcelAnnotation( importCellName = "EN1",isRequired=true ,importDataLen=100,importDataType="String",importCellIndex = 1,isEixtance=true) private String sellEntity; @ExcelAnnotation(importCellName = "SE2",isRequired=false ,importDataLen=4,importDataType="String",importCellIndex =2, isEixtance=false) private String sellSe; @ExcelAnnotation(importCellName = "DI3",isRequired=false ,importDataLen=10,importDataType="String",importCellIndex = 3, isEixtance=false) private String sellDi; @ExcelAnnotation(importCellName = "DEPT4",isRequired=false ,importDataLen=8,importDataType="String",importCellIndex = 4, isEixtance=false) private String sellDept; @ExcelAnnotation(importCellName = "SF5",isRequired=false ,importDataLen=10,importDataType="String",importCellIndex = 5, isEixtance=false) private String sellSf; @ExcelAnnotation(importCellName = "PT6",isRequired=false ,importDataLen=4,importDataType="String",importCellIndex = 6, isEixtance=false) private String sellPt; @ExcelAnnotation(importCellName = "PL7",isRequired=false ,importDataLen=10,importDataType="String",importCellIndex = 7, isEixtance=false) private String sellPl; @ExcelAnnotation(importCellName = "SL8",isRequired=false ,importDataLen=4,importDataType="String",importCellIndex = 8, isEixtance=false) private String sellSl; @ExcelAnnotation(importCellName = "Amount9",isRequired=false ,importDataLen=25,importDataType="number(25,10)",importCellIndex = 9, isEixtance=false) private String sellAmout; @ExcelAnnotation(importCellName = "EN10",isRequired=true ,importDataLen=100,importDataType="String",importCellIndex = 10, isEixtance=true) private String buyEntity; @ExcelAnnotation(importCellName = "SE11",isRequired=false ,importDataLen=4,importDataType="String",importCellIndex = 11, isEixtance=false) private String buySe; @ExcelAnnotation(importCellName = "DI12",isRequired=false ,importDataLen=10,importDataType="String",importCellIndex = 12, isEixtance=false) private String buyDi; @ExcelAnnotation(importCellName = "DEPT13",isRequired=false ,importDataLen=8,importDataType="String",importCellIndex = 13, isEixtance=false) private String buyDept; @ExcelAnnotation(importCellName = "SF14",isRequired=false ,importDataLen=10,importDataType="String",importCellIndex = 14, isEixtance=false) private String buySf; @ExcelAnnotation(importCellName = "PT15",isRequired=false ,importDataLen=4,importDataType="String",importCellIndex = 15, isEixtance=false) private String buyPt; @ExcelAnnotation(importCellName = "PL16",isRequired=false ,importDataLen=10,importDataType="String",importCellIndex = 16, isEixtance=false) private String buyPl; @ExcelAnnotation(importCellName = "SL17",isRequired=false ,importDataLen=4,importDataType="String",importCellIndex = 17, isEixtance=false) private String buySl; @ExcelAnnotation(importCellName = "LSA Factor19",isRequired=false ,importDataLen=25,importDataType="number(25,10)",importCellIndex = 19, isEixtance=false) private String tranFactor; @ExcelAnnotation(importCellName = "GrossICPurchase20",isRequired=false ,importDataLen=25,importDataType="number(25,10)",importCellIndex = 20, isEixtance=false) private String tranGross; @ExcelAnnotation(importCellName = "EX Code21",isRequired=false ,importDataLen=5,importDataType="String",importCellIndex = 21, isEixtance=false) private String tranExCode; @ExcelAnnotation(importCellName = "Transaction Description24",isRequired=false ,importDataLen=200,importDataType="String",importCellIndex = 24, isEixtance=false) private String tranDesr; @ExcelAnnotation(importCellName = "Line Comments25",isRequired=false ,importDataLen=200,importDataType="String",importCellIndex = 25, isEixtance=false) private String tranLineComm; @ExcelAnnotation(importCellName = "Additional Line Comments30",isRequired=false ,importDataLen=200,importDataType="String",importCellIndex = 30, isEixtance=false) private String tranAddLinComm; @ExcelAnnotation(importCellName = "FROM VATID31",isRequired=false ,importDataLen=6,importDataType="String",importCellIndex = 31, isEixtance=false) private String tranFVatId; @ExcelAnnotation(importCellName = "TO VATID32",isRequired=false ,importDataLen=6,importDataType="String",importCellIndex = 32, isEixtance=false) private String tranTVatId; }
excel模型的解析中如何获取到此模型的描述配置。也就是获取他的annotation。
Map<Integer,ExcelAnnotation> mapping=new HashMap<Integer,ExcelAnnotation>(); for (int i = 0; i < filed.length; i++) { Field f = filed[i]; ExcelAnnotation excel = f.getAnnotation(ExcelAnnotation.class); if (excel != null) { mapping.put(excel.importCellIndex(), excel); } }
如何根据annotation的描述来验证。
if(anno.isRequired()){ if(cellValue==null||"".equals(cellValue.trim())){ sb.append(anno.importCellName()+" can not be empty!"); 。。。} }
如果需要跟db交互来进行检验的话,只需要定义一个abstract校验方法,具体的校验规则交给其实现者来完成
public abstract String validateExist(String value);
Noted:
你可以annotation完全的看做一种配置,你只需要获取你的model种的annotation标识,然后根据标识来进行一个校验规则的解析
你也可以结合 class reflection 机制来使用annotation,获取到model的annotation描述之后,根据class reflection 来动态的激活调用一些model中的方法,