SpringMVC Spring Mybatis 整合中所需要的配置文件

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>


<web-app>

可能需要配套的文件(三大組件)

  1. 前端控制器
  <!-- 配置前端控制器 -->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    
    <!-- 加載springMVC的配置文件 -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springMVC.xml</param-value>
    </init-param>
    
    <!-- 啓動服務時就啓動servlet -->
    <load-on-startup>1</load-on-startup>
  </servlet>

	<!-- 映射前端控制器-->
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  1. 字符編碼過濾器
  <!-- 配置編碼過濾器 -->
  <filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <!-- 編碼方式-->
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  
  <!-- 映射編碼過濾器-->
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  1. 監聽器
  <!-- 配置Spring的監聽器,默認只加載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:applicationConfig.xml</param-value>
  </context-param>

|
|
|


|
|
|

2.SpringMVC的配置文件

  • springMVC.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       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.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">



</beans>

可能需要配套的文件

  1. 註解掃描
    <!-- 開啓註解掃描 只掃描controller -->
    <context:component-scan base-package="cn.itcast">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

  1. 視圖解析器
	<!-- 配置視圖解析器 -->
    <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 路徑 value路徑可以自己選擇-->
        <property name="prefix" value="/WEB-INF/pages/"/>
        <!-- 後綴 value後綴可以自己選擇-->
        
        <property name="suffix" value=".jsp"/>
    </bean>
  1. 靜態資源過濾器
	<!-- 配置靜態資源過濾器 -->
    <mvc:resources mapping="/css/**" location="/css/"/>
    <mvc:resources mapping="/js/**" location="/js/"/>
    <mvc:resources mapping="/images/**" location="/images/"/>
  1. 開啓springmvc註解配置方式
	<!-- 開啓mvc註解方法-->
    <mvc:annotation-driven/>

|
|
|


|
|
|

3.Spring和Mybatis組合文件

  • applicationConfig.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: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.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">


</beans>

可能需要配套的文件

  1. 開啓註解掃描
	<!-- 開啓註解掃描 不掃描controller-->
    <context:component-scan base-package="cn.itcast">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
  1. spring整合Mybatis
	<!-- spring整合Mybatis -->
    <!-- 
    	1. 導入jdbcConfig.properties(可選)
    	自己可以寫jdbcConfig.properties這樣寫配置連接池的時候相對比較方便
    -->
    <context:property-placeholder location="classpath:jdbcConfig.properties"/>

    <!-- 2. 配置連接池 -->
    <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>
    
    <!-- 3. 配置SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 
    	4. 配置接口所在包
    	value就是自己持久層接口的路徑 
     -->
    <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.yang.dao"/>
    </bean>
  1. 配置spring框架的事務聲明管理
<!-- 配置spring框架的聲明事務管理 -->
    <!-- 1. 配置事務 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 2. 配置事務通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 配置find字段開頭的都是隻讀-->
            <tx:method name="find*" read-only="true"/>
            <!-- 其它任意字段都是默認 -->
            <tx:method name="*" isolation="DEFAULT"/>
        </tx:attributes>
    </tx:advice>

    <!-- 3. 配置aop增強-->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.itcast.service.impl.*ServiceImpl.*(..))"/>
    </aop:config>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章