Skip to content

Commit 21e6780

Browse files
committed
版本号修正,dependence修正,excel导入入口类修正,
excel读取为Object导致接口变化后实现的修正,验证失败通过field获取excel表头名称修正, 导入一对一数据的实现, 代码修正,注释修正
1 parent 97e8c29 commit 21e6780

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1873
-466
lines changed

pom.xml

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,25 @@
2727
</build>
2828

2929
<dependencies>
30+
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
31+
<dependency>
32+
<groupId>org.slf4j</groupId>
33+
<artifactId>slf4j-api</artifactId>
34+
<version>1.7.25</version>
35+
</dependency>
3036
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
3137
<dependency>
3238
<groupId>org.slf4j</groupId>
3339
<artifactId>slf4j-log4j12</artifactId>
3440
<version>1.7.25</version>
3541
<!--<scope>test</scope>-->
3642
</dependency>
43+
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
44+
<dependency>
45+
<groupId>log4j</groupId>
46+
<artifactId>log4j</artifactId>
47+
<version>1.2.17</version>
48+
</dependency>
3749

3850
<!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
3951
<dependency>
@@ -67,7 +79,6 @@
6779
<version>3.6</version>
6880
</dependency>
6981

70-
7182
<dependency>
7283
<groupId>junit</groupId>
7384
<artifactId>junit</artifactId>
@@ -78,19 +89,19 @@
7889
<dependency>
7990
<groupId>online.dinghuiye</groupId>
8091
<artifactId>excelutil-api</artifactId>
81-
<version>1.0.0</version>
92+
<version>2.0.0</version>
8293
</dependency>
8394

8495
<dependency>
8596
<groupId>online.dinghuiye</groupId>
8697
<artifactId>poi-kit</artifactId>
87-
<version>1.0.0</version>
98+
<version>1.0.1</version>
8899
</dependency>
89100

90101
<dependency>
91102
<groupId>online.dinghuiye</groupId>
92103
<artifactId>persistence-hibernate-impl</artifactId>
93-
<version>1.0.0</version>
104+
<version>2.0.0</version>
94105
<scope>test</scope>
95106
</dependency>
96107

@@ -103,6 +114,14 @@
103114
<scope>test</scope>
104115
</dependency>
105116

117+
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
118+
<dependency>
119+
<groupId>com.alibaba</groupId>
120+
<artifactId>druid</artifactId>
121+
<version>1.0.31</version>
122+
<scope>test</scope>
123+
</dependency>
124+
106125
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
107126
<dependency>
108127
<groupId>org.hibernate</groupId>

src/main/java/online/dinghuiye/core/ImportHandler.java

Lines changed: 106 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import online.dinghuiye.api.AbstractExcel;
44
import online.dinghuiye.api.entity.RowRecord;
5+
import online.dinghuiye.api.entity.TransactionMode;
6+
import online.dinghuiye.api.persistence.RowRecordPerPersistentRepairer;
57
import online.dinghuiye.api.persistence.RowRecordPersistencor;
68
import online.dinghuiye.api.resolution.torowrecord.RowRecordHandler;
79
import online.dinghuiye.api.validation.RowRecordValidator;
@@ -14,45 +16,133 @@
1416
import java.util.Observer;
1517

