`
陈谏辉
  • 浏览: 48113 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

Spring + Hibernate 实现多数据库链接

阅读更多

1、配置数据库文件dbconfig.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/mallDBE?useUnicode=true&characterEncoding=utf-8
jdbc.urlCenter=jdbc:mysql://localhost/mallDB?useUnicode=true&characterEncoding=utf-8
#jdbc.url=jdbc:mysql://220.231.152.176:3418/mallDBE?useUnicode=true&characterEncoding=utf-8
jdbc.username=dingDang
jdbc.password=dingdang
#jdbc.username=mall
#jdbc.password=d4#2dk*sd
jdbc.minConnections=10
jdbc.maxConnections=300
#jdbc.driverClassName=net.sourceforge.jtds.jdbc.Driver
#jdbc.url=jdbc:jtds:sqlserver://192.168.16.226:1433/dingDang;tds=8.0;lastupdatecount=true

hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.cache.use_second_level_cache=true
#hibernate.dialect=com.dingDang.mall.dao.dialect.SQLServerDialectInDingDang
#messageSource
messageSource.cacheSeconds=-1

2、依赖注入配置信息spring-resources.xml

<!-- Default DBCP datasource -->
<bean id="dbcpDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" lazy-init="false">
<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="${jdbc.minConnections}"/>
<property name="maxActive" value="${jdbc.maxConnections}"/>
<property name="maxIdle" value="100"/>
<property name="minIdle" value="${jdbc.minConnections}"/>
<property name="maxWait" value="5000"/>
<property name="defaultAutoCommit" value="true"/>
<property name="removeAbandoned" value="true"/>
<property name="removeAbandonedTimeout" value="300"/>
<property name="logAbandoned" value="false"/>
<property name="poolPreparedStatements" value="true"/>
<property name="testWhileIdle" value="true"/>
<property name="validationQuery" value="select count(*) from app_role where 1=0"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy" lazy-init="false">
<property name="targetDataSource" ref="dbcpDataSource" />
</bean>
<!-- the center of user database control -->
<bean id="ct_DbcpDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" lazy-init="false">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.urlCenter}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="initialSize" value="${jdbc.minConnections}"/>
<property name="maxActive" value="${jdbc.maxConnections}"/>
<property name="maxIdle" value="100"/>
<property name="minIdle" value="${jdbc.minConnections}"/>
<property name="maxWait" value="5000"/>
<property name="defaultAutoCommit" value="true"/>
<property name="removeAbandoned" value="true"/>
<property name="removeAbandonedTimeout" value="300"/>
<property name="logAbandoned" value="false"/>
<property name="poolPreparedStatements" value="true"/>
<property name="testWhileIdle" value="true"/>
<property name="validationQuery" value="select count(*) from app_role where 1=0"/>
</bean>
<bean id="ct_DataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy" lazy-init="false">
<property name="targetDataSource" ref="ct_DbcpDataSource" />
</bean>

3、hibernate配置文件spring-hibernate.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="com.dingDang.mall.core.hibernate.LocalSessionFactoryBean" lazy-init="false">
<property name="dataSource" ref="dataSource" />
<property name="lobHandler">
<bean class="org.springframework.jdbc.support.lob.DefaultLobHandler" lazy-init="true" />
</property>
<!-- I tried cacheableMappingLocations but it turned out to be more slow. -->
<property name="mappingLocations">
<list>
<value>classpath*:com/dingDang/mall/**/*.hbm.xml</value>
<!-- <value>classpath*:org/jbpm/**/*.hbm.xml</value> -->
</list>
</property>
<!-- Create/update the database tables automatically when the JVM starts up -->
<!-- <property name="schemaUpdate" value="true"/> -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.default_batch_fetch_size">10</prop>
<prop key="hibernate.jdbc.batch_versioned_data">true</prop>
<prop key="hibernate.jdbc.fetch_size">10</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.default_entity_mode">pojo</prop>
<prop key="hibernate.generate_statistics">false</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
<prop key="net.sf.ehcache.configurationResourceName">conf/ehcache.xml</prop>
<!-- Note, you can turn off hibernate caching by setting it to be false -->
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
</props>
</property>
<property name="entityCacheStrategies">
<props>
<prop key="com.dingDang.mall.common.model.system.Membership">nonstrict-read-write</prop>
<prop key="com.dingDang.mall.common.model.catalog.Attribute">nonstrict-read-write</prop>
<prop key="com.dingDang.mall.common.model.system.MetricType">nonstrict-read-write</prop>
<prop key="com.dingDang.mall.common.model.system.MetricUnit">nonstrict-read-write</prop>
<prop key="com.dingDang.mall.common.model.catalog.Department">nonstrict-read-write</prop>
<prop key="com.dingDang.mall.common.model.catalog.Manufacturer">nonstrict-read-write</prop>
<prop key="com.dingDang.mall.common.model.sales.CouponType">nonstrict-read-write</prop>
<prop key="com.dingDang.mall.common.model.sales.RecommendedType">nonstrict-read-write</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<property name="defaultTimeout" value="30" />
</bean>
<!-- Hibernate SessionFactory -->
<bean id="Ct_SessionFactory" class="com.dingDang.mall.core.hibernate.LocalSessionFactoryBean" lazy-init="false">
<property name="dataSource" ref="ct_DataSource" />
<property name="lobHandler">
<bean class="org.springframework.jdbc.support.lob.DefaultLobHandler" lazy-init="true" />
</property>
<property name="mappingLocations">
<list><value>classpath*:outSystem/mytest/*.hbm.xml</value></list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.default_batch_fetch_size">10</prop>
<prop key="hibernate.jdbc.batch_versioned_data">true</prop>
<prop key="hibernate.jdbc.fetch_size">10</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.default_entity_mode">pojo</prop>
<prop key="hibernate.generate_statistics">false</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
<prop key="net.sf.ehcache.configurationResourceName">conf/ehcache.xml</prop>
<!-- Note, you can turn off hibernate caching by setting it to be false -->
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
</props>
</property>
</bean>
<bean id="ct_TransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="Ct_SessionFactory" />
<property name="defaultTimeout" value="30" />
</bean>
</beans>

5、配置sprinDAO文件spring-dao.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans default-lazy-init="true">
<!-- create two Hibernate based DAOs , that's refer two database -->
<bean id="hibernateGenericDao" class="com.dingDang.mall.core.dao.impl.HibernateGenericDaoImpl" abstract="true">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="ct_HibernateGenericDao" class="com.dingDang.mall.core.dao.impl.HibernateGenericDaoImpl" abstract="true">
<property name="sessionFactory" ref="Ct_SessionFactory" />
</bean>
<!-- Add new DAOs here -->
<!-- ========================= Start of Normal DAO DEFINITIONS ========================= -->
<!-- Note that all must extend the base definition of genericDAO.-->
<bean id="userDAO" class="com.dingDang.mall.system.dao.impl.UserDAOImpl" parent="hibernateGenericDao"/>

<bean id="testDAO" class="com.dingDang.mall.system.dao.impl.TestDAOImpl" parent="ct_hibernateGenericDao"/>

</bean><!-- 注意不同的数据库对应不同的parent DAO -->

6、配置springManager文件spring-manager.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans default-lazy-init="true">
<!-- Transaction template for all Managers -->
<bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="set*">PROPAGATION_NOT_SUPPORTED,readOnly</prop>
<prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop>
<prop key="is*">PROPAGATION_SUPPORTS,readOnly</prop>
<prop key="find*">PROPAGATION_SUPPORTS,readOnly</prop>
<prop key="search*">PROPAGATION_SUPPORTS,readOnly</prop>
<prop key="load*">PROPAGATION_SUPPORTS,readOnly</prop>
<prop key="save*">PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED</prop>
<prop key="update*">PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED</prop>
<prop key="create*">PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED</prop>
<prop key="add*">PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED</prop>
<prop key="remove*">PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED</prop>
<prop key="delete*">PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED</prop>
<prop key="do*">PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED</prop>
<prop key="*">PROPAGATION_SUPPORTS</prop>
</props>
</property>
</bean>
<bean id="ct_TxProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="ct_TransactionManager" />
<property name="transactionAttributes">
<props>
<prop key="set*">PROPAGATION_NOT_SUPPORTED,readOnly</prop>
<prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop>
<prop key="is*">PROPAGATION_SUPPORTS,readOnly</prop>
<prop key="find*">PROPAGATION_SUPPORTS,readOnly</prop>
<prop key="search*">PROPAGATION_SUPPORTS,readOnly</prop>
<prop key="load*">PROPAGATION_SUPPORTS,readOnly</prop>
<prop key="save*">PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED</prop>
<prop key="update*">PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED</prop>
<prop key="create*">PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED</prop>
<prop key="add*">PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED</prop>
<prop key="remove*">PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED</prop>
<prop key="delete*">PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED</prop>
<prop key="do*">PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED</prop>
<prop key="*">PROPAGATION_SUPPORTS</prop>
</props>
</property>
</bean>

</bean><!-- 不同数据库的manager对应不同的 managerbase -->

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics