1.完成过滤器(解决可以直接打开后台网址的问题)
先创建filter包,然后新建LoginCheckFilter类(注意启动类要添加注解)
1 @WebFilter(filterName="LoginCheckFilter",urlPatterns = "/*") 2 public class LoginCheckFilter implements Filter { 3 //路径匹配器,支持通配符 4 public static final AntPathMatcher PATH_MATCHER =new AntPathMatcher(); 5 @Override 6 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { 7 HttpServletRequest request= (HttpServletRequest) servletRequest; 8 HttpServletResponse response= (HttpServletResponse) servletResponse; 9 10 //1、获取本次请求的URI 11 String requestURI =request.getRequestURI(); 12 //定义不需要拦截的路径 13 String []urls =new String[]{ 14 "/employee/login", 15 "/employee/logout", 16 "/backend/**", 17 "/front/**" 18 }; 19 //2.判断请求是否需要拦截 20 boolean check=check(urls,requestURI); 21 //3.如果不需要处理就放行 22 if(check){ 23 filterChain.doFilter(request,response); 24 return; 25 } 26 //4.判断登录状态,已经登录则放行 27 28 if(request.getSession().getAttribute("employee")!=null){ 29 filterChain.doFilter(request,response); 30 return; 31 } 32 33 34 //5.如果未登录则返回登录结果 35 response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN"))); 36 return; 37 //request.getRequestURL() 返回全路径 request.getRequestURI() 返回除去host(域名或者ip)部分的路径 38 39 } 40 public boolean check(String []urls,String requestURI){ 41 for (String url : urls) { 42 boolean match=PATH_MATCHER.match(url,requestURI); 43 if(match){ 44 return true; 45 } 46 } 47 return false; 48 } 49 }
2.完成新增员工功能
1 /** 2 * 新增员工 3 */ 4 @PostMapping 5 public R<String> save(HttpServletRequest request,@RequestBody Employee employee){ 6 log.info("新增员工信息:{}",employee.toString()); 7 //设置初始密码并加密 8 employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes())); 9 employee.setCreateTime(LocalDateTime.now()); 10 employee.setUpdateTime(LocalDateTime.now()); 11 //获得当前登录用户的id 12 Long emId=(Long) request.getSession().getAttribute("employee"); 13 employee.setCreateUser(emId); 14 employee.setUpdateUser(emId); 15 16 employeeService.save(employee); 17 return R.success("新增员工成功!"); 18 }
3.新增员工时如果用户名相同时会报错(数据库表设置了唯一),需要处理异常。
我们在common包下面创建一个类,在类上面加上@ControllerAdvice注解
1 @ControllerAdvice(annotations = {RestController.class, Controller.class})//拦截给Controller控制器添加统一的操作或处理 2 @Slf4j 3 @ResponseBody 4 public class GlobalEceptionHandler { 5 @ExceptionHandler(SQLIntegrityConstraintViolationException.class) 6 public R<String> exceptionHander (SQLIntegrityConstraintViolationException ex){ 7 log.error(ex.getMessage()); 8 if(ex.getMessage().contains("Duplicate entry")) 9 { 10 String[] spli= ex.getMessage().split(" "); 11 String msg= spli[2] + "已经存在"; 12 return R.error(msg); 13 } 14 return R.error("失败了"); 15 } 16 }
4.完成分页查询
先配置mybatisplus插件,在common包创建类
1 @Configuration 2 public class MybatisPlusConfig { 3 @Bean 4 public MybatisPlusInterceptor mybatisPlusInterceptor(){ 5 MybatisPlusInterceptor mybatisPlusInterceptor= new MybatisPlusInterceptor(); 6 mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor()); 7 return mybatisPlusInterceptor; 8 } 9 }
然后完成controller操作
1 @GetMapping("/page") 2 public R<Page> page(int page,int pageSize,String name){ 3 4 //分页构造器 5 Page pageInfo=new Page(page,pageSize); 6 //构造条件构造器 7 LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper(); 8 queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name); 9 employeeService.page(pageInfo,queryWrapper); 10 return R.success(pageInfo); 11 }
5.完成员工状态禁用
1 @PutMapping 2 public R<String> update(@RequestBody Employee employee,HttpServletRequest request){ 3 Long emId= (Long) request.getSession().getAttribute("employee"); 4 employee.setUpdateTime(LocalDateTime.now()); 5 employee.setUpdateUser(emId); 6 employeeService.updateById(employee); 7 return R.success("员工信息修改成功"); 8 }
此时重新运行,点击禁用提示已经成功,但是数据库中的status值并未改变。这是前端js精度丢失的问题,前端只能保证前16位准确,由于id超过16位,后面的值就不准确。
我们可以把long类型的id转换为字符串,为了实现这个功能,我们需另外创建一个对象映射器来扩展mvc框架的消息转换器,复制JacksonObjectMapper.java类到config包中。
然后在webmvcconfig类中添加
1 /** 2 * 扩展消息转换器 3 */ 4 @Override 5 protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) { 6 log.info("消息转换器"); 7 //创建消息转换对象 8 MappingJackson2HttpMessageConverter messageConverter=new MappingJackson2HttpMessageConverter(); 9 //设置对象转换器,底层使用Jackson将Java对象转为json 10 messageConverter.setObjectMapper(new JacksonObjectMapper()); 11 //将上面的消息转换器对象追加到mvc框架的转换集合中 12 converters.add(0,messageConverter); 13 }
本身mvc自带有转换器,不把索引设置为0,生效的就不是我们设置的转换器
6.完成编辑功能(回显和保存)
1 @GetMapping("/{id}") 2 public R<Employee> getById(@PathVariable Long id){//@PathVariable 路径变量注解 3 Employee employee=employeeService.getById(id); 4 if (employee!=null){ 5 return R.success(employee); 6 } 7 return R.error("没有查询到员工信息"); 8 }
保存功能和之前的共用
标签:02,return,String,request,瑞吉,外卖,employee,new,public From: https://www.cnblogs.com/zhaosanmao/p/16755668.html