Java教程

多线程插入数据库

本文主要是介绍多线程插入数据库,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

本素材只是为了方便学习,没有任何盈利目的:素材来源https://mp.weixin.qq.com/s/P1dJWtcmGe_KYThDo_8Ltg,如有侵权请联系作者删除

 

@Transactional(rollbackFor = Exception.class)
public int addFreshStudentsNew2(List<FreshStudentAndStudentModel> list, String schoolNo) {
    if (list == null || list.isEmpty()) {
        return 0;
    }
    List<StudentEntity> studentEntityList = new LinkedList<>();
    List<EnrollStudentEntity> enrollStudentEntityList = new LinkedList<>();
    List<AllusersEntity> allusersEntityList = new LinkedList<>();

    for (FreshStudentAndStudentModel freshStudentAndStudentModel : list) {

        EnrollStudentEntity enrollStudentEntity = new EnrollStudentEntity();
        StudentEntity studentEntity = new StudentEntity();
        BeanUtils.copyProperties(freshStudentAndStudentModel, studentEntity);
        BeanUtils.copyProperties(freshStudentAndStudentModel, enrollStudentEntity);
        String operator = TenancyContext.UserID.get();
        String studentId = BaseUuidUtils.base58Uuid();
        enrollStudentEntity.setId(BaseUuidUtils.base58Uuid());
        enrollStudentEntity.setStudentId(studentId);
        enrollStudentEntity.setIdentityCardId(freshStudentAndStudentModel.getIdCard());
        enrollStudentEntity.setOperator(operator);
        studentEntity.setId(studentId);
        studentEntity.setIdentityCardId(freshStudentAndStudentModel.getIdCard());
        studentEntity.setOperator(operator);
        studentEntityList.add(studentEntity);
        enrollStudentEntityList.add(enrollStudentEntity);

        AllusersEntity allusersEntity = new AllusersEntity();
        allusersEntity.setId(enrollStudentEntity.getId());
        allusersEntity.setUserCode(enrollStudentEntity.getNemtCode());
        allusersEntity.setUserName(enrollStudentEntity.getName());
        allusersEntity.setSchoolNo(schoolNo);
        allusersEntity.setTelNum(enrollStudentEntity.getTelNum());
        allusersEntity.setPassword(enrollStudentEntity.getNemtCode());  //密码设置为考生号
        allusersEntityList.add(allusersEntity);
    }

    int c = 100;
    int b = enrollStudentEntityList.size() / c;
    int d = enrollStudentEntityList.size() % c;

    int enResult = 0;
    int stuResult = 0;
    boolean allResult = false;

    for (int e = c; e <= c * b; e = e + c) {
        enResult = enrollStudentDao.insertAll(enrollStudentEntityList.subList(e - c, e));
        stuResult = studentDao.insertAll(studentEntityList.subList(e - c, e));
        allResult = allusersFacade.insertUserList(allusersEntityList.subList(e - c, e));
    }
    if (d != 0) {
        enResult = enrollStudentDao.insertAll(enrollStudentEntityList.subList(c * b, enrollStudentEntityList.size()));
        stuResult = studentDao.insertAll(studentEntityList.subList(c * b, studentEntityList.size()));
        allResult = allusersFacade.insertUserList(allusersEntityList.subList(c * b, allusersEntityList.size()));
    }

    if (enResult > 0 && stuResult > 0 && allResult) {
        return 10;
    }
    return -10;
}
代码

 

这篇关于多线程插入数据库的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!