导入线程池类
@Resource
private ThreadPoolTaskExecutor taskExecutor;
private List<Object> cache = new ArrayList<>();
设置行数大于多少插入数据库
if (cache.size() >= 1000) {
addTaskToExecutors();
}
private void addTaskToExecutors() {
List<Object> addList = new ArrayList<>(cache);
taskExecutor.execute(new BatchSqlHandler(addList, sessionFactory));
cache.clear();
}
异步调用
public class BatchSqlHandler implements Runnable{
private List<Object> list ;
private SessionFactory sessionFactory ;
public BatchSqlHandler(List<Object> list,SessionFactory sessionFactory){
this.list = list;
this.sessionFactory = sessionFactory;
}
@Override
public void run() {
}