接上一個博文(http://www.loveweir.com/html/18.html),沒有數據庫鏈接池,純粹用jpa的官方連接。html
因此此次要加上鍊接池本文用Druid鏈接池來實現多數據源的配置。java
persistence.xml 這個文件能夠省略了,所有配置在applicationContext.xml 裏面:mysql
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.2.xsd"> <context:annotation-config/> <context:component-scan base-package="com.tw"/> <!-- mysql數據源配置 --> <bean id="mysqlDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 驅動名稱 --> <property name="DriverClassName" value="com.mysql.jdbc.Driver" /> <!-- JDBC鏈接串 --> <property name="url" value="jdbc:mysql://192.168.132.1:3306/twq?useUnicode=true&characterEncoding=UTF-8" /> <!-- 數據庫用戶名稱 --> <property name="username" value="ws" /> <!-- 數據庫密碼 --> <property name="password" value="unionmanws" /> <!-- 鏈接池最大使用鏈接數量 --> <property name="maxActive" value="20" /> <!-- 初始化大小 --> <property name="initialSize" value="5" /> <!-- 獲取鏈接最大等待時間 --> <property name="maxWait" value="60000" /> <!-- 鏈接池最小空閒 --> <property name="minIdle" value="2" /> <!-- 逐出鏈接的檢測時間間隔 --> <property name="timeBetweenEvictionRunsMillis" value="3000" /> <!-- 最小逐出時間 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <!-- 測試有效用的SQL Query --> <property name="validationQuery" value="SELECT 'x'" /> <!-- 鏈接空閒時測試是否有效 --> <property name="testWhileIdle" value="true" /> <!-- 獲取鏈接時測試是否有效 --> <property name="testOnBorrow" value="false" /> <!-- 歸還鏈接時是否測試有效 --> <property name="testOnReturn" value="false" /> </bean> <!-- 整合mysqljpa --> <bean id="mysqlEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="mysqlDataSource"></property> <property name="packagesToScan" value="com.tw.entity.sys"></property> <property name="persistenceUnitName" value="mysqldb"></property> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="showSql" value="true"></property> </bean> </property> <property name="jpaProperties"> <props> <!--設置外鏈接抓取樹的最大深度 --> <prop key="hibernate.max_fetch_depth">3</prop> <prop key="hibernate.jdbc.fetch_size">18</prop> <prop key="hibernate.jdbc.batch_size">10</prop> <!-- 自動建表類型 validate|create|create-drop|update --> <!-- <prop key="hibernate.hbm2ddl.auto">validate</prop> --> <!-- 是否顯示SQL --> <prop key="hibernate.show_sql">false</prop> <!-- 顯示SQL是否格式化 --> <prop key="hibernate.format_sql">false</prop> <!-- 關閉二級緩存 --> <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop> <!-- 關閉實體字段映射校驗 --> <prop key="javax.persistence.validation.mode">none</prop> </props> </property> </bean> <bean id="mysqltransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="mysqlEntityManagerFactory" /> <qualifier value="mysqlEM"/> </bean> <tx:annotation-driven transaction-manager="mysqltransactionManager" proxy-target-class="false"/> <!-- sqlserver數據源配置 --> <bean id="sqlserverDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 驅動名稱 --> <property name="DriverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /> <!-- JDBC鏈接串 --> <property name="url" value="jdbc:sqlserver://192.168.130.10:1433;DatabaseName=unionman" /> <!-- 數據庫用戶名稱 --> <property name="username" value="sa" /> <!-- 數據庫密碼 --> <property name="password" value="123abc" /> <!-- 鏈接池最大使用鏈接數量 --> <property name="maxActive" value="20" /> <!-- 初始化大小 --> <property name="initialSize" value="5" /> <!-- 獲取鏈接最大等待時間 --> <property name="maxWait" value="60000" /> <!-- 鏈接池最小空閒 --> <property name="minIdle" value="2" /> <!-- 逐出鏈接的檢測時間間隔 --> <property name="timeBetweenEvictionRunsMillis" value="3000" /> <!-- 最小逐出時間 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <!-- 測試有效用的SQL Query --> <property name="validationQuery" value="SELECT 'x'" /> <!-- 鏈接空閒時測試是否有效 --> <property name="testWhileIdle" value="true" /> <!-- 獲取鏈接時測試是否有效 --> <property name="testOnBorrow" value="false" /> <!-- 歸還鏈接時是否測試有效 --> <property name="testOnReturn" value="false" /> </bean> <!-- 整合sqlserverjpa --> <bean id="sqlserverEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="sqlserverDataSource"></property> <property name="packagesToScan" value="com.tw.entity.plan"></property> <property name="persistenceUnitName" value="sqlserverdb"></property> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="showSql" value="true"></property> </bean> </property> <property name="jpaProperties"> <props> <!--設置外鏈接抓取樹的最大深度 --> <prop key="hibernate.max_fetch_depth">3</prop> <prop key="hibernate.jdbc.fetch_size">18</prop> <prop key="hibernate.jdbc.batch_size">10</prop> <!-- 自動建表類型 validate|create|create-drop|update --> <!-- <prop key="hibernate.hbm2ddl.auto">validate</prop> --> <!-- 是否顯示SQL --> <prop key="hibernate.show_sql">false</prop> <!-- 顯示SQL是否格式化 --> <prop key="hibernate.format_sql">false</prop> <!-- 關閉二級緩存 --> <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop> <!-- 關閉實體字段映射校驗 --> <prop key="javax.persistence.validation.mode">none</prop> </props> </property> </bean> <bean id="sqlservertransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="sqlserverEntityManagerFactory" /> <qualifier value="sqlserverEM"/> </bean> <tx:annotation-driven transaction-manager="sqlservertransactionManager" proxy-target-class="false"/> </beans>
其餘不須要變更,這樣就ok。spring