部门与职位表的关系--职位表的增加与分页

文章讲述了在Java中如何处理部门表与职位表的一对多关系,包括创建新职位时的部门装配和查询职位时的部门信息查找并装配。方法涉及数据库操作、对象映射和数据处理。
摘要由CSDN通过智能技术生成
  1. 部门表与职位表是一对多的关系:
    增加或查看职位都需要装配对应的部门;删除部门则需要删除其对应的职位;
    在这里插入图片描述
  2. 增加职位
 /**
     *  创建岗位表
     * @param postDto 对象信息
     * @return PostDto
     */
    @Override
    public PostVo createPost(PostDto postDto) {
        //转换PostVo为Post
        Post post = BeanUtil.toBean(postDto, Post.class);
        //生成编号
        String postNo = createPostNo(post.getDeptNo());
        post.setPostNo(postNo);
        int flag = postMapper.insert(post);
        if (flag==0){
            throw new RuntimeException("保存职位信息出错");
        }
        PostVo postVoResult = BeanConv.toBean(post, PostVo.class);
        //装配部门
        DeptDto deptDto = DeptDto.builder()
                .dataState(SuperConstant.DATA_STATE_0)
                .parentDeptNo(postDto.getDeptNo()).build();
        //显示当前部门
        List<Dept> deptList = deptMapper.selectList(deptDto);
        //需要把对应职位列表装配到postVo中
        if (!EmptyUtil.isNullOrEmpty(deptList)){
            postVoResult.setDeptVo(BeanConv.toBean(deptList.get(0),DeptVo.class));
        }
        return postVoResult;
    }
  1. 分页查询职位
 /**
     *  多条件查询岗位表分页列表
     * @param postDto 查询条件
     * @param pageNum 页码
     * @param pageSize 每页条数
     * @return Page<PostVo>
     */
    @Override
    public PageResponse<PostVo> findPostPage(PostDto postDto, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        if (EmptyUtil.isNullOrEmpty(postDto.getDeptNo())){
            throw new BaseException("部门不能为空");
        }
        Page<List<Post>> page = postMapper.selectPage(postDto);
        PageResponse<PostVo> pageResponse = PageResponse.of(page, PostVo.class);
        if (!EmptyUtil.isNullOrEmpty(pageResponse.getRecords())){
            //获取所有记录中的部门编号
            List<String> deptNos = pageResponse.getRecords().stream().map(PostVo::getDeptNo).collect(Collectors.toList());
           //查询这些部门编号对应的部门信息
            List<DeptVo> deptVoList = deptService.findDeptInDeptNos(deptNos);
            // 遍历每条岗位记录
            pageResponse.getRecords().forEach(n->{
                n.setCreateDay(LocalDateTimeUtil.format(n.getCreateTime(), "yyyy-MM-dd"));
                //装配部门(非必须)
                deptVoList.forEach(d->{
                    // 如果岗位的部门编号与某个部门信息的编号相同,则将该部门信息装配到岗位中。
                    if (n.getDeptNo().equals(d.getDeptNo())){
                        n.setDeptVo(BeanConv.toBean(d,DeptVo.class));//职位对应部门
                    }
                });
            });
        }
        return pageResponse;
    }
  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值