【javaWeb & 功能介绍第二篇】前端表格修改数据

发布于:2024-04-06 ⋅ 阅读:(130) ⋅ 点赞:(0)

修改数据

本篇文章通过修改员工数据为例,在表格每行员工后点击修改,弹出表单进行更改数据,点击保存更新数据。

点击修改数据的按钮弹出表单:
这个时候在点击的同时访问后端服务将根据查询返回单个员工整体数据,再展现在表单之中,首先需要通过员工id进行查询:
在Controller类中:

    @GetMapping("/emps/{id}")
    //查询员工
    public Result select(@PathVariable Integer id){
        log.info("查询数据操作:{}",id);
        Emp emp=empService.select(id);
        return Result.success(emp);
    }

在服务类中:

    @Override
    public Emp select(Integer id) {
        Emp emp=empMapper.select(id);
        return emp;
    }

在数据层接口当中:

    @Select("select * from emp where id=#{}")
    public Emp select(Integer id);

通过id查询员工数据将员工数据返回在表单当中,
当点击保存时通过id进行数据的更改:
Controller类中:


    //更新员工
    @PutMapping("/emps")
    public Result update(@RequestBody Emp emp){
        log.info("更新数据操作:{}",emp);
        empService.update(emp);
        return Result.success();
    }

在服务层中:


    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
    }

在数据层接口中:

    void update(Emp emp);

在XML映射文件当中进行数据库的操作:

    <!--更新员工-->
    <update id="update">
        update emp
        set username=#{username},password=#{password},name=#{name},gender=#{gender},image=#{image},job=#{job},entrydate=#{entrydate},dept_id=#{deptId},update_time=#{updateTime}where id=#{id}
    </update>

这样数据库中的数据就会同步进行更新


网站公告

今日签到

点亮在社区的每一天
去签到