SSM搭建過程

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>
     
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章