这波 SpringFramework5.x 我先干了(MVC框架整合)

本文详细介绍了MVC框架整合Spring MVC与Mybatis的步骤,包括构建web环境所需的依赖,整合Spring与Struts2、Spring与Mybatis的思路和编码实现,以及Spring多配置文件管理和SSM整合实例。
摘要由CSDN通过智能技术生成

一、MVC框架整合思想

1.搭建web运行环境

 <dependencies>
    <!--Servlet相关jar包-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>javax.servlet.jsp-api</artifactId>
      <version>2.3.1</version>
      <scope>provided</scope>
    </dependency>
    <!--SpringWeb 依赖jar-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.1.14.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.1.14.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.1.14.RELEASE</version>
    </dependency>
    <!--Spring 控制事务 依赖jar-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.1.14.RELEASE</version>
    </dependency>
    <!--spring依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.1.14.RELEASE</version>
    </dependency>

    <!--Spring web依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.1.14.RELEASE</version>
    </dependency>

    <!--排除默认日志依赖-->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.25</version>
    </dependency>

    <!--log4j依赖-->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>

    <!--单元测试-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
    </dependency>

    <!--MySql 依赖jar-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.49</version>
    </dependency>

    <!--mybatis 依赖jar-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.6</version>
    </dependency>

    <!--Spring与Mybatis整合 依赖jar-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.1.14.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>2.0.2</version>
    </dependency>

    <!--连接池 依赖jar-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.18</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <!--SpringAOP代理开发 jar-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>5.1.14.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.8.8</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.3</version>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.12</version>
      <scope>compile</scope>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

2.为什么要整合MVC框架

1.MVC框架提供了控制器(Controller)调用Service
2.请求相应的处理
3.接受请求参数 
4.控制程序运行流程
5.试图解析(JSP  JSON  Freemarker Thyemeleaf)

3.Spring可以整合哪些MVC框架

1.struts1
2.webwork
3.jsf
4.struts2
5.springMVC

4.Spring整合MVC框架的核心思路
(1)准备工厂

1.Web开发过程中如何创建工厂
		ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
												new  WebXmlApplicationContext();
2.如何保证工厂唯一同时被公用
	被公用:Web   request|session|ServletContext(application)
	工厂存储在ServletContext这个作用域中ServletContext.setAttribut("xxx",ctx);
	
	唯一:ServletContext对象 创建的同时--->ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
	ServletContextListener --->Application ctx  =new ClassPathXmlApplicationContext("/applicationContext.xml");
	ServletContextListener 在ServlContext对象创建的同时,被调用(只会被调用一次),把工程创建的代码,写在ServletContextListener中,也会保证只调用一次,最终工厂就保证了唯一性
3.总结
	ServletContextListener(唯一)
		ApplicationContext ctx =new ClassPathXmlApplicationContext("/applicationContext.xml");
		ServletContext.setAttribute("xxx",ctx);  (公用)
4.Spring封装了一个ContextLoaderListener			
		1)创建工厂
		2)把工厂存在ServletContext中										
ContextLoaderListener使用方式
Web.xml

<listener>
	<listener—class>org.springframework.web.context.ContextLoaderListener</listener>
</listener>
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:applicationContext.xml<param-value>
</context-param>

(2)代码整合

依赖注入:把Service对象注入给控制器对象
在这里插入图片描述

二、Spring与Struts2框架整合

1.Spring与Struts2整合思路分析

Struts2中的Action需要通过Spring的依赖注入获得Service对象。

