当前位置: 首页 > article >正文

springboot根据id编辑用户账号信息

一、首先需要在修改表单中“回显”数据

在这里插入图片描述

Controller类

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")//id为路径参数
    @ApiOperation("根据id查询员工信息")
    public Result<Employee> selectById(@PathVariable Long id){
        log.info("根据id查询员工信息:{}",id);
        Employee employee = employeeService.selectById(id);
        return Result.success(employee);
    }

Service类

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @Override
    public Employee selectById(Long id) {
        Employee employee = employeeMapper.selectById(id);
        //将密码隐藏,返回前端
        employee.setPassword("******");
        return employee;
    }

Mapper.java实现

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @Select("select * from employee where id = #{id}")
    Employee selectById(Long id);

二、编辑员工信息

Controller类

    /**
     * 编辑员工信息
     * @param employeeDTO
     * @return
     */
    @PutMapping//注意这是PutMapping
    @ApiOperation("编辑员工信息")
    public Result update(@RequestBody EmployeeDTO employeeDTO){
        log.info("编辑员工信息:{}",employeeDTO);
        employeeService.update(employeeDTO);
        return Result.success();
    }

Service类

    /**
     * 编辑员工信息
     * @param employeeDTO
     */
    @Override
    public void update(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
		//对象拷贝
        BeanUtils.copyProperties(employeeDTO,employee);

        employee.setUpdateTime(LocalDateTime.now());
        //获取当前登录者的id,当前登录者id在拦截器里已经存放进ThreadLocal中
        employee.setUpdateUser(BaseContext.getCurrentId());

        employeeMapper.update(employee);
    }

Mapper.java类

    /**
     * 根据主键动态修改属性
     * @param employee
     */
    void update(Employee employee);

Mapper.xml

    <update id="update" parameterType="Employee">
        update employee
        <set>
            <if test="name != null">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="idNumber != null">id_Number = #{idNumber},</if>
            <if test="updateTime != null">update_Time = #{updateTime},</if>
            <if test="updateUser != null">update_User = #{updateUser},</if>
            <if test="status != null">status = #{status},</if>
        </set>
        where id = #{id};
    </update>

拦截器实现

package com.sky.interceptor;

import com.sky.constant.JwtClaimsConstant;
import com.sky.context.BaseContext;
import com.sky.properties.JwtProperties;
import com.sky.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * jwt令牌校验的拦截器
 */
@Component
@Slf4j
public class JwtTokenAdminInterceptor implements HandlerInterceptor {

    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 校验jwt
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        System.out.println("当前线程的id"+Thread.currentThread().getId());

        //判断当前拦截到的是Controller的方法还是其他资源
        if (!(handler instanceof HandlerMethod)) {
            //当前拦截到的不是动态方法,直接放行
            return true;
        }

        //1、从请求头中获取令牌
        String token = request.getHeader(jwtProperties.getAdminTokenName());

        //2、校验令牌
        try {
            log.info("jwt校验:{}", token);
            Claims claims = JwtUtil.parseJWT(jwtProperties.getAdminSecretKey(), token);
            Long empId = Long.valueOf(claims.get(JwtClaimsConstant.EMP_ID).toString());
            log.info("当前员工id:", empId);

            //将当前登录的员工的id存放在ThreadLocal(线程中的局部变量,整个线程共享)中;
            //拦截器中将员工id存进ThreadLocal中,在项目的任何一个地方都可以取到
            BaseContext.setCurrentId(empId);
            //3、通过,放行
            return true;
        } catch (Exception ex) {
            //4、不通过,响应401状态码
            response.setStatus(401);
            return false;
        }
    }
}

GetMapping、PostMapping和PutMapping各有什么区别

@PutMapping@PostMapping@GetMapping 是 Spring Framework 中用于处理 HTTP 请求的方法注解。它们的主要区别在于 HTTP 方法的语义和用途:

  1. @GetMapping

    • HTTP 方法:GET
    • 用途:用于请求数据。通常用于从服务器获取资源,不应对服务器的状态产生影响。
    • 特点:请求参数可以附加在 URL 中,且通常是安全的(即无副作用)。
  2. @PostMapping

    • HTTP 方法:POST
    • 用途:用于向服务器提交数据。常用于创建新的资源或进行一些需要改变服务器状态的操作。
    • 特点:请求体中可以包含大量数据,适用于需要发送复杂对象的场景。
  3. @PutMapping

    • HTTP 方法:PUT
    • 用途:用于更新现有资源。通常用于替换现有资源的全部内容。
    • 特点:可以用于幂等操作,即多次相同的请求会产生相同的效果。

总结

  • GET:获取资源,不改变状态。
  • POST:提交数据,创建新资源。
  • PUT:更新资源,替换内容。

http://www.kler.cn/a/349134.html

相关文章:

  • Zsh 安装与配置
  • 电子取证新视角:USB键盘流量提取密码方法研究与实现
  • Python脚本爬取目标网站上的所有链接
  • 低代码工单管理app评测,功能与效率解析
  • LangChain使用Prompt02
  • Matlab实现野马优化算法优化回声状态网络模型 (WHO-ESN)(附源码)
  • 算法:238.除自身以外数组的乘积
  • 浙大数据结构:11-散列3 QQ帐户的申请与登陆
  • 植物大战僵尸杂交版
  • 运行Springboot + Vue 项目
  • 回归本真 治愈心灵——汪青《在西行的路上》
  • 地平线与英伟达工具链 PTQ 工具功能参数对比与实操
  • Linux下以编译源码的方式安装Qt5与Qt6及其使用
  • AB路线——BFS+分层图
  • Git---Git打标签
  • ui入门
  • Antsword-labs靶机渗透
  • Python基础语法条件
  • 基础IO -- 理解文件(1)
  • 使用tgz包下载安装clickhouse低版本