@Override public void savePatientAndSeekMedical(){ long start = System.currentTimeMillis(); // ((PatientService) AopContext.currentProxy()).saveBatch(patients, 1000); // saveBatch(patients, 1000); SpringUtil.getBean(this.getClass()).saveBatch(patients, 1000); // patientMapper.saveBatchBySql(patients); // seekMedicalService.saveBatch(seekMedicals, 1000); log.warn("保存" + patients.size() + "條患者數據,用時:" + (System.currentTimeMillis() - start) + "ms"); patients.clear(); seekMedicals.clear(); }
這段代碼里面使用了三種方式保存患者數據
第一種 ((PatientService) AopContext.currentProxy()).saveBatch(patients, 1000); 方式在這里會拋出Cannot find current proxy: Set 'exposeProxy' property on Advised to 'true' to make it available異常,猜測原因是saveBatch是mybatisPlus的方法,普通情況如果需要在在該方法上做切面可以用該方式
第二種 saveBatch(patients, 1000); 會導致性能巨慢 導入2w調數據需要900秒左右
第三種 SpringUtil.getBean(this.getClass()).saveBatch(patients, 1000); 可以保證事務並且性能非常好(2w條數據大概15秒左右)
貼上SpringUtil代碼
package cn.rivamed.fhvc.util; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; @Component public class SpringUtil implements ApplicationContextAware { private static ApplicationContext applicationContext = null; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringUtil.applicationContext = applicationContext; } public static <T> T getBean(Class<T> cla) { return applicationContext.getBean(cla); } public static <T> T getBean(String name, Class<T> cal) { return applicationContext.getBean(name, cal); } public static Object getBean(String name){ return applicationContext.getBean(name); } public static String getProperty(String key) { return applicationContext.getBean(Environment.class).getProperty(key); } }