SpringMVC的架构的执行流程和ssm框架的整合

一.springMVC架构的执行流程

  1. 用户发送请求到前端控制器DispatcherServlet

  2. DispatcherServlet会调用Handler处理器映射器(Handler处理器映射器就相当于一个map集合,通过地址栏中输入的将要执行的方法的字符串和注解中写的字符串进行对比,如果发现相同的话就会把这个Hander处理连发送给前端控制器DispatcherServlet)

  3. 前端控制器会把这个handler发送给Handler处理器适配器(什么是适配器:就是不同的格式转换为同一个格式,因为Handler可能是不相同的可能有的是通过注解的方式,也有可能是通过继承的方式)

  4. 不同的Handler使用不同Handler执行器去执行,并返回ModelAndView到Handler处理器适配器

  5. Handler适配器处理器会把这个ModelAndView对象返回到前端控制器5. Handler适配器处理器会把这个ModelAndView对象返回到前端控制器

  6. 前端控制器会把这个对象发送到视图解析器6. 前端控制器会把这个对象发送到视图解析器

  7. 视图解析器返回一个View对象到前端控制器

  8. 前端控制器会把这个View进行渲染,就是把数据放到jsp界面中

  9. 相应客户

二.ssm整合所需的大致东西

1.Dao层

	pojo和映射文件和接口使用逆向工程生成
	SqlMapConfig.xml	mybatis核心配置文件
	ApplicationContexr-dao.xml整合后spring在dao层的配置
		数据源
		会话工厂
		扫描Mapper
2.service层

	事务					ApplicationContexr-trans.xml
	@Service注解扫描		ApplicationContexr-service.xml
3.controller层

	SpringMvc.xml
			注解扫描	扫描@Controller注解
			注解驱动	替我们显示的配置最新版的处理器映射器和处理器适配器
			视图解析器	显示的配置是为了在controller中不用每个方法都写页面的全路径
4web.xml

	springMVC前端控制器
	spring监听器

三.搭建ssm整合的流程

1.创建项目,开始配置web.xml文件
		1).配置spring容器监听器
			<!-- 加载spring容器监听器 -->
			<context-param>
				<param-name>contextConfigLocation</param-name>
				<param-value>classpath:ApplicationContext-*.xml</param-value>
			</context-param>
			<listener>
				<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
			</listener>
		2).配置springMvc的前端控制器
			<servlet>
				<servlet-name>springMvc</servlet-name>
				<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
				<init-param>
					<param-name>contextConfigLocation</param-name>
					<param-value>classpath:SpringMvc.xml</param-value>
				</init-param>
				<!-- 在tomcat启动的时候就加载这个servlet -->
				<load-on-startup>1</load-on-startup>
			</servlet>
			<servlet-mapping>
				<servlet-name>springMvc</servlet-name>
				<url-pattern>*.action</url-pattern>
			</servlet-mapping>
2.ApplicationContext-*.xml
	1).pojo和映射文件和接口使用逆向工程生成
	2).SqlMapConfig.xml
		可以先什么都没有,只是一个简单的SqlMapConfig.xml
			<?xml version="1.0" encoding="UTF-8" ?>
			<!DOCTYPE configuration
			PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
			"http://mybatis.org/dtd/mybatis-3-config.dtd">
			<configuration>

			</configuration>
	3).配置db.properties文件
		jdbc.driver=com.mysql.jdbc.Driver
		jdbc.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8
		jdbc.username=root
		jdbc.password=root
	4).配置dao层的ApplicationContext-dao.xml
		<beans xmlns="http://www.springframework.org/schema/beans"
			xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
			xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
			xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
			xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
			http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

			<!-- 加载配置文件 -->
			<context:property-placeholder location="classpath:db.properties" />
			<!-- 数据库连接池 -->
			<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
				destroy-method="close">
				<property name="driverClassName" value="${jdbc.driver}" />
				<property name="url" value="${jdbc.url}" />
				<property name="username" value="${jdbc.username}" />
				<property name="password" value="${jdbc.password}" />
				<property name="maxActive" value="10" />
				<property name="maxIdle" value="5" />
			</bean>

			<!-- mapper配置 -->
			<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
			<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
				<!-- 数据库连接池 -->
				<property name="dataSource" ref="dataSource" />
				<!-- 加载mybatis的全局配置文件 -->
				<property name="configLocation" value="classpath:SqlMapConfig.xml" />
			</bean>

			<!-- 配置Mapper扫描器 -->
			<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
				<property name="basePackage" value="bh.shy.dao" />
			</bean>
		</beans>
	5).开始配置service层的ApplicationContext-service.xml
		<?xml version="1.0" encoding="UTF-8"?>
		<beans xmlns="http://www.springframework.org/schema/beans"
			xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
			xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
			xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
			xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
			http://www.springframework.org/schema/tx 
			http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
			http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
			
			<!-- service扫描 -->
			<context:component-scan base-package="bh.shy.service"></context:component-scan>
			
		</beans>
	6).配置平台事务管理器ApplicationContext-trans.xml
		<?xml version="1.0" encoding="UTF-8"?>
		<beans xmlns="http://www.springframework.org/schema/beans"
			xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
			xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
			xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
			xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
			http://www.springframework.org/schema/tx 
			http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
			http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

			<!-- 事务管理器 -->
			<bean id="transactionManager"
				class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
				<!-- 数据源 -->
				<property name="dataSource" ref="dataSource" />
			</bean>

			<!-- 通知 -->
			<tx:advice id="txAdvice" transaction-manager="transactionManager">
				<tx:attributes>
					<!-- 传播行为 -->
					<tx:method name="save*" propagation="REQUIRED" />
					<tx:method name="insert*" propagation="REQUIRED" />
					<tx:method name="delete*" propagation="REQUIRED" />
					<tx:method name="update*" propagation="REQUIRED" />
					<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
					<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
				</tx:attributes>
			</tx:advice>

			<!-- 切面 -->
			<aop:config>
				<aop:advisor advice-ref="txAdvice"
					pointcut="execution(* bh.shy.service.*.*(..))" />
			</aop:config>

		</beans>
	7).配置springMvc.xml
		<?xml version="1.0" encoding="UTF-8"?>
		<beans xmlns="http://www.springframework.org/schema/beans"
			xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
			xmlns:context="http://www.springframework.org/schema/context"
			xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
			xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
				http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
				http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

			<!-- @Controller注解扫描 -->
			<context:component-scan base-package="bh.shy.controller"/>
			<!-- 我也不知道为什么这里还要开启扫描,不开启不好使 -->
			<context:component-scan base-package="bh.shy.service"/>

			<!-- 注解驱动: 替我们显示的配置了最新版的注解的处理器映射器和处理器适配器 -->
			<mvc:annotation-driven></mvc:annotation-driven>

			<!-- 配置视图解析器 作用:在controller中指定页面路径的时候就不用写页面的完整路径名称了,可以直接写页面去掉扩展名的名称 -->
			<bean
				class="org.springframework.web.servlet.view.InternalResourceViewResolver">
				<!-- 真正的页面路径 = 前缀 + 去掉后缀名的页面名称 + 后缀 -->
				<!-- 前缀 -->
				<property name="prefix" value="/WEB-INF/jsp/"></property>
				<!-- 后缀 -->
				<property name="suffix" value=".jsp"></property>
			</bean>
		</beans>
3.生成项目的整体结构controller,service,dao,pojo
4.在controller类中先开启@Controller注解
	开启RequestMapping("/xxx")注解
	注入service
5.在servcieImpl中开启@Service注解
	注入Mapping
6.测试通过
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值