苍穹外卖编辑员工功能开发

编辑员工功能开发

1. 需求分析

首先需要完成根据员工id查询员工的功能,使员工的原始数据正常渲染

其次要完成编辑员工功能,根据PUT请求发送的数据进行更新操作

image-20250728223746296

image-20250728223806439

2. 代码开发

controller层:

@GetMapping("/{id}")
    @ApiOperation(value = "根据id查询员工")
    public Result<Employee> getById(@PathVariable Long id){
        log.info("查询id为{}的员工",id);
        Employee employee = employeeService.getById(id);
        return Result.success(employee);
    }

    @PutMapping
    @ApiOperation(value = "编辑员工")
    public Result editEmp(@RequestBody EmployeeDTO employeeDTO){
        log.info("编辑员工: {}",employeeDTO);
        employeeService.editEmp(employeeDTO);
        return Result.success();
    }

service层:

@Override
    public void editEmp(EmployeeDTO employeeDTO) {
        //创建新的员工对象
        Employee employee = new Employee();

        //将DTO中的属性复制到员工对象
        BeanUtils.copyProperties(employeeDTO,employee);

        //设置更新人
        employee.setUpdateUser(BaseContext.getCurrentId());

        //设置更新时间为now
        employee.setUpdateTime(LocalDateTime.now());

        //调用mapper层接口更新员工信息
        employeeMapper.update(employee);
    }

    @Override
    public Employee getById(Long id) {
        Employee employee = employeeMapper.getById(id);
        return employee;
    }

mapper层:

查询员工:

@Select("select * from employee where id = #{id}")
    Employee getById(Long id);

更新操作:

<update id="update">
        update employee
        <set>
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="username != null">username = #{username},</if>
            <if test="password != null">password = #{password},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="sex != null">sex = #{sex},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="idNumber != null">id_number = #{idNumber},</if>
            <if test="updateUser != null">update_user = #{updateUser},</if>
            <if test="status != null">status = #{status}</if>
        </set>
        where id = #{id}
    </update>

注意,字段名要使用SQL的格式如:update_time,而不能使用驼峰命名法:updateTime,因为mybatis的驼峰命名法是将对应的驼峰命名的属性映射到对应的字段,在表示实际字段时,应当书写对应的字段名,而不是错误地把字段名写成驼峰命名

3. 功能测试

前后端联调:

根据id查询功能页面正常渲染

image-20250728223419940

员工数据正常修改

image-20250728223535246

image-20250728223555733

4. 代码提交

点击Git,再点击提交并推送

中…(img-5AO2iDTC-1753713860886)]

[外链图片转存中…(img-IAlbfE85-1753713860886)]

4. 代码提交

点击Git,再点击提交并推送

image-20250728224322857

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值