1.spring-web.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"
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">
<!-- 配置spring MVC -->
<!-- 1.開啓springmvc註解模式 -->
<mvc:annotation-driven>
<!-- 處理responseBody 裏面日期類型 -->
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
</bean>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 2.靜態資源默認servlet配置
(1)加入對靜態資源的處理:js/gif/png
(2)允許使用"/"做整體映射
-->
<mvc:resources mapping="/**" location="/static/" />
<mvc:default-servlet-handler/>
<!-- 3.定義視圖解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置邏輯視圖前後綴 -->
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 4.掃描web相關的bean -->
<context:component-scan base-package="com.rms"/>
<!-- 配置文件上傳解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 上傳的最大限制 -->
<property name="maxUploadSize" value="209715200" />
<!-- 設置請求編碼格式 -->
<property name="defaultEncoding" value="UTF-8"/>
<!-- 上傳文件的解析 -->
<property name="resolveLazily" value="true" />
</bean>
<!-- 配置登錄攔截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/manage/**"/>
<mvc:exclude-mapping path="/manage/login.html"/>
<mvc:exclude-mapping path="/manage/login"/>
<mvc:exclude-mapping path="/manage/resources/**"/>
<bean class="com.rms.interceptor.ManageLoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
2.spring-mybatis.xml
1.基於mapper.xml文件的配置
此示例使用dbcp連接池。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<!-- 引入 JDBC 配置文件 -->
<context:property-placeholder
location="classpath:properties/jdbc.properties"
ignore-unresolvable="true" />
<!-- 配置數據源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp2.BasicDataSource">
<!-- 驅動類 -->
<property name="driverClassName" value="${jdbc.driver}" />
<!-- 連接地址 -->
<property name="url" value="${jdbc.url}" />
<!-- 用戶名 -->
<property name="username" value="${jdbc.username}" />
<!-- 密碼 -->
<property name="password" value="${jdbc.password}" />
<!-- 連接初始值,連接池啓動時創建的連接數量的初始值,默認值爲 0 -->
<property name="initialSize"
value="${pool.dbcp2.initialSize}" />
<!-- 最小空間值,當空間的連接數少於閾值時,連接池就會去預申請一些連接,以免洪峯來時來不及申請,默認值爲 0 -->
<property name="minIdle" value="${pool.dbcp2.minIdle}" />
<!-- 最大空間值,當經過一個高峯時間後,連接池可以慢慢將已經用不到的連接慢慢釋放一部分,一直減少到 maxIdle 爲止 ,0 時無限制,默認值爲8 -->
<property name="maxIdle" value="${pool.dbcp2.maxIdle}" />
<!-- 連接池的最大值,同一時間可以從池分配的最多連接數量,0 時無限制,默認值爲8 -->
<property name="maxTotal" value="${pool.dbcp2.maxTotal}" />
<!--是否自動提交事務,即單條 SQL 語句算是一個事務 -->
<property name="defaultAutoCommit"
value="${pool.dbcp2.defaultAutoCommit}" />
</bean>
<!-- MyBatis 構建 SqlSessionFactory 的實例配置文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 數據源 -->
<property name="dataSource" ref="dataSource"/>
<!--加載mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
<!-- Mapper 配置文件所在路徑 -->
<property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/>
<!--配置包別名 -->
<property name="typeAliasesPackage" value="${實體類所在包全限名}"/>
</bean>
<!-- MyBatis 自動查找指定包名下的Mapper類,如果找到對應的映射文件,將包下的 interface 動態定義爲 Spring Bean -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="${mapper接口所在包全限名}"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
jdbc.properties文件示例如下:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/rms?tinyInt1isBit=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&allowMultiQueries=true&useSSL=false
jdbc.username=root
jdbc.password=123456
pool.dbcp2.initialSize=3
pool.dbcp2.minIdle=3
pool.dbcp2.maxIdle=5
pool.dbcp2.maxTotal=15
pool.dbcp2.defaultAutoCommit=false
2.基於mapper註解的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: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.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
">
<!--配置Druid連接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/demo?serverTimezone=UTC"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
<property name="initialSize" value="3"></property>
<property name="maxActive" value="5"></property>
</bean>
<!--Spring整合Mybatis-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hd.dao"></property>
</bean>
</beans>