1.当一次插入过多的数据时,数据库会报错.可以通过java程序实现分批插入mysql
@Override
public void initVisitorPersonPermission() {
List<Person> personList = personService.lambdaQuery().eq(Person::getDeleted, 0).lt(Person::getPersonType, 3).list();
Long userId = LoginUtil.getUserId();
LocalDateTime now = LocalDateTime.now();
int size = personList.size();
int count = 10000;
if(size > count){
int part = (size - 1) / count + 1;
for (int i = 0; i < part; i++) {
List<Person> myList;
if(i == (part -1)){
myList = personList.subList(count * i, size);
}else{
myList = personList.subList(count * i, count * (i + 1));
}
savePersonPermission(myList, userId, now);
}
}else{
savePersonPermission(personList, userId, now);
}
}
2.批量插入代码
private void savePersonPermission(List<Person> personList, Long userId, LocalDateTime now){
ArrayList<VisitorPersonPermission> visitorPersonPermissions = new ArrayList<>();
VisitorPersonPermission visitorPersonPermission;
for (Person person : personList) {
visitorPersonPermission = new VisitorPersonPermission();
visitorPersonPermission.setCommunityId(person.getCommunityId());
visitorPersonPermission.setPersonId(person.getId());
visitorPersonPermission.setCreateBy(userId);
visitorPersonPermission.setCreateTime(now);
visitorPersonPermissions.add(visitorPersonPermission);
}
baseMapper.batchSave(visitorPersonPermissions);
}