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

SSM整合流程

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

SSM整合流程

SSM整合流程

  1. Pom文件导入依赖包

  2. 创建项目基础框架(可以只准备好包结构),实体类,mapper.xml文件等,方便配置spring配置文件

  3. 配置数据库jdbc.properties

  4. 配置spring配置文件applicationContext.xml

  5. 配置springMVC配置文件

  6. 配置web.xml文件

1、pom导入依赖


    5.3.10
    3.5.7
    2.0.6
    UTF-8
    8
    8
  

  
    
      junit
      junit
      4.11
      test
    
    
      javax.servlet
      javax.servlet-api
      3.0.1
      provided
    
    
    
      javax.servlet.jsp
      jsp-api
      2.0
      provided
    
    
      org.springframework
      spring-beans
      ${spring.version}
    
    
      org.springframework
      spring-core
      ${spring.version}
    
    
      org.springframework
      spring-context
      ${spring.version}
    
    
      org.springframework
      spring-expression
      ${spring.version}
    


    
      org.springframework
      spring-aop
      ${spring.version}
    
    
    
      aopalliance
      aopalliance
      1.0
    

    
    
      org.springframework
      spring-aspects
      ${spring.version}
    
    
      org.springframework
      spring-jdbc
      ${spring.version}
    
    
      org.springframework
      spring-tx
      ${spring.version}
    
    
      org.mybatis
      mybatis
      ${mybatis.version}
    
    
      org.mybatis
      mybatis-spring
      ${mybatis-spring.verion}
    
    
      mysql
      mysql-connector-java
      8.0.29
    
    
      com.alibaba
      druid
      1.2.7
    
    
      org.springframework
      spring-web
      ${spring.version}
    
    
      org.springframework
      spring-webmvc
      ${spring.version}
    
    
      org.projectlombok
      lombok
      1.18.24
    
    
      jstl
      jstl
      1.2
    
    
      taglibs
      standard
      1.1.2
    
  
2、 Spring的配置文件如下
  • jdbc.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/rbac?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
jdbc.user=root
jdbc.password=root
  • applicationContext.xml



  
    
    
    
    
        
        
        
        
    
    
    
        
        
        
        
        
        
        
        
            
                
                
            
        
    
    
    
        
        
    
    
    
        
    

    

注意: 3、SpringMVC配置文件

  •  springMVC-servlet.xml

    

    
        
        
        
    
    
    
    
4、配置web.xml

Spring提供了Web环境下的ApplicationContext(IOC)-WebApplicationContext,获取该对象的方法如下:

在web.xml中注册加载Spring上下文监听器,Spring框架中提供了ContextLoaderListener监听器类,这是一个ServletContextListner的实现类。

ContextLoaderListener负责在系统加载的时候读取配置文件,初始化ApplicationContext的实例对象,并将该实例对象放入application作用域中。

在web.xml注册ContextLoaderListener的方式如下:


        org.springframework.web.context.ContextLoaderListener
        
     

这种情况下Spring框架的配置文件应当放置在/WEB-INF文件夹下(即默认位置),并命名applicationContext.xml,web应用程序中通过这个监听器自动加载Spring的配置文件。

如果配置文件不在默认位置,也可以在web.xml中通过配置指定其位置:

  
    org.springframework.web.context.ContextLoaderListener
    
  
  
    contextConfigLocation
    classpath:applicationContext.xml
    

我们可以通过点击ContextLoaderListener进入监听器,再点击this.initWebApplicationContext(event.getServletContext());进入public WebApplicationContext initWebApplicationContext(ServletContext servletContext)方法中,在此我们可以看到ApplicationContext,

另外也可以看一下WebApplicationContext 的结构,发现父接口是ApplicationContext

  •  完整版web.xml文件配置

    
        org.springframework.web.context.ContextLoaderListener
        
    
    
        contextConfigLocation
        classpath:applicationContext.xml
    
    
        ssm
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:springmvc-config.xml
        
        1
    
    
        ssm
        /
    

  • 控制层如下
@Controller
@RequestMapping("user")
public class UserController {
    @RequestMapping("login")
    public String toLogin(){
        return "login";
    }
    @Autowired
    private UserService userService;
    @RequestMapping("/toLogin")
   public String login(Long id, String password, Model model){
        User user = userService.login(id,password);
        if(user!=null){
            model.addAttribute("user",user);
            return "main";
        }else {
            return "login";
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/978659.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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