1. 程式人生 > >Spring4.2+SpringMVC+Mybatis3.4的整合

Spring4.2+SpringMVC+Mybatis3.4的整合

<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:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
 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/util 
  http://www.springframework.org/schema/util/spring-util.xsd">
 <context:annotation-config />
 <!-- 掃描service,dao -->
 <context:component-scan base-package="com.sm">
  <context:exclude-filter type="regex"
   expression="com.sm.controller" />
 </context:component-scan>
 <!-- ////////////////////////配置資料來源///////////////////////// -->
 <!-- 把.properties檔案引入xml檔案中 -->




 <!-- 第一種方式,比較方便 -->
 <!-- <util:properties location="classpath*:conf/jdbc.properties" /> -->
 <!-- 第二中方式 -->
 <!-- <context:property-placeholder location="classpath*:conf/jdbc.properties"/> -->
 <!-- 第三種方式 ,稍複雜點 -->
 <bean id="propertyConfigurer"
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations" value="classpath*:conf/jdbc.properties" />
  <!-- <property name="locations"> <list> <value>classpath*:conf/jdbc.properties</value> 
   </list> </property> -->
 </bean>




 <!-- 將Mybatis的DataSource,sessionFactory以及Transaction受控於Spring容器 -->
 <!-- 資料來源有多種:1,dbcp class="" 2,c3p0, class="" 3,alibaba的Druid(德魯伊)資料來源 class="com.alibaba.druid.pool.DruidDataSource" 
  4,spring class="org.springframework.jdbc.datasource.DriverManagerDataSource" -->
 <bean id="dataSource"
 class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="${jdbc.driverClassName}" />
  <property name="url" value="${jdbc.url}" />
  <property name="username" value="${jdbc.username}" />
  <property name="password" value="${jdbc.password}" />
  <!-- 初始化連線大小 -->
  <property name="initialSize" value="0" />
  <!-- 連線池最大使用連線數量 -->
  <property name="maxActive" value="20" />
  <!-- 連線池最大空閒 -->
  <property name="maxIdle" value="20" />
  <!-- 連線池最小空閒 -->
  <property name="minIdle" value="0" />
  <!-- 獲取連線最大等待時間 -->
  <property name="maxWait" value="60000" />
  <property name="validationQuery" value="${validationQuery}" />
  <property name="testOnBorrow" value="false" />
  <property name="testOnReturn" value="false" />
  <property name="testWhileIdle" value="true" />
  <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連線,單位是毫秒 -->
  <property name="timeBetweenEvictionRunsMillis" value="60000" />
  <!-- 配置一個連線在池中最小生存的時間,單位是毫秒 -->
  <property name="minEvictableIdleTimeMillis" value="25200000" />
  <!-- 開啟removeAbandoned功能 -->
  <property name="removeAbandoned" value="true" />
  <!-- 1800秒,也就是30分鐘 -->
  <property name="removeAbandonedTimeout" value="1800" />
  <!-- 關閉abanded連線時輸出錯誤日誌 -->
  <property name="logAbandoned" value="true" />
  <!-- 監控資料庫 -->
  <property name="filters" value="mergeStat" />
 </bean>
 <!-- ////////////配置Mybatis/////////////////////////////////// -->
 <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <!-- <property name="configLocation" value="classpath*:conf/sqlMapConfig.xml" 
   /> -->
  <property name="mapperLocations">
   <list>
    <value>classpath*:conf/com/dao/mapper/*.map.xml</value>
   </list>
  </property>
 </bean>




 <!-- 配置掃描介面 -->
 <!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> value指明介面所在的包中 
  <property name="basePackage" value="com.sm.dao" /> <property name="sessionFactory" 
  ref="sessionFactory" /> </bean> -->
</beans>