一、定义DBExec
public abstract class DBExec {
public abstract void operate(List list);
二、定义BatchDBService
public interface BatchDBService {
void exec(int batchSize, List list, DBExec dbExec);
三、实现BatchDBService
public class BatchDBImpl implements BatchDBService {
public void exec(int batchSize, List list, DBExec dbExec) {
if (list != null && list.size() > 0) {
int cycelTotal = total / init;
for (int j = 0; j < cycelTotal; j++) {
List batchInsertList = new ArrayList<>();
for (int i = j * init; i < (j + 1) * init && i < list.size(); i++) {
batchInsertList.add(list.get(i));
dbExec.operate(batchInsertList);
四、使用
BatchDBService batchDBService = new BatchDBImpl();
DBExec saveDBExec = new DBExec() {
public void operate(List list) {
testDao.batchInsert(list);
batchDBService.exec(2000, testList, saveDBExec);