spring 4 + jpa(hibernate 3/4) + spring mvc 多数据源配置(二)+Druid连接池

spring 4 + jpa(hibernate 3/4) + spring mvc 多数据源配置(二)+Druid连接池

接上一个博文(http://www.loveweir.com/html/18.html),没有数据库连接池,纯粹用jpa的官方链接。

所以这次要加上连接池本文用Druid连接池来实现多数据源的配置。

persistence.xml 这个文件可以省略了,全部配置在applicationContext.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: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&amp;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 4 + jpa(hibernate 3/4) + spring mvc 多数据源配置(二)+Druid连接池)