sharding JDBC 不支持批量导入解决方法
package com.ydmes.service.impl.log;
import com.ydmes.domain.entity.log.BarTraceBackLog;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import java.util.List;
@Component
public class BarTraceBackLogBatchServiceImpl implements ApplicationContextAware {
private static ApplicationContext applicationContext;
public void batchInsertBarTraceBackLogs(List<BarTraceBackLog> barTraceBackLogs) {
DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
definition.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
DataSourceTransactionManager transactionManager = (DataSourceTransactionManager)
applicationContext.getBean("shardingTransactitonManager");
TransactionStatus transactionStatus = transactionManager.getTransaction(definition);
for (BarTraceBackLog barTraceBackLog : barTraceBackLogs) {
//UserDao有create方法,单条插入
applicationContext.getBean(BarTraceBackLogServiceImpl.class).insertSelective(barTraceBackLog);
}
transactionManager.commit(transactionStatus);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}