【Java项目】添加教职工

本文介绍了一个教职工管理系统的实现过程,重点讲解了如何通过一系列复杂的数据库操作完成教职工信息的添加功能,并解决了分布式事务等难题。

简介

    项目中教职工管理的添加功能,由于是前辈留下的,总共用了5张表,前前后后这段代码改了n次,最后终于实现了,特此纪念一下。

代码

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertTeacher(String schoolNo, StaffModel staffModel) {

        //01、判断职工是否存在
        if (!this.isExist(staffModel)) {
            logger.error("Service-职工已经存在");
            return false;
        }

        //构造机构实体
        StaffEntity staffEntity = getStaffEntity(staffModel);

        staffEntity.setId(UuidUtils.base58Uuid());//添加id
        staffEntity.setCreateTime(new Date());//添加时间
        staffEntity.setUpdateTime(new Date());
        staffEntity.setIsDelete(0);
        //02、更新staff表
        int flag1 = staffDao.insert(staffEntity);

        //03、更新教职工与机构信息。
        int flag2 = insertInstitutions(staffModel.getInstitutionId(), staffEntity.getId());

        //添加角色
        List<UserRoleModel> roleList = new ArrayList<>();
        UserRoleModel userRoleModel = new UserRoleModel();

        userRoleModel.setId(staffEntity.getId());

        List<String> roles = new ArrayList<>();
        roles.add(staffModel.getRoleId());
        userRoleModel.setRoleId(roles);

        //userRoleModel的属性从UserEntity中获取
        UserEntity userEntity = new UserEntity();
        userEntity.setId(staffEntity.getId());
        userEntity.setPassword(PasswordUtil.encryptPassword(staffModel.getCode()));	//插入用户表的密码加密设置
        userEntity.setEmail(staffModel.getEmail());
        userEntity.setTelNum(staffModel.getTelNum());
        userEntity.setUserCode(staffModel.getCode());
        userEntity.setUserRealName(staffModel.getName());
        userEntity.setSchoolNo(schoolNo);
        userEntity.setCreateTime(new Date());
        userEntity.setUpdateTime(new Date());
        userRoleModel.setUserEntity(userEntity);

        roleList.add(userRoleModel);

        // 04、在user表添加user
        int flag3 = userManagementFacade.insertUser(roleList);


        if (flag1 > 0 && flag2 > 0 && flag3 > 0) {
            return true;
        } else {
            return false;
        }

    }


 /**
     * 判断教职工是否已经存在
     *
     * @param staffModel
     * @return 存在返回false, 不存在返回true;
     */
    private boolean isExist(StaffModel staffModel) {
        StaffExample staffExample = new StaffExample();
        StaffCriteria staffCriteria = staffExample.createCriteria();
        staffCriteria.andCodeEqualTo(staffModel.getCode());			//判断教职工号是否相同
        staffCriteria.andIdentityCardIdEqualTo(staffModel.getIdentityCardId()); //判断身份证是否相同
        staffCriteria.andIsDeleteEqualTo((byte) 0);
        List<StaffEntity> list = this.selectByExample(staffExample);

        return list == null || list.isEmpty();
    }


//插入教职工-机构表,关系为1:n

private int insertInstitutions(String institutionIds, String staffId) {
        String[] ids = institutionIds.split(",");

        int i = 0;
        for (String id : ids) {
            StaffInstitutionEntity s = new StaffInstitutionEntity();
            s.setInstitutionId(id);
            s.setStaffId(staffId);
            s.setId(UuidUtils.base58Uuid());
            s.setCreateTime(new Date());
            s.setUpdateTime(new Date());
            s.setIsDelete(0);
            int flag = staffInstitutionDao.insert(s);
            if (flag == 0) {
                return 0;
            } else {
                i = i + 1;
            }
        }
        return i;
    }

//Model转换为entity实体

private StaffEntity getStaffEntity(StaffModel m) {

        StaffEntity staffEntity = new StaffEntity();
        staffEntity.setId(m.getId());
        staffEntity.setName(m.getName());
        staffEntity.setCode(m.getCode());
        staffEntity.setAccountAddress(m.getAccountAddress());
        staffEntity.setAchivement(m.getAchivement());
        staffEntity.setBrief(m.getBrief());
        staffEntity.setCheckOrNot(m.getCheckOrNot());
        staffEntity.setDegree(m.getDegree());
        staffEntity.setEducation(m.getEducation());
        staffEntity.setEmail(m.getEmail());
        staffEntity.setEntranceDate(m.getEntranceDate());
        staffEntity.setGraduateSchool(m.getGraduateSchool());
        staffEntity.setIdentityCardId(m.getIdentityCardId());
        staffEntity.setIsExternal(m.getIsExternal());
        staffEntity.setNation(m.getNation());
        staffEntity.setPictrue(m.getPictrue());
        staffEntity.setPoliticalStatus(m.getPoliticalStatus());
        staffEntity.setSex(m.getSex());
        staffEntity.setStatus(m.getStatus());
        staffEntity.setTelNum(m.getTelNum());
        staffEntity.setDegree(m.getDegree());
        staffEntity.setIsTeacher(m.getIsTeacher());
        staffEntity.setJobtitleId(m.getJobtitleId());
        staffEntity.setNowAddress(m.getNowAddress());
        staffEntity.setIsTutor(m.getIsTutor());
        staffEntity.setDutyId(m.getDutyId());

        return staffEntity;
    }


错误原因

1.业务逻辑:插入五张表的顺序关系;

2.架构设计:不同服务,使用分布式事务问题;

3.数据库关联:教职工id与用户id一一对应,前期没有考虑到这一问题。

总结

认真的对待自己的代码,不断重复基础知识,积累实战经验。

评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨倩-Yvonne

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值