【Java项目】添加教职工
来源:互联网 发布:算法和程序的关系 编辑:程序博客网 时间:2024/06/02 01:10
简介
项目中教职工管理的添加功能,由于是前辈留下的,总共用了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一一对应,前期没有考虑到这一问题。
总结
认真的对待自己的代码,不断重复基础知识,积累实战经验。
阅读全文
0 0
- 【Java项目】添加教职工
- 第八周项目(3)—教职工工资
- java项目添加 memcahced
- java 项目 添加 log4j
- java项目中添加日志
- C++课程设计 教职工管理系统
- 怎样添加java的log4j添加到java项目中
- UAP框架--开发java项目--添加过滤器
- intelliJ idea 中添加gradle java项目
- eclipse java项目无法添加到servers
- IDEA java项目中添加jar包
- java小项目之用户信息添加
- 给java项目手动(代码)添加jar包
- 获得java项目,并且在某个方法中添加内容
- 为SSH架构的java web项目添加flex支持
- 向已建JAVA项目添加jar包
- java项目实战-计算器(页面布局学习 添加事件)
- 不使用maven添加dagger到eclipse Java项目里
- 阿里巴巴Java开发规约插件使用
- 使用dir列出包含继承的属性
- 如何隐藏你的Linux的命令行历史
- Hdu 3853 LOOPS DP求期望
- pandaroboot使用
- 【Java项目】添加教职工
- FZU 2108(AC自动机)
- 第一篇博客
- 36. Valid Sudoku
- Java语言基础相关的面试题目
- malloc和new的区别
- 复习:虚函数
- 【读论文】Attentive Collaborative Filtering【待完成】
- Qt————计算器练习