其他分享
首页 > 其他分享> > 跟着黑马学SSM——Day8之SSM整合

跟着黑马学SSM——Day8之SSM整合

作者:互联网

SSM整合流程

  1. 创建工程
  2. SSM整合
    • Spring
      • SpringConfig
    • Mybatis
      • MybaitsConfig
      • JdbcConfig
      • jdbc.properties
    • SpringMVC
      • ServletConfig
      • SpringMvcConfig
  3. 功能模块
    • 表与实体类
    • dao(接口+自动代理)
    • service(接口+实现类)
      • 业务层接口测试(整合Junit)
    • controller
      • 表现层接口测试(PostMan)

Spring整合Mybatis

Spring整合SpringMVC

异常处理器

异常处理器

  • 集中的、统一的处理项目中出现的异常
@RestControllerAdvice
public class ProjectExceptionAdvice {
    @ExceptionHandler(Exception.class)
    public Result deException(Exception e){
        System.out.println("危险危险危险");
        return new Result(000,null,"危险危险危险");
    }
}

项目异常处理方案

项目异常处理方案

项目异常处理

  1. 自定义项目系统级异常

  2. 自定义项目业务级异常

  3. 自定义异常编码(持续补充)

  4. 触发自定义异常

  5. 拦截并处理异常

  6. 异常处理器效果对比

拦截器

拦截器概念

拦截器入门案例

  1. 声明拦截器的bean,并实现HandlerInterceptor接口(注意:扫描加载bean)

    @Component
    public class ProjectInterceptor implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            System.out.println("preHandle...");
            return true;
        }
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
            System.out.println("postHandle...");
            HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
        }
    
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
                                    Exception ex) throws Exception {
            System.out.println("afterCompletion...");
            HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
        }
    }
    
  2. 定义配置类,继承WebMvcConfigurationSupport,实现addIntercaptor方法(注意:扫描加载配置)

    @Configuration
    public class SpringMvcSupport extends WebMvcConfigurationSupport {
    
        @Autowired
        private ProjectInterceptor projectInterceptor;
        @Override
        protected void addResourceHandlers(ResourceHandlerRegistry registry) {
            super.addResourceHandlers(registry);
        }
    
        @Override
        protected void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(projectInterceptor).addPathPatterns("/books","/books/*");
        }
    }
    
  3. 添加拦截器并设定拦截的访问路径,路径可以通过可变参数设置多个

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(projectInterceptor).addPathPatterns("/books","/books/*");
    }
    
  4. 使用标准接口WebMvcConfigurer简化开发(注意:侵入式较强)

    @Configuration
    @ComponentScan({"com.xhj.controller"})
    @EnableWebMvc
    public class SpringMvcConfig implements WebMvcConfigurer {
        @Autowired
        private ProjectInterceptor projectInterceptor;
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(projectInterceptor).addPathPatterns("/books","/books/*");
        }
    }
    

执行流程

拦截器参数



多拦截器执行顺序

标签:拦截器,return,Day8,黑马,SSM,public,book,异常,id
来源: https://www.cnblogs.com/ltom/p/16676233.html