1. 程式人生 > >SSM所需要的配置檔案(一般來說)。

SSM所需要的配置檔案(一般來說)。

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <!-- 配置載入Spring檔案的監聽器-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

	<!-- 編碼過濾器 放在所有過濾器之前 -->
    <filter>
        <filter-name>encoding</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>
        <init-param>
			<param-name>forceRequestEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>forceResponseEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

	<!-- 配置Spring MVC前端核心控制器 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--指定springMvc配置檔案-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-config.xml</param-value>
        </init-param>
        <!-- 配置伺服器啟動後立即載入Spring MVC配置檔案 -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!-- 1:*.do *.action 攔截以.do結尾的請求 (不攔截 jsp png jpg .js .css) 
		2:/ 攔截所有請求 (不攔截.jsp) 建議使用此種 方式 (攔截 .js.css .png) (放行靜態資源) 
		3:/* 攔截所有請求(包括.jsp) 此種方式 不建議使用 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

applicationContext.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.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
        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">
        
    <!--讀取db.properties-->
    <context:property-placeholder location="classpath:db.properties"/>

	<!--配置資料來源-->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <!--資料庫驅動-->
        <property name="driverClassName" value="${jdbc.driver}"/>
        <!--連線資料庫的url-->
        <property name="url" value="${jdbc.url}"/>
        <!--連線資料庫的使用者名稱-->
        <property name="username" value="${jdbc.username}"/>
        <!--連線資料庫的密碼-->
        <property name="password" value="${jdbc.password}"/>
        <!--最大連線數-->
        <property name="maxTotal" value="${jdbc.maxTotal}"/>
        <!--最大空閒數-->
        <property name="maxIdle" value="${jdbc.maxIdle}"/>
        <!--初始化連線數-->
        <property name="initialSize" value="${jdbc.initialSize}"/>
    </bean>

	<!--配置MyBatis工廠SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--注入資料來源-->
        <property name="dataSource" ref="dataSource"/>
        <!--指定mapper檔案的位置-->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
        <!--指定MyBatis的核心配置檔案-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
    <!-- 介面開發,掃描 com.XX.XX.dao包 ,寫在此包下的介面即可被掃描到 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.XX.XX.dao"/>
    </bean>
    
	<!-- 事務管理器,依賴於資料來源-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
        <constructor-arg name="executorType" value="BATCH"/>
    </bean>

    <!--通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!--傳播行為-->
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="create*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <!--切面-->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.XX.XX.service.*.*(..))" id="txPoint"/>
    </aop:config>

	<!-- 配置掃描@Service註解 -->
    <context:component-scan base-package="com.XX.XX.service"/>
</beans>

springmvc的配置檔案 springmvc-config.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.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
    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">
    
    <!-- 載入屬性檔案 -->
    <context:property-placeholder location="classpath:resource.properties"/>
    <!-- 配置掃描器 -->
    <context:component-scan base-package="com.XX.XX.controller"/>
    <!-- 註解驅動:配置處理器對映器和介面卡 -->
    <mvc:annotation-driven/>
    <!--配置靜態資源的訪問對映,此配置中的檔案,將不被前端控制器攔截 -->
    <mvc:resources mapping="/js/**" location="/js/"/>
    <mvc:resources mapping="/css/**" location="/css/"/>
    <mvc:resources mapping="/fonts/**" location="/fonts/"/>
    <mvc:resources mapping="/images/**" location="/images/"/>
    <!-- 配置檢視直譯器ViewResolver -->
    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--配置攔截器-->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <bean class="com.ma.core.interceptor.LoginInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>
</beans>

db.properties檔案

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/資料庫名?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5

mybatis的配置檔案mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- 別名定義 -->
    <settings>
		<setting name="mapUnderscoreToCamelCase" value="true" />
	</settings>
    <typeAliases>
        <package name="com.XX.XX.bean" />
    </typeAliases>
   <!--若用了pagehelper分頁外掛,則需要配置-->
    <plugins>
         <plugin interceptor="com.github.pagehelper.PageHelper">
             <!--指明資料庫 4.0.0以後不需要設定此屬性-->
             <property name="dialect" value="mysql"/>
             <!-- 該引數預設為false -->
             <!-- 設定為true時,會將RowBounds第一個引數offset當成pageNum頁碼使用 -->
             <!-- 和startPage中的pageNum效果一樣-->
             <property name="offsetAsPageNum" value="true"/>
             <!-- 該引數預設為false -->
             <!-- 設定為true時,使用RowBounds分頁會進行count查詢 -->
             <property name="rowBoundsWithCount" value="true"/>
             <!-- 設定為true時,如果pageSize=0或者RowBounds.limit = 0就會查詢出全部的結果 -->
             <!-- (相當於沒有執行分頁查詢,但是返回結果仍然是Page型別)-->
             <property name="pageSizeZero" value="true"/>
             <!-- 3.3.0版本可用 - 分頁引數合理化,預設false禁用 -->
             <!-- 啟用合理化時,如果pageNum<1會查詢第一頁,如果pageNum>pages會查詢最後一頁 -->
             <!-- 禁用合理化時,如果pageNum<1或pageNum>pages會返回空資料 -->
            <property name="reasonable" value="true"/>
             <!-- 3.5.0版本可用 - 為了支援startPage(Object params)方法 -->
             <!-- 增加了一個`params`引數來配置引數對映,用於從Map或ServletRequest中取值 -->
             <!-- 可以配置pageNum,pageSize,count,pageSizeZero,reasonable,orderBy,不配置對映的用預設值 -->
             <!-- 不理解該含義的前提下,不要隨便複製該配置 -->
             <property name="params" value="pageNum=start;pageSize=limit;"/>
             <!-- 支援通過Mapper介面引數來傳遞分頁引數 -->
             <property name="supportMethodsArguments" value="true"/>
             <!-- always總是返回PageInfo型別,check檢查返回型別是否為PageInfo,none返回Page -->
             <property name="returnPageInfo" value="check"/>
         </plugin>
    </plugins>
</configuration>

log4j.properties檔案

# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.ma.core=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n