SSM整合筆記

clipboard.png

1 web.xml
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!-- 統一字符集-->
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!-- springmvc 的前端控制器 -->
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--
      springMVC的配置文件
      判斷contextConfigLocation是否存在
      存在:web容器會加載該配置項的value
      不存在:web容器會去約定好的位置/WEB-INF/ 下尋找 <servlet-name>-servlet.xml
    -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
  <!-- spring的配置文件 -->
  <!--
       當配置ContextLoaderListener時 會監聽contextConfigLocation是否存在
       存在:web容器會加載該配置項的value
       不存在:web容器會去約定好的位置/WEB-INF/ 下尋找applicationContext.xml
  -->
  <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>

</web-app>
2 spring的配置文件 applicationContext.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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:component-scan base-package="com.mmall" />

    <!-- 導入spring 和 mybatis的整合文件 -->
    <import resource="spring-mybatis.xml" />
</beans>

<!-- 這是spring的配置文件
     當配置-->
3 springMVC的配置文件 spring-mvc.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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 激活spring-mvc的註解掃描 -->
    <context:component-scan base-package="com.mmall" />

    <!-- (1)自動註冊DefaultAnnotationHandleMapping,AnnotationMethodHandlerAdapter
         (2)提供一些列的功能:數據綁定,數字和日期format @NumberFormat,@DataTimeFormat,xml,json默認-->
    <mvc:annotation-driven />


    <!-- 文件上傳 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10485760"/> <!-- 10m -->
        <property name="maxInMemorySize" value="4096" />
        <property name="defaultEncoding" value="UTF-8"></property>
    </bean>

</beans>



        <!-- 這是spring-mvc 的配置文件 當web.xml中的DispatcherServlet中沒有聲明contextConfigLocation時
             按照約定 默認去找 /WEB-INF/{servlet-name}-servlet.xml 做爲配置文件
             若是配置了contextConfigLocation 則去 <context-value> 的路徑中查找配置文件-->
4 spring和mybatis的整合文件
<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 1 導入jdbc.properties-->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
            </list>
        </property>
    </bean>

    <!-- 2 JDBC數據庫鏈接池-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${db.driverClassName}"/>
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
        <!-- 鏈接池啓動時的初始值 -->
        <property name="initialSize" value="${db.initialSize}"/>
        <!-- 鏈接池的最大值 -->
        <property name="maxActive" value="${db.maxActive}"/>
        <!-- 最大空閒值.當通過一個高峯時間後,鏈接池能夠慢慢將已經用不到的鏈接慢慢釋放一部分,一直減小到maxIdle爲止 -->
        <property name="maxIdle" value="${db.maxIdle}"/>
        <!-- 最小空閒值.當空閒的鏈接數少於閥值時,鏈接池就會預申請去一些鏈接,以避免洪峯來時來不及申請 -->
        <property name="minIdle" value="${db.minIdle}"/>
        <!-- 最大創建鏈接等待時間。若是超過此時間將接到異常。設爲-1表示無限制 -->
        <property name="maxWait" value="${db.maxWait}"/>
        <!--#給出一條簡單的sql語句進行驗證 -->
        <!--<property name="validationQuery" value="select getdate()" />-->
        <property name="defaultAutoCommit" value="${db.defaultAutoCommit}"/>
        <!-- 回收被遺棄的(通常是忘了釋放的)數據庫鏈接到鏈接池中 -->
        <!--<property name="removeAbandoned" value="true" />-->
        <!-- 數據庫鏈接過多長時間不用將被視爲被遺棄而收回鏈接池中 -->
        <!--<property name="removeAbandonedTimeout" value="120" />-->
        <!-- #鏈接的超時時間,默認爲半小時。 -->
        <property name="minEvictableIdleTimeMillis" value="${db.minEvictableIdleTimeMillis}"/>

        <!--# 失效檢查線程運行時間間隔,要小於MySQL默認-->
        <property name="timeBetweenEvictionRunsMillis" value="40000"/>
        <!--# 檢查鏈接是否有效-->
        <property name="testWhileIdle" value="true"/>
        <!--# 檢查鏈接有效性的SQL語句-->
        <property name="validationQuery" value="SELECT 1 FROM dual"/>
    </bean>


    <!-- 3  配置sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入數據庫鏈接池 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 配置MyBatis的全局配置文件 :mybatis-config.xml -->
        <!--<property name="configLocation" value="classpath:mybatis-config.xml"/>-->
        <!-- 掃描entity包 使用別名 -->
        <property name="typeAliasesPackage" value="com.dsying.entity"/>
        <!-- 掃描sql配置文件:***Mapper.xml -->
        <property name="mapperLocations" value="classpath*:mappers/*.xml" />
    </bean>


    <!-- 4  配置掃描DAO接口   目的:爲了動態實現Dao接口,注入到spring容器中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注入sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <!-- 給出Dao接口包 -->
        <property name="basePackage" value="com.mmall.dao" />
    </bean>


    <!-- 使用@Transactional進行聲明式事務管理須要聲明下面這行 -->
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
    <!-- 事務管理 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
        <!-- 失敗回滾 -->
        <property name="rollbackOnCommitFailure" value="true"/>
    </bean>

</beans>
5 數據庫參數 jdbc.properties
db.driverLocation=/Users/****/Develop/lib/mysql-connector-java-5.1.6-bin.jar
db.driverClassName=com.mysql.jdbc.Driver
db.username=用戶名
db.password=密碼
db.url=jdbc:mysql://localhost:3306/mmall_learning?characterEncoding=utf-8


db.initialSize = 20
db.maxActive = 50
db.maxIdle = 20
db.minIdle = 10
db.maxWait = 10
db.defaultAutoCommit = true
db.minEvictableIdleTimeMillis = 3600000
7 項目結構圖

clipboard.png

相關文章
相關標籤/搜索