5 changed files with 114 additions and 1 deletions
@ -0,0 +1,66 @@ |
|||
package org.dromara.platform.listener; |
|||
|
|||
import com.alibaba.excel.context.AnalysisContext; |
|||
import com.alibaba.excel.read.listener.ReadListener; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.dromara.platform.domain.AgreementInfo; |
|||
import org.dromara.platform.domain.ProjectManager; |
|||
import org.dromara.platform.service.IAgreementInfoService; |
|||
import org.dromara.platform.service.ProjectManagerService; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
/** |
|||
* 自定义监听器读数据 |
|||
* @author gjh |
|||
*/ |
|||
@Slf4j |
|||
public class AgreementInfoListener implements ReadListener<AgreementInfo> { |
|||
|
|||
private List<AgreementInfo> list = new ArrayList<>(); |
|||
/** |
|||
* 自己定义一个缓冲量 |
|||
*/ |
|||
private static final int BATCH_COUNT = 20; |
|||
@Autowired |
|||
private IAgreementInfoService agreementInfoService; |
|||
private final String categoryId; |
|||
|
|||
public AgreementInfoListener(IAgreementInfoService agreementInfoService,String categoryId) { |
|||
this.agreementInfoService = agreementInfoService; |
|||
this.categoryId = categoryId; |
|||
} |
|||
|
|||
/** |
|||
* 每读一行数据都会调用这个方法 |
|||
* |
|||
* @param agreementInfo |
|||
* @param analysisContext |
|||
*/ |
|||
@Override |
|||
public void invoke(AgreementInfo agreementInfo, AnalysisContext analysisContext) { |
|||
agreementInfo.setServiceCategoryId(categoryId); |
|||
// 读取一行数据就添加到集合
|
|||
list.add(agreementInfo); |
|||
// 判断是否到达缓存量了
|
|||
if (list.size() >= BATCH_COUNT){ |
|||
// 操作数据库
|
|||
agreementInfoService.addData(list); |
|||
list = new ArrayList<>(BATCH_COUNT); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 读完整个excel之后再调用这个方法 |
|||
* |
|||
* @param analysisContext |
|||
*/ |
|||
@Override |
|||
public void doAfterAllAnalysed(AnalysisContext analysisContext) { |
|||
if (list.size()>0){ |
|||
agreementInfoService.addData(list); |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue