1. 程式人生 > >新巴巴運動網專案:SSM(Spring+SpringMVC+mybatis)框架的配置

新巴巴運動網專案:SSM(Spring+SpringMVC+mybatis)框架的配置

新學的框架配置,先把配置過程記錄下來,有些不懂的地方以後再慢慢理解,本專案採用IDEA+Maven的方式建立,具體建立過程不再細說,下面從具體的配置檔案寫起。

1.首先在web.xml裡配置spring監聽器:
     方法:找包,在org.org.springframework.web.context.ContextLoaderListener下,複製路徑,ok
     作用:讀取檔案。在監聽器中有常量:contextConfigLocation,來讀取上下文,我們經常配置到classpath:application-context.xml檔案

 <context-param>
          <param-name> contextConfigLocation</param-name>
          <param-value>classpath:application-context.xml</param-value>
  </context-param>
  <!-- spring的監聽器 :用來讀取上下文(contextConfigLocation)-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

2.以前我們把所有的東西配置在application-context.xml檔案中,這次採用新的方式,我們將不同的配置分開,新建幾種xml來配置,然後

   將所有的xml檔案全都包含到application-context.xml中,這樣使得配置檔案一目瞭然。

  掃包註解(@Controller等):anotation.xml
  配資料來源c3p0:jdbc.xml

  配事務:transation.xml

  配mybatis:mybatis.xml

3.詳細配置:
  3.1 配註解:anotation.xml
 <!--    註解配置檔案-->
    <!--  spring 掃包 @Service -->

    <context:component-scan base-package="cn.hdu.li.core">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
<!--  自動裝配,可以不寫-->
    <context:annotation-config/>
   3.2  配資料來源c3p0:jdbc.xml  為什麼用c3p0?  可以連線多個數據庫,斷後自動重連
<!-- c3p0配置-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>

    </bean>
 在配置中我們使用了EL表示式,那麼我們要新建一個db.properties檔案來儲存 jdbc.driver這些值,然後新建一個property.xml來讀取db.properties檔案

db.properties檔案配置:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/Sports?useSSL=false
jdbc.username=root
jdbc.password=root
#定義初始連線數
jdbc.initialSize=0
#定義最大連線數
jdbc.maxActive=20
#定義最大空閒
jdbc.maxIdle=20
#定義最小空閒
jdbc.minIdle=1
#定義最長等待時間
jdbc.maxWait=60000

   property.xml檔案配置:

 <!-- 讀取db.properties配置檔案-->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <!--jdbc配置-->
                <value>classpath:properties/db.properties</value>
                <!--memcached配置-->
            </list>
        </property>
    </bean>
  3.3  配置mybatis:mybatis.xml
<!--mybatis配置  sessionFactory配置-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--新增資料來源-->
        <property name="dataSource" ref="dataSource"/>
        <!--配置configLocation-->
        <property name="mapperLocations" value="classpath:cn/hdu/li/core/dao/product/*.xml"/>
        <!--別名-->
        <property name="typeAliasesPackage" value="cn.hdu.li.core.bean,cn.hdu.li.core.query"/>

    </bean>

    <!--掃包-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.hdu.li.core.dao"/>
    </bean>
3.4  配置spring jdbc事務:transation.xml ,關於事務的理解還會有一片博文
<!--spring jdbc事務-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--開啟事務註解-->
    <tx:annotation-driven transaction-manager="transactionManager"/>

新增tomcat,執行結果如下:


接下來繼續配置springmvc

4  spring mvc配置

  4.1 首先配置web.xml

     此專案我們配置兩個,相當於兩個前後臺Servlet,格式如下:

 <!--springmvc 前臺的配置-->
        <servlet>
            <servlet-name>front</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!--初始化引數-->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc-front.xml</param-value>
            </init-param>
        </servlet>
        <servlet-mapping>
            <servlet-name>front</servlet-name>
            <url-pattern>*.shtml</url-pattern>
        </servlet-mapping>

    <!--springmvc 後臺的配置-->
        <servlet>
            <servlet-name>back</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!--初始化引數-->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc-back.xml</param-value>
            </init-param>
        </servlet>
    <!-- 主要是截獲請求的,如果你的url-pattern定義的是資源格式例如*.do等,那麼對於所有符合這種格式的資源的請求都由指定的servlet處理。  -->
        <servlet-mapping>
            <servlet-name>back</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>

4.2.分別到springmvc-back.xml和springmvc-front.xml中去配置

     springmvc-back.xml:  這裡要注意include-filter和exclude-filter的區別,exclude-filter:除了Controller層都掃。include-filter:當我們在springmvc中我們只掃Controller層就可以了,但include-filter也會掃描其他層,所以,我們需要加上:

use-default-filters="false"
 <!--springmvc掃包 @Controller-->
    <!--use-default-filters:使用預設過濾器嗎?將其設定為false,原因在於include-filter來講它都會掃描,而不是僅僅掃描@Controller-->
    <context:component-scan base-package="cn.hdu.li" use-default-filters="false">
        <!--我們只想掃cn.hdu.li下面的Controller層-->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--jsp檢視解析器-->
    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--字首-->
        <property name="prefix" value="/WEB-INF/back_page"/>
        <!--字尾-->
        <property name="suffix" value=".jsp"/>
    </bean>

     springmvc-front.xml:
 <!--springmvc掃包 @Controller-->
    <!--use-default-filters:使用預設過濾器嗎?將其設定為false,原因在於include-filter來講它都會掃描,而不是僅僅掃描@Controller-->
    <context:component-scan base-package="cn.hdu.li" use-default-filters="false">
        <!--我們只想掃cn.hdu.li下面的Controller層-->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--jsp檢視解析器-->
    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--字首-->
        <property name="prefix" value="/WEB-INF/front_page"/>
        <!--字尾-->
        <property name="suffix" value=".jsp"/>
    </bean>

至此,springmvc配置完畢。