1618
/**
17-
* Created by Strangeen on 2017/8/6.
19+
* <p>excel导入入口类</p>
1820
*
19-
* 导入handler
20-
* 先实现持久操作对象传入,后期更改为SpringBean
21+
* @author Strangeen
22+
* on 2017/8/6
2123
*/
2224
public class ImportHandler {
2325

24-
private RowRecordHandler convertorAndHandler;
26+
private RowRecordHandler handler;
2527
private RowRecordValidator validator;
2628
private RowRecordPersistencor persistencor;
29+
private TransactionMode mode;
30+
private RowRecordPerPersistentRepairer repairer;
2731

28-
public ImportHandler(RowRecordPersistencor persistencor) {
29-
this.convertorAndHandler = new RowRecordHandlerSinglePojoImpl();
32+
public void setHandler(RowRecordHandler handler) { this.handler = handler; }
33+
34+
public void setValidator(RowRecordValidator validator) {
35+
this.validator = validator;
36+
}
37+
38+
public void setPersistencor(RowRecordPersistencor persistencor) {
39+
this.persistencor = persistencor;
40+
}
41+
42+
public void setMode(TransactionMode mode) {
43+
this.mode = mode;
44+
}
45+
46+
public void setRepairer(RowRecordPerPersistentRepairer repairer) {
47+
this.repairer = repairer;
48+
}
49+
50+
51+
public ImportHandler() {}
52+
53+
/**
54+
*
55+
* @param handler excel解析实现<br>
56+
* 实现:
57+
* {@link RowRecordHandlerSinglePojoImpl},
58+
* {@link RowRecordHandler}
59+
* @param validator pojo对象验证实现<br>
60+
* 实现:{@link RowRecordValidatorImpl}
61+
* @param persistencor 持久化实现<br>
62+
* 实现:配套包persistence-hibernate-impl中RowRecordPersistencorHibernateImpl类
63+
* @param mode 事务形式,参见{@link TransactionMode}
64+
* @param repairer 持久化前修正List&lt;RowRecord&gt;数据实现
65+
*/
66+
public ImportHandler(RowRecordHandler handler, RowRecordValidator validator, RowRecordPersistencor persistencor, TransactionMode mode, RowRecordPerPersistentRepairer repairer) {
67+
this.handler = handler;
68+
this.validator = validator;
69+
this.persistencor = persistencor;
70+
this.mode = mode;
71+
this.repairer = repairer;
72+
}
73+
74+
/**
75+
* <p>{@link RowRecordHandler}默认使用{@link RowRecordHandlerSinglePojoImpl}</p>
76+
* <p>{@link RowRecordValidator}默认使用{@link RowRecordValidatorImpl}</p>
77+
*
78+
* @param persistencor 持久化实现<br>
79+
* 备选参见:配套包persistence-hibernate-impl中RowRecordPersistencorHibernateImpl类
80+
* @param mode 事务形式,参见{@link TransactionMode}
81+
*/
82+
public ImportHandler(RowRecordPersistencor persistencor, TransactionMode mode) {
83+
this.handler = new RowRecordHandlerSinglePojoImpl();
3084
this.validator = new RowRecordValidatorImpl();
3185
this.persistencor = persistencor;
86+
this.mode = mode;
3287
}
3388

3489
/**
35-
* 读取第一张sheet中的数据
90+
* <p>{@link RowRecordValidator}默认使用{@link RowRecordValidatorImpl}</p>
91+
*
92+
* @param handler excel解析实现<br>
93+
* 实现:
94+
* {@link RowRecordHandlerSinglePojoImpl},
95+
* {@link RowRecordHandler}
96+
* @param persistencor 持久化实现<br>
97+
* 实现:配套包persistence-hibernate-impl中RowRecordPersistencorHibernateImpl类
98+
* @param mode 事务形式,参见{@link TransactionMode}
99+
*/
100+
public ImportHandler(RowRecordHandler handler, RowRecordPersistencor persistencor, TransactionMode mode) {
101+
this.handler = handler;
102+
this.validator = new RowRecordValidatorImpl();
103+
this.persistencor = persistencor;
104+
this.mode = mode;
105+
}
106+
107+
/**
108+
* <p>导入excel方法,默认读取第一张sheet中的数据</p>
109+
*
36110
* @param excel 需要导入的excel对象
37111
* @param processObserver 进度处理器
38112
* @param pojos 导入对象的pojo类
39113
* @return RowRecord的list,通过getResult可以获取导入结果
40114
*/
41115
public List<RowRecord> importExcel(AbstractExcel excel, Observer processObserver, Class<?>... pojos) {
42116

43-
List<Map<String, String>> excelDataList = excel.readExcel(0);
117+
return importExcel(excel, 0, processObserver, pojos);
118+
}
119+
44120

45-
List<RowRecord> rowRecordList = new ArrayList<RowRecord>();
121+
/**
122+
* <p>导入excel方法</p>
123+
*
124+
* @param excel 需要导入的excel对象
125+
* @param sheetNo excel中sheet的编号
126+
* @param processObserver 进度处理器
127+
* @param pojos 导入对象的pojo类
128+
* @return RowRecord的list,通过getResult可以获取导入结果
129+
*/
130+
public List<RowRecord> importExcel(AbstractExcel excel, int sheetNo, Observer processObserver, Class<?>... pojos) {
131+
132+
List<Map<String, Object>> excelDataList = excel.readExcel(sheetNo);
133+
134+
List<RowRecord> rowRecordList = new ArrayList<>();
46135
for (int i = 0; i < excelDataList.size(); i ++) {
47-
Map<String, String> excelData = excelDataList.get(i);
48-
rowRecordList.add(convertorAndHandler.rowRecordCreate(excelData, i + 2));
136+
Map<String, Object> excelData = excelDataList.get(i);
137+
rowRecordList.add(handler.rowRecordCreate(excelData, i + 2));
49138
}
50139

51-
convertorAndHandler.handle(rowRecordList, pojos);
52-
validator.valid(rowRecordList);
53-
persistencor.persist(rowRecordList, processObserver);
54-
55-
// 返回rowRecord信息
140+
if (!handler.handle(rowRecordList, pojos) && mode == TransactionMode.MULTIPLE)
141+
return rowRecordList;
142+
if (!validator.valid(rowRecordList) && mode == TransactionMode.MULTIPLE)
143+
return rowRecordList;
144+
if (repairer != null) repairer.repaire(rowRecordList); // 持久化前修正rowRecord,如:密码md5加密转换
145+
persistencor.persist(rowRecordList, mode, processObserver);
56146
return rowRecordList;
57147
}
58148
}

src/main/java/online/dinghuiye/core/annotation/convert/BlankToNull.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package online.dinghuiye.core.annotation.convert;
22

3+
import online.dinghuiye.api.annotation.convert.Convert;
34
import online.dinghuiye.core.resolution.convert.BlankToNullConvertor;
45

56
import java.lang.annotation.ElementType;
@@ -8,7 +9,10 @@
89
import java.lang.annotation.Target;
910

1011
/**
11-
* Created by Strangeen on 2017/7/1.
12+
* 空串转null
13+
*
14+
* @author Strangeen
15+
* on 2017/7/1
1216
*/
1317
@Retention(RetentionPolicy.RUNTIME)
1418
@Target(ElementType.FIELD)

src/main/java/online/dinghuiye/core/annotation/convert/ConstValue.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package online.dinghuiye.core.annotation.convert;
22

3+
import online.dinghuiye.api.annotation.convert.Convert;
34
import online.dinghuiye.core.resolution.convert.ConstValueConvertor;
45

56
import java.lang.annotation.ElementType;
@@ -8,10 +9,11 @@
89
import java.lang.annotation.Target;
910

1011
/**
11-
* Created by Strangeen on 2017/8/3.
12-
*
13-
* POJO属性为常量值的转换注解标注
12+
* 常量值转换
1413
* 比如:用户的启用和停用,导入数据时并没有传入值,需要手动设置常量值
14+
*
15+
* @author Strangeen
16+
* on 2017/8/3
1517
*/
1618
@Retention(RetentionPolicy.RUNTIME)
1719
@Target(ElementType.FIELD)

src/main/java/online/dinghuiye/core/annotation/convert/Convert.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/main/java/online/dinghuiye/core/annotation/convert/DateFormat.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package online.dinghuiye.core.annotation.convert;
22

3+
import online.dinghuiye.api.annotation.convert.Convert;
34
import online.dinghuiye.core.resolution.convert.DateFormatConvertor;
45

56
import java.lang.annotation.ElementType;
@@ -8,9 +9,11 @@
89
import java.lang.annotation.Target;
910

1011
/**
11-
* Created by Strangeen on 2017/6/27.
12+
* java.util.Date类型转换
13+
* 当excel传入Date属性值类型为String时,需要使用注解转换
1214
*
13-
* POJO属性为java.util.Date类型的转换注解标注
15+
* @author Strangeen
16+
* on 2017/6/27
1417
*/
1518
@Retention(RetentionPolicy.RUNTIME)
1619
@Target(ElementType.FIELD)

src/main/java/online/dinghuiye/core/annotation/convert/ValueConvert.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package online.dinghuiye.core.annotation.convert;
22

3+
import online.dinghuiye.api.annotation.convert.Convert;
34
import online.dinghuiye.api.resolution.convert.Convertor;
45
import online.dinghuiye.core.resolution.convert.ValueConvertConvertor;
56

@@ -9,7 +10,10 @@
910
import java.lang.annotation.Target;
1011

1112
/**
12-
* Created by Strangeen on 2017/6/27.
13+
* 自定义转换
14+
*
15+
* @author Strangeen
16+
* on 2017/6/27
1317
*/
1418
@Retention(RetentionPolicy.RUNTIME)
1519
@Target(ElementType.FIELD)
@@ -18,6 +22,7 @@
1822

1923
/**
2024
* 按照数组顺序执行Convertor
25+
*
2126
* @return Convertor数组
2227
*/
2328
Class<? extends Convertor>[] value();

src/main/java/online/dinghuiye/core/annotation/convert/ValueMap.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package online.dinghuiye.core.annotation.convert;
22

3-
import online.dinghuiye.core.consts.Consts;
3+
import online.dinghuiye.api.annotation.convert.Convert;
44
import online.dinghuiye.core.resolution.convert.ValueMapConvertor;
55

66
import java.lang.annotation.ElementType;
@@ -9,24 +9,26 @@
99
import java.lang.annotation.Target;
1010

1111
/**
12-
* Created by Strangeen on 2017/6/27.<br>
13-
* <br>
14-
* 按json将值对应的key转换为value<br>
15-
* 注解用于少量数据形式转换,否则使用{@link ValueConvert}
12+
* <p>按json将值对应的key转换为value</p>
13+
* <p>注解用于少量数据形式转换</p>
14+
*
15+
* @author Strangeen
16+
* on 2017/6/27
1617
*/
1718
@Retention(RetentionPolicy.RUNTIME)
1819
@Target(ElementType.FIELD)
1920
@Convert(ValueMapConvertor.class)
2021
public @interface ValueMap {
2122

2223
/**
23-
* @return json格式的转换mapping<br>
24-
* 如:excel中“性别”字段值为“男”和“女”,注解value="{'男':1, '女':0}"时,
25-
* 将在解析为POJO过程中将“男”置为1,“女”置为0<br>
26-
* 注意:json解析利用fastjson完成,格式可以为:<br>
24+
* @return
25+
* <p>json格式的转换mapping</p>
26+
* <p>如:excel中“性别”字段值为“男”和“女”,注解value="{'男':1, '女':0}"时,
27+
* 将在解析为POJO过程中将“男”置为1,“女”置为0</p>
28+
* <p>注意:json解析利用fastjson完成,格式可以为:<br>
2729
* 1. value="{'男':1, '女':'female'}" (推荐)<br>
2830
* 2. value="{男:1, 女:'female'}" (推荐)<br>
29-
* 3. value="{\"男\":1, \"女\":\"female\"}" <br>
31+
* 3. value="{\"男\":1, \"女\":\"female\"}" <br></p>
3032
*
3133
*/
3234
String value();

0 commit comments

Comments
 (0)