我试图用很多测试数据填充我的数据库,所以我编写了一个CommandLineRunner来保存2k个实体。弹簧引导jpa - 生成并保存测试数据
它的工作 - 但需要永久完成(5-10分钟) - 我处理这个错误的方式?
@Component
public class DbSeederTest implements CommandLineRunner {
@Autowired
FirstRepo firstRepo;
@Autowired
SecondRepo secondRepo;
@Autowired
ThirdRepo thirdRepo;
private List<FirstEnt> firstList = new ArrayList<>();
private List<SecondEnt> secondList = new ArrayList<>();
private List<ThirdEnt> thirdList = new ArrayList<>();
private void generateTestData() {
// generate alot of entities, and add them to the Lists
}
@Override
public void run(String... args) throws Exception {
System.out.println("saving ents...");
generateTestData();
try {
firstRepo.save(firstList);
secondRepo.save(secondList);
thirdRepo.save(thirdList);
} catch(Exception e) {
e.printStackTrace();
}
}
}
我不知道为什么它花费了太多的时间,但你可以通过使用线程概念确实减少时间。由于数据已经在'generateTestData();'后面填充,所以现在在不同线程中调用每个repo save方法。 – SachinSarawgi