mybatis-config.xml是mybatis配置的核心文件,在此項目中將會被spring-mybatis.xml引用。php
<?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-->
<package name="com.blog.entity" />
</typeAliases>
<plugins>
<plugin interceptor="tk.mybatis.mapper.mapperhelper.MapperInterceptor">
<!--主鍵自增回寫方法,默認值MYSQL,詳細說明請看文檔HSQLDB-->
<property name="IDENTITY" value="MYSQL"/>
<!--可選參數一共3個,對應0,1,2,分別爲SequenceName,ColumnName,PropertyName-->
<property name="seqFormat" value="{0}.nextval"/>
<!--通用Mapper接口,多個通用接口用逗號隔開-->
<property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
</plugin>
</plugins>
</configuration>
複製代碼
spring-mybatis.xml是spring和mybatis的整合配置文件,此配置文件能夠是Spring自動加載Mybatis的SqlSession相關類,並把繁雜的Mybatis會話操做封裝起來了css
<?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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 自動掃描 -->
<context:component-scan base-package="com.blog.service.impl"/>
<!--加載jdbc相關的配置文件-->
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
<property name="fileEncoding" value="UTF-8"/>
</bean>
<!--引用上面的jdbc配置-->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="properties" ref="configProperties"/>
</bean>
<!-- 配置數據源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClass}"/>
<property name="url" value="${jdbcUrl}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<!-- 初始化鏈接大小 -->
<property name="initialSize" value="${initialSize}"/>
<!-- 鏈接池最大數量 -->
<property name="maxActive" value="${maxActive}"/>
<!-- 鏈接池最大空閒 -->
<property name="maxIdle" value="${maxIdle}"/>
<!-- 鏈接池最小空閒 -->
<property name="minIdle" value="${minIdle}"/>
<!-- 獲取鏈接最大等待時間 -->
<property name="maxWait" value="${maxWait}"/>
</bean>
<!-- 整合spring 與 Mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--引用配置好的數據源-->
<property name="dataSource" ref="dataSource"/>
<!-- 配置Mybatis的核心配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
<!-- 自動掃描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:mapping/*.xml"/>
</bean>
<!-- DAO接口所在包名,Spring會自動查找其下的類 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.blog.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!-- 事務管理-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
複製代碼
springMVC-servlet.xml是spring配置的核心文件java
<?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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<mvc:default-servlet-handler />
<!-- 自動掃描 -->
<context:component-scan base-package="com.blog.controller"/>
<mvc:annotation-driven />
<!--視圖配置-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<!-- 配置靜態資源訪問地址-->
<mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/>
<mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/>
<mvc:resources mapping="/css/**" location="/css/" cache-period="31556926"/>
</beans>
複製代碼
web.xml是web容器的配置文件,加載配置文件也是從這個文件開始加載的。web
這裏要注意幾個問題:spring
spring配置文件的加載 在這裏spring配置文件不是自動加載的,而是使用sql
<param-value>classpath:springMVC-servlet.xml</param-value>
複製代碼
來配置,因此spring核心配置文件應該放在resources文件夾中。apache
日誌配置以前要加上spring-mvc
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webApp.root</param-value>
</context-param>
複製代碼
<!--配置mybatis的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:spring-mybatis.xml
</param-value>
</context-param>
複製代碼
來加載。session
完整配置文件:mybatis
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>Blog</display-name>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webApp.root</param-value>
</context-param>
<!--配置日誌-->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>3000</param-value>
</context-param>
<!--配置mybatis的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:spring-mybatis.xml
</param-value>
</context-param>
<!-- 編碼過濾器 解決POST亂碼問題-->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring監聽器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止spring內存溢出監聽器,好比quartz -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--更改Spring配置文件的讀取路徑,Spring配置文件能夠不放到WEB-INF文件夾下-->
<param-value>classpath:springMVC-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<!-- servlet-mapping -->
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 指明對於以下資源文件不採用spring的過濾器 -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.xml</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- session配置, 失效時間爲15分鐘 -->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
複製代碼