9 changed files with 135 additions and 3 deletions
@ -0,0 +1,68 @@ |
|||
package org.dromara.platform.listener; |
|||
|
|||
import com.alibaba.excel.context.AnalysisContext; |
|||
import com.alibaba.excel.read.listener.ReadListener; |
|||
import jakarta.annotation.Resource; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.dromara.platform.domain.PerformanceManagement; |
|||
import org.dromara.platform.domain.ProjectManager; |
|||
import org.dromara.platform.mapper.PerformanceManagementMapper; |
|||
import org.dromara.platform.service.IPerformanceManagementService; |
|||
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 PerformanceManagementListener implements ReadListener<PerformanceManagement> { |
|||
|
|||
private List<PerformanceManagement> list = new ArrayList<>(); |
|||
/** |
|||
* 自己定义一个缓冲量 |
|||
*/ |
|||
private static final int BATCH_COUNT = 20; |
|||
@Resource |
|||
private PerformanceManagementMapper performanceManagementMapper; |
|||
|
|||
@Resource |
|||
private IPerformanceManagementService performanceManagementService; |
|||
|
|||
public PerformanceManagementListener(IPerformanceManagementService performanceManagementService) { |
|||
this.performanceManagementService = performanceManagementService; |
|||
} |
|||
|
|||
/** |
|||
* 每读一行数据都会调用这个方法 |
|||
* |
|||
* @param performanceManagement |
|||
* @param analysisContext |
|||
*/ |
|||
@Override |
|||
public void invoke(PerformanceManagement performanceManagement, AnalysisContext analysisContext) { |
|||
// 读取一行数据就添加到集合
|
|||
list.add(performanceManagement); |
|||
// 判断是否到达缓存量了
|
|||
if (list.size() >= BATCH_COUNT){ |
|||
// 操作数据库
|
|||
performanceManagementService.addData(list); |
|||
list = new ArrayList<>(BATCH_COUNT); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 读完整个excel之后再调用这个方法 |
|||
* |
|||
* @param analysisContext |
|||
*/ |
|||
@Override |
|||
public void doAfterAllAnalysed(AnalysisContext analysisContext) { |
|||
if (list.size()>0){ |
|||
performanceManagementService.addData(list); |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue