栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

ssm整合

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ssm整合

1.搭建ssm框架

  • 准备jar包:spring,aop,springmvc,druid,mybatis,事务,分页

     
            
                org.mybatis
                mybatis-spring
                2.0.7
            
            
            
                org.springframework
                spring-jdbc
                5.3.20
            
    
            
            
                org.mybatis
                mybatis
                3.5.10
            
            
            
                com.github.pagehelper
                pagehelper
                5.3.0
            
            
                
            
                
                com.fasterxml.jackson.core
                jackson-databind
                2.13.3
            
                     
            
                mysql
                mysql-connector-java
                8.0.28
            
                     
            
                com.alibaba
                druid
                1.2.10
            
            
            
                org.springframework
                spring-webmvc
                5.3.20
            
            
            
                commons-logging
                commons-logging
                1.2
            
    

    创建两个主配置文件,父子容器需要用到两个xml配置

    spring.xml—由ContextLoaderListener监听器加载创建父容器,加载其他bean
    springmvc.xml-----由DispatcherServlet加载创建子容器,只加载Controller层的bean

    spring.xml

    
    
       
           
       
        
        
        
    
    

    springmvc.xml

    1.springmvc注解识别
    2.静态资源配置
    3.文件解析器
    4.拦截器

    
    
       
        
        
            
            
        
        
        
        
        
        
        
        
        
      
            
            
            
            
        
      
        
            
                
                    
            
            
            
                
                    
                        
                            reasonable=true
                        
                    
                
            
        
        
        
        
        
            
        
        
            
        
        
        
    
    

    跨域配置

    
    
    
    
        
            
                
                    
                        
                            
                                
                                    http://localhost:8081
                                
                            
                            
                            
                            
                                
                                    GET
                                    POST
                                    PUT
                                    DELETE
                                    OPTIONS
                                
                            
                            
                        
                        
                    
                
            
        
    
    
    
                                    
                                    
     在web.xml中配置
      
            delegatingFilterProxy
            org.springframework.web.filter.DelegatingFilterProxy
            
                targetBeanName
                corsFilter
            
        
        
            delegatingFilterProxy
            /*
        
    

    web.xml配置

    
    
        
        
            contextConfigLocation
            classpath:spring.xml
        
        
            org.springframework.web.context.ContextLoaderListener
        
        
            delegatingFilterProxy
            org.springframework.web.filter.DelegatingFilterProxy
            
                targetBeanName
                corsFilter
            
        
        
            delegatingFilterProxy
            /*
        
        
        
            dispatcherServlet
            org.springframework.web.servlet.DispatcherServlet
            
                contextConfigLocation
                classpath:springmvc.xml
            
            1
        
        
            dispatcherServlet
            /
        
    
    

    jdbc.properties配置

    jdbc.url=jdbc:mysql://127.0.0.1:3306/xxxx?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC
    jdbc.driver=com.mysql.cj.jdbc.Driver
    jdbc.username=xxx
    jdbc.password=xxx
    jdbc.initSize=4
    jdbc.maxActive=10
    
    

    统一异常返回处理

    MvcException

    public class MvcException extends RuntimeException{
        private E e;
        public MvcException(E e) {
            this.e = e;
        }
        public E getE() {
            return e;
        }
        public void setE(E e) {
            this.e = e;
        }
    }
    

    MvcExceptionHandler

    @RestControllerAdvice
    public class MvcExceptionHandler {
    
        @ExceptionHandler(MvcException.class)
        public AxiosResult doMvcException(MvcException e){
            return AxiosResult.error(e.getE());
        }
    
        @ExceptionHandler(Exception.class)
        public AxiosResult defaultException(Exception e){
            return AxiosResult.error(e.getMessage());
        }
    }
    
    

    统一异常返回值

    AxiosResult

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class AxiosResult {
        private Integer code;
        private String msg;
        private Object data;
    
        public AxiosResult(E e, Object data) {
            this(e);
            this.data = data;
        }
    
        public AxiosResult(Object data) {
            this(E.SUC);
            this.data = data;
        }
    
        public AxiosResult(E e) {
            this.code = e.getCode();
            this.msg = e.getMsg();
        }
    
        public static AxiosResult suc(){
            return new AxiosResult(E.SUC);
        }
    
        public static AxiosResult suc(Object data){
            return new AxiosResult(E.SUC,data);
        }
    
        public static AxiosResult error(){
            return new AxiosResult(E.ERROR);
        }
        public static AxiosResult error(E e){
            return new AxiosResult(e);
        }
        public static AxiosResult error(Object data){
            return new AxiosResult(E.ERROR,data);
        }
        public static AxiosResult error(E e,Object data){
            return new AxiosResult(e,data);
        }
        public Integer getCode() {
            return code;
        }
    
        public void setCode(Integer code) {
            this.code = code;
        }
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    
        public Object getData() {
            return data;
        }
    
        public void setData(Object data) {
            this.data = data;
        }
    }
    

    E

    public enum E {
        SUC(20000,"suc"),
        ERROR(50000,"ERROR"),
        NOT_EXISTS(50001,"数据不存在"),
        PWD_ERROR(50002,"用户名或密码错误"),
        FILE_MAX_SIZE(50003,"上传文件过大" ),
        NO_LOGIN(50004,"用户未登录" );
        private Integer code;
        private String msg;
    
        E(Integer code, String msg) {
            this.code = code;
            this.msg = msg;
        }
    
        public Integer getCode() {
            return code;
        }
    
        public void setCode(Integer code) {
            this.code = code;
        }
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    }
    
    

    LoginInterceptorded拦截器

    @Component
    public class LoginInterceptor implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
            HttpSession session = request.getSession();
    
            Object login_user = session.getAttribute("LOGIN_USER");
            if(login_user==null) throw new MvcException(E.NO_LOGIN);
            return true;
        }
    }
    
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/966762.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号