java基于xml配置的通用excel单表数据导入组件(二、xml配置文件解析加载)

时间:2023-11-22 11:53:44

1、BN_ImportExcel.java 对应xml主节点属性

package XXXXX.manage.importexcel;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map; public class BN_ImportExcel implements Serializable { /**
*
*/
private static final long serialVersionUID = -3837515724164300087L; private String name;
private String initSQL;
private String beforeSaveSQL;
private String afterSaveSQL;
private Map<String, BN_Column> excelColumn;
private Map<String, BN_Column> systemColumn; public void addExcelColumn(String key, BN_Column column) {
if (excelColumn == null) {
excelColumn = new HashMap<String, BN_Column>();
}
excelColumn.put(key, column);
} public void addSystemColumn(String key, BN_Column column) {
if (systemColumn == null) {
systemColumn = new HashMap<String, BN_Column>();
}
systemColumn.put(key, column);
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} public String getInitSQL() {
return initSQL;
} public void setInitSQL(String initSQL) {
this.initSQL = initSQL;
} public String getBeforeSaveSQL() {
return beforeSaveSQL;
} public void setBeforeSaveSQL(String beforeSaveSQL) {
this.beforeSaveSQL = beforeSaveSQL;
} public String getAfterSaveSQL() {
return afterSaveSQL;
} public void setAfterSaveSQL(String afterSaveSQL) {
this.afterSaveSQL = afterSaveSQL;
} public Map<String, BN_Column> getExcelColumn() {
return excelColumn;
}
public void setExcelColumn(Map<String, BN_Column> excelColumn) {
this.excelColumn = excelColumn;
}
public Map<String, BN_Column> getSystemColumn() {
return systemColumn;
}
public void setSystemColumn(Map<String, BN_Column> systemColumn) {
this.systemColumn = systemColumn;
} public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("{")
.append("name:'").append(name).append("',")
.append("initSQL:'").append(initSQL).append("',")
.append("beforeSaveSQL:'").append(beforeSaveSQL).append("',")
.append("afterSaveSQL:'").append(afterSaveSQL).append("',")
.append("excelColumn:").append(excelColumn).append(",")
.append("systemColumn:").append(systemColumn).append("'}");
return sb.toString();
} }

  2.BN_Column.java 对应详细字段属性配置

package XXXXXXXX.manage.importexcel;

import java.io.Serializable;

public class BN_Column implements Serializable {

	/**
*
*/
private static final long serialVersionUID = 3549142775670034730L; private String column;
private String dataType;
private int length;
private boolean required;
private boolean transfer;
private String className;
private String method;
private String param; public String getColumn() {
return column;
}
public void setColumn(String column) {
this.column = column;
}
public String getDataType() {
return dataType;
}
public void setDataType(String dataType) {
this.dataType = dataType;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public boolean isRequired() {
return required;
}
public void setRequired(boolean required) {
this.required = required;
}
public boolean isTransfer() {
return transfer;
}
public void setTransfer(boolean transfer) {
this.transfer = transfer;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public String getParam() {
return param;
}
public void setParam(String param) {
this.param = param;
} public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("{")
.append("column:'").append(column).append("',")
.append("dataType:'").append(dataType).append("',")
.append("length:").append(length).append(",")
.append("required:").append(required).append(",")
.append("transfer:").append(transfer).append(",")
.append("className:'").append(className).append("',")
.append("method:'").append(method).append("',")
.append("param:'").append(param).append("'}");
return sb.toString();
} }

  3.解析xml为BN_ImportExcel对象

public class DAO_ImportExcel {
private BN_ImportExcel xmlBean;
private Map<String, BN_Column> excelColumn; // 列名称、字段定义映射
private Map<String, BN_Column> systemColumn;// 系统内置字段定义 private void loadXmlBean() {
BeanReader beanReader = new BeanReader();
// 配置BeanReader实例
beanReader.getXMLIntrospector().getConfiguration()
.setAttributesForPrimitives(false);
beanReader.getBindingConfiguration().setMapIDs(false); // 不自动生成ID
// 注册要转换对象的类,并指定根节点名称
BufferedReader br = null;
try {
beanReader.registerBeanClass("importExcel", BN_ImportExcel.class);
br = new BufferedReader(new InputStreamReader(
new FileInputStream(new File(
PathUtil.getPath("/WEB-INF/excel/" + tableImport + ".xml"))), "GBK"));
// 将XML解析Java Object
xmlBean = (BN_ImportExcel) beanReader.parse(br);
excelColumn = xmlBean.getExcelColumn();
systemColumn = xmlBean.getSystemColumn();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IntrospectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (br != null){
try {
br.close();
} catch (IOException e) {
}
}
}
}
}