2.Spring与Struts2整合的编码实现

  • 搭建开发环境

    • 引入相关jar(Spring Struts2)
    <!--struts2 相关jar-->
    <dependency>
     	<groupId>org.apache.struts</groupId>
     	<artifactId>struts2-spring-plugin</artifactId>
     	<version>2.3.8</version>
    </dependency>
    
    • 引入对应的配置文件
      (1)applicationContext.xml
      (2)struts.xml
      (3)log4j.properties
    • 初始化配置
      Spring —ContextLoaderListener -> web.xml
      Struts2—filter -> web.xml
       <!--Spring 创建工厂的配置-->
    <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    
    <!--Struts2 配置-->
    <filter>
       <filter-name>struts2</filter-name>
       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
       <filter-name>struts2</filter-name>
       <url-pattern>/*</url-pattern>
    </filter-mapping>
    
  • 编码

    • 开发service对象
      最终在Spring配置文件中创建Service对象
      <bean id="userService" class="com.springweb.struts.UserServiceImpl"></bean>
      
    • 开发Action对象
      (1)开发类
      public class RegAction implements Action {
          private UserService userService;
      
          public UserService getUserService() {
              return userService;
          }
      
          public void setUserService(UserService userService) {
              this.userService = userService;
          }
      
          @Override
          public String execute() throws Exception {
              userService.register();
              return Action.SUCCESS;
          }
      }
      
      (2)Spring(appicationContext.xml)
      <bean id="regAction" scope="prototype" class="com.springweb.struts.RegAction">
          <property name="userService" ref="userService"></property>
      </bean>
      
      (3)Struts2(struts.xml)
      <package name="str" extends="struts-default">
      	url reg.action --->会接受到用户的请求后,创建RegAction这个类的对象 进行相应的处理
      	<action name="reg" class="regAction">
          	<result name="success">/index.jsp</result>
      	</action>
      </package>
      

3.Spring+Struts2+Mybatis整合
1.思路分析

SSM = Spring + Struts2 Spring+Mybatis
在这里插入图片描述

2.整合编码

  • 搭建开发环境

    • 引入相关jar(Spring Struts2 Mybatis)
      <!--struts2 相关jar-->
      <dependency>
         <groupId>org.apache.struts</groupId>
         <artifactId>struts2-spring-plugin</artifactId>
         <version>2.3.8</version>
      </dependency>
      <!--Servlet相关jar包-->
      <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>javax.servlet-api</artifactId>
         <version>3.1.0</version>
         <scope>provided</scope>
      </dependency>
      <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>jstl</artifactId>
         <version>1.2</version>
      </dependency>
      <dependency>
         <groupId>javax.servlet.jsp</groupId>
         <artifactId>javax.servlet.jsp-api</artifactId>
         <version>2.3.1</version>
         <scope>provided</scope>
      </dependency>
      <!--SpringWeb 依赖jar-->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-web</artifactId>
         <version>${spring.version}</version>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-core</artifactId>
         <version>${spring.version}</version>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-beans</artifactId>
         <version>${spring.version}</version>
      </dependency>
      <!--Spring 控制事务 依赖jar-->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-tx</artifactId>
         <version>${spring.version}</version>
      </dependency>
      <!--spring依赖-->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-context</artifactId>
         <version>${spring.version}</version>
      </dependency>
      <!--Spring web依赖-->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-webmvc</artifactId>
         <version>${spring.version}</version>
      </dependency>
      <!--排除默认日志依赖-->
      <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-log4j12</artifactId>
         <version>1.7.25</version>
      </dependency>
      <!--log4j依赖-->
      <dependency>
         <groupId>log4j</groupId>
         <artifactId>log4j</artifactId>
         <version>1.2.17</version>
      </dependency>
      <!--单元测试-->
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
      </dependency>
      <!--MySql 依赖jar-->
      <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version>5.1.49</version>
      </dependency>
      <!--mybatis 依赖jar-->
      <dependency>
         <groupId>org.mybatis</groupId>
         <artifactId>mybatis</artifactId>
         <version>3.4.6</version>
      </dependency>
      <!--Spring与Mybatis整合 依赖jar-->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-jdbc</artifactId>
         <version>${spring.version}</version>
      </dependency>
      <dependency>
         <groupId>org.mybatis</groupId>
         <artifactId>mybatis-spring</artifactId>
         <version>2.0.2</version>
      </dependency>
      <!--连接池 依赖jar-->
      <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>druid</artifactId>
         <version>1.1.18</version>
      </dependency>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.12</version>
         <scope>test</scope>
      </dependency>
      <!--SpringAOP代理开发 jar-->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-aop</artifactId>
         <version>${spring.version}</version>
      </dependency>
      <dependency>
         <groupId>org.aspectj</groupId>
         <artifactId>aspectjrt</artifactId>
         <version>1.8.8</version>
      </dependency>
      <dependency>
         <groupId>org.aspectj</groupId>
         <artifactId>aspectjweaver</artifactId>
         <version>1.8.3</version>
      </dependency>
      <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
         <version>1.18.12</version>
         <scope>compile</scope>
      </dependency>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.11</version>
         <scope>test</scope>
      </dependency>
      
    • 引入对应的配置文件
      (1)applicationContext.xml
      (2)struts.xml
      (3)log4j.properties
      (4)xxxMapper.xml
    • 初始化配置
      Spring —ContextLoaderListener -> web.xml
      Struts2—filter -> web.xml
       <!--Spring 创建工厂的配置-->
    <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    
    <!--Struts2 配置-->
    <filter>
       <filter-name>struts2</filter-name>
       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
       <filter-name>struts2</filter-name>
       <url-pattern>/*</url-pattern>
    </filter-mapping>
    
  • 编码

    • DAO(Spring+Mybatis)
      1.配置文件的配置
      	(1).DataSource
      	(2).SqlSessionFactory --->SqlSessionFactoryBean
      		1>.dataSource
      		2>.typeAliasesPackage
      		3>.mapperLocations
      	(3).MapperScannerConfigur --->DAO接口实现类	
      2.编码
      	(1).entity
      	(2).table数据库表
      	(3).DAO接口
      	(4).实现Mapper
      
    • Service(Spring添加事务)
      1.原始对象 ---->注入DAO
      2.额外功能 ---->DataSourceTransactionManager --->dataSource
      3.切入点 + 事务属性
      	@Transactional(propagation,redOnly...)
      4.组装切面
      	<tx:annotation-driven
      
    • Controller(Spring+Struts2)
      1.开发控制器 impliements Action 注入Service
      2.Spring的配置文件
      	(1).注入Service
      	(2).scope=prototype
      3.struts.xml
      	<action class="Spring配置文件中action对应的id值"/>
      

三、Spring多配置文件

Spring会根据需要,把配置信息分门别类的放置在多个配置文件中,便于后续的管理及维护。
DAO -------------application-dao.xml
Service -------------application-service.xml
Action -------------application-action.xml
注意:虽然提供了多个配置文件,但是后续应用的过程中,还要进行整合

  • 通配符方式
1.非web环境
	ApplicationContext ctx = new ClassPathXmlApplicationContext("/application-*.xml")
2.web环境
 	<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext-*.xml</param-value>
    </context-param>
  • import标签
applicationContext.xml 目的 整合其他配置内容
<import resource="applicationContext-dao.xml"/>
<import resource="applicationContext-service.xml"/>
<import resource="applicationContext-action.xml"/>
1.非web环境
	ApplicationContext ctx = new ClassPathXmlApplicationContext("/application.xml")
2.web环境
 	<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

四、Spring+SpringMVC+Mybatis编码整合

待完结。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值