SSM搭建过程

本文详细介绍了SSM(Spring+SpringMVC+MyBatis)框架的工作流程,从用户请求到响应结果的全过程,以及如何在web.xml和springmvc.xml中进行配置。涵盖了前端控制器、处理器映射器、适配器、视图解析器等核心组件的设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SSM

一、一个完整的过程
      第一步:发起请求到前端控制器(DispatcherServlet)
      第二步:前端控制器请求HandlerMapping查找 Handler
    可以根据xml配置、注解进行查找
      第三步:处理器映射器HandlerMapping向前端控制器返回Handler
      第四步:前端控制器调用处理器适配器去执行Handler
      第五步:处理器适配器去执行Handler
      第六步:Handler执行完成给适配器返回ModelAndView
      第七步:处理器适配器向前端控制器返回ModelAndView
    ModelAndView是springmvc框架的一个底层对象,包括 Model和view
      第八步:前端控制器请求视图解析器去进行视图解析
    根据逻辑视图名解析成真正的视图(jsp)
      第九步:视图解析器向前端控制器返回View
      第十步:前端控制器进行视图渲染
    视图渲染将模型数据(在ModelAndView对象中)填充到request域
      第十一步:前端控制器向用户响应结果

二、配置过程

1、配置前端控制器   ( dispatcherServlet )  在web.xml中配置
     头部  <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
              </web-app>

               <!-- 加载spring容器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

          <!-- 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>
                </servlet>
    <servlet-mapping>
       <servlet-name>springmvc</servlet-name>
       <url-pattern>*.action</url-pattern>
    </servlet-mapping>
2、配置处理器映射器 、配置处理器适配器、配置视图解析器   在springmvc.xml中配置
      头部   
                 <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
                   </beans>
        注解映射器、注解适配器
              <mvc:annotation-driven ></mvc:annotation-driven>     一般用这个
              或者
               <!--注解映射器 -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    <!--注解适配器 -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
        
        <!--组件扫描 :找到对应的处理  -->
       <context:component-scan base-package="com.controller"></context:component-scan>

        视图解析器
                  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                                <!-- 配置jsp路径的前缀 -->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!-- 配置jsp路径的后缀 -->
        <property name="suffix" value=".jsp"/>
                    </bean>

3、mybatis的配置文件  sqlMapConfig.xml
      头部  <!DOCTYPE configuration
                 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
                 "http://mybatis.org/dtd/mybatis-3-config.dtd">
       具体配置
                <configuration>
    <!-- 全局setting配置,根据需要添加 -->    
    <!-- 配置别名 -->
    <typeAliases>
                <!-- 批量扫描别名 --><package name="com.po"/>
    </typeAliases>
    <!-- 配置mapper
    由于使用spring和mybatis的整合包进行mapper扫描,这里不需要配置了。
    必须遵循:mapper.xml和mapper.java文件同名且在一个目录
     -->
    <!-- <mappers></mappers> -->
                </configuration>
4、配置 applicationContext-dao.xml
     头部跟springmvc.xml的一样
 
         <!-- 加载db.properties文件中的内容,db.properties文件中key命名要有一定的特殊规则 -->
    <context:property-placeholder location="classpath:db.properties" />
    <!-- 配置数据源 ,dbcp -->
    <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="30" />
        <property name="maxIdle" value="5" />
    </bean>
    <!-- sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据库连接池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 加载mybatis的全局配置文件 -->
        <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
    </bean>
    <!-- mapper扫描器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 扫描包路径,如果需要扫描多个包,中间使用半角逗号隔开 -->
        <property name="basePackage" value="com.mapper"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>
5、配置applicationContext-service.xml
     头部跟springmvc.xml的一样

     <!-- 商品管理的service -->
     <bean id="itemsService" class="com.service.impl.ItemsServiceImpl"/>
     </beans>
6、配置applicationContext-transaction.xml
     头部跟springmvc.xml一样
   
     <!-- 事务管理器     s对mybatis操作数据库事务控制,spring使用jdbc的事务控制类-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!-- 数据源  dataSource在applicationContext-dao.xml中配置了 -->
    <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <!-- 传播行为 -->
        <tx:method name="save*" propagation="REQUIRED"/>
        <tx:method name="delete*" propagation="REQUIRED"/>
        <tx:method name="insert*" 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:method name="select*" propagation="SUPPORTS" read-only="true"/>
    </tx:attributes>
    </tx:advice>
    <!-- aop -->
    <aop:config>
    <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.service.impl.*.*(..))"/>
    </aop:config>
     
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值