瑞吉外卖项目Day2———完善登录问题、员工功能

创建过滤器类(filter)

package com.study.filter;

import com.alibaba.fastjson.JSON;
import com.study.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//注解配置拦截器名称(类名),拦截路径"/*"
@WebFilter(filterName = "LoginCheckFilter",urlPatterns = "/*")
@Slf4j
public class LoginCheckFilter implements Filter {
    //1.路径匹配器,支持通配符
    public static  final AntPathMatcher PATH_MATCHER=new AntPathMatcher();

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest) servletRequest;
        HttpServletResponse response=(HttpServletResponse) servletResponse;

        //1.获取本次请求的URI
        String requestURI =request.getRequestURI();
        //2.不需要处理的路径定义
        String[] urls=new String[]{
                  "/employee/login",
                  "/employee/logout",
                  "backend/**",
                  "front/**"
        };

        //3.判断此次请求是否需要处理
        boolean flag=check(urls,requestURI);

        //4.如果不需要处理,直接放行
        if(flag){
            log.info("本次请求{}不需要处理",requestURI);
            filterChain.doFilter(request,response);
            return;
        }

        //4.判断登录状态,如果已经登录,则直接放行
        //查看浏览器session里面是否存在id
        if(request.getSession().getAttribute("employee")!=null){
            log.info("用户已经登录,用户id为:{}",request.getSession().getAttribute("employee"));
            filterChain.doFilter(request,response);
            return;
        }

        log.info("用户未登录");
        //5.如果未登录则返回未登录结果,通过输出流方式向客户端响应数据
        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
        return;
    }


    /**
     * 检查此次请求是否需要放行
     * @param urls
     * @param requestURI
     * @return
     */
    public  boolean check(String[] urls,String requestURI){
        for(String url:urls){
            boolean match=PATH_MATCHER.match(url,requestURI);
            if(match){
                return true;
            }
        }
        return false;
    }
}

新增员工(Controller)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yirUoxB9-1679626714687)
瑞吉外卖项目Day2———完善登录问题、员工功能_第1张图片

在Controller包中的EmployeeController添加如下方法:

/**
     * 新增员工
     * @param request
     * @param employee
     * @return
     */
    @PostMapping
    public R save(HttpServletRequest request,@RequestBody Employee employee){
        log.info("新增员工,员工信息{}",employee.toString());

        //设置初始密码为123456并且利用m5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        //获取当前登录用户的id
        Long empId =(Long) request.getSession().getAttribute("employee");

        //设置id和修改id
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);

        employeeService.save(employee);
        
        return R.success("新增员工成功");
    }

分页查询

1.创建MybatisPlusConfig类配置分页插件

package com.study.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置MP的分页插件
 */
@Configuration
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return mybatisPlusInterceptor;
    }
}

2.在EmployeeController增加分页查询方法

   @GetMapping("/page")
    public R page(int page,int pageSize,String name){
        log.info("page={},pageSize ={} name={}",page,pageSize,name);
        //构造分页构造器
        Page pageInfo=new Page(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper queryWrapper=new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(!StringUtils.isEmpty(name),Employee::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        //执行查询
        employeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

启用禁止员工

1.前端代码展示

瑞吉外卖项目Day2———完善登录问题、员工功能_第2张图片

2.增加修改和查询数据方法(controller)

/**
     * 根据id修改员工信息
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public R update(HttpServletRequest request,@RequestBody Employee employee){
        log.info(employee.toString());
        long empId=(long)request.getSession().getAttribute("employee");
        //设置更新时间
        employee.setUpdateTime(LocalDateTime.now());
        //更新用户id
        employee.setUpdateUser(empId);
        //根据id进行更新
        employeeService.updateById(employee);

        return R.success("员工信息修改成功");
    }

功能无法实现[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5npd8GPw-1679626714688)

瑞吉外卖项目Day2———完善登录问题、员工功能_第3张图片

原因:客户端将id(long)传递给服务器,服务器无法保留id的精确度(只能保留到前16类,后面三位采取四舍五入的方式保留226–>200)

1.解决:创建JackSonObjectMapper类(common)

package com.study.common;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;

/**
 * 对象映射器:基于jackson将Java对象转为json,或者将json转为Java对象
 * 将JSON解析为Java对象的过程称为 [从JSON反序列化Java对象]
 * 从Java对象生成JSON的过程称为 [序列化Java对象到JSON]
 */
public class JacksonObjectMapper extends ObjectMapper {

    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
    public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";

    public JacksonObjectMapper() {
        super();
        //收到未知属性时不报异常
        this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);

        //反序列化时,属性不存在的兼容处理
        this.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);


        SimpleModule simpleModule = new SimpleModule()
                  .addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
                  .addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
                  .addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)))

                  .addSerializer(BigInteger.class, ToStringSerializer.instance)
                  .addSerializer(Long.class, ToStringSerializer.instance)
                  .addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
                  .addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
                  .addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));

        //注册功能模块 例如,可以添加自定义序列化器和反序列化器
        this.registerModule(simpleModule);
    }
}

2.在WebMvcConfig中添加如下方法扩张MVC框架消息转换器

/**
     * 扩张MVC框架消息转换器
     * @param converters
     */
    @Override
    protected void extendMessageConverters(List> converters) {
        //创建消息转换器
        MappingJackson2HttpMessageConverter messageConverter=new MappingJackson2HttpMessageConverter();
        //设置对象转换器,底层使用JackSon将java转为json
        messageConverter.setObjectMapper(new JacksonObjectMapper());
        //将上面的消息转换器追加到MVC框架的转换器集合中,index越小越优先使用
        converters.add(0,messageConverter);
    }

编辑员工

瑞吉外卖项目Day2———完善登录问题、员工功能_第4张图片

add.html获取客户端编辑客户的id,并将id传到服务端。

服务端接受请求查询员工信息,并以json格式响应到页面。

服务端接收json数据,并将员工信息回显。再次点击保存按钮,

将员工信息传到服务器中,并且调用save方法进行保存

在EmployeeController类中添加getById方法查询数据

  @GetMapping("/{id}")
    public R getById(@PathVariable long id){
        log.info("根据id查询员工信息...");
        Employee employee=employeeService.getById(id);
        if(employee!=null){
            return R.success(employee);
        }
        return R.error("没有查询到对应员工信息");
    }

异常处理

1.创建异常类进行全局异常捕获

瑞吉外卖项目Day2———完善登录问题、员工功能_第5张图片

2.创建GlobalExceptionHandler 类

package com.study.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLIntegrityConstraintViolationException;


@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 异常处理方法
     * @param ex
     * @return
     */
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class
    )
    public R exceptionHandler(SQLIntegrityConstraintViolationException ex) {
        log.error(ex.getMessage());

        if (ex.getMessage().contains("Duplicate entry")) {
            String[] split = ex.getMessage().split(" ");
            String msg = split[2] + "已存在";
            R.error(msg);
        }

        return R.error("未知错误");
    }
}

你可能感兴趣的:(开发学习,java,spring,spring,boot,mybatis,mvc)