|
|
@ -27,7 +27,7 @@ public class ExcelListener<T> extends AnalysisEventListener<T> { |
|
|
|
/** |
|
|
|
* excel数据 |
|
|
|
*/ |
|
|
|
private List<Object> dataList; |
|
|
|
private List<T> dataList; |
|
|
|
|
|
|
|
/** |
|
|
|
* 判断重复的属性方法 |
|
|
@ -52,13 +52,14 @@ public class ExcelListener<T> extends AnalysisEventListener<T> { |
|
|
|
/** |
|
|
|
* 构造函数 |
|
|
|
*/ |
|
|
|
public ExcelListener(Class<?> mainClass) { |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
public ExcelListener() { |
|
|
|
dataList = new ArrayList<>(); |
|
|
|
this.mainClass = mainClass; |
|
|
|
this.mainClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void invoke(T object, AnalysisContext analysisContext) { |
|
|
|
public void invoke(Object object, AnalysisContext analysisContext) { |
|
|
|
if(this.methodName == null) { |
|
|
|
String methodName = this.getOnlyOneAnnotation(); |
|
|
|
if(methodName == null) { |
|
|
@ -84,9 +85,9 @@ public class ExcelListener<T> extends AnalysisEventListener<T> { |
|
|
|
/** |
|
|
|
* 判断数据是否存在 |
|
|
|
*/ |
|
|
|
private Object checkDataIsExist(T object) { |
|
|
|
for(Object obj : dataList) { |
|
|
|
Object methodValue1 = ReflectUtil.invoke(obj, "get" + this.methodName); |
|
|
|
private T checkDataIsExist(Object object) { |
|
|
|
for(T obj : dataList) { |
|
|
|
T methodValue1 = ReflectUtil.invoke(obj, "get" + this.methodName); |
|
|
|
Object methodValue2 = ReflectUtil.invoke(object, "get" + this.methodName); |
|
|
|
if(methodValue1.equals(methodValue2)) { |
|
|
|
return obj; |
|
|
@ -99,8 +100,9 @@ public class ExcelListener<T> extends AnalysisEventListener<T> { |
|
|
|
* 添加数据 |
|
|
|
* @param vo |
|
|
|
*/ |
|
|
|
private void buildEntity(T vo) { |
|
|
|
Object mainObject = this.checkDataIsExist(vo); |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
private void buildEntity(Object vo) { |
|
|
|
T mainObject = this.checkDataIsExist(vo); |
|
|
|
Object subObject; |
|
|
|
try {//创建子数据对象
|
|
|
|
subObject = this.subClass.getDeclaredConstructors()[0].newInstance((Object) null); |
|
|
@ -110,7 +112,7 @@ public class ExcelListener<T> extends AnalysisEventListener<T> { |
|
|
|
BeanUtil.copyProperties(vo, subObject); |
|
|
|
if(mainObject == null) {//list中不存在主数据
|
|
|
|
try {//创建主数据对象
|
|
|
|
mainObject = this.mainClass.getDeclaredConstructors()[0].newInstance((Object) null); |
|
|
|
mainObject = (T) this.mainClass.getDeclaredConstructors()[0].newInstance((Object) null); |
|
|
|
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|