您的位置:首页 > 编程语言 > Java开发

spring 集成hibernate 连接多数据库配置

2016-08-31 00:00 597 查看
定义一个辅助类:BaseHibernateTemplates

详细内容如下:

import java.util.Map;

//import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class BaseHibernateTemplates {

private Map<String,HibernateTemplate> hibernateTemplateMap;

public Map<String, HibernateTemplate> getHibernateTemplateMap() {
return hibernateTemplateMap;
}

public void setHibernateTemplateMap(
Map<String, HibernateTemplate> hibernateTemplateMap) {
this.hibernateTemplateMap = hibernateTemplateMap;
}

}

对于spring连接各个数据库的相关配置:

spring-orm-base.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 基础框架数据库 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.jdbcUrl}" />
<property name="user" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
<!-- 指定连接池中保留的最大连接数. Default:15 -->
<property name="maxPoolSize" value="${jdbc.maxPoolSize}" />
<!-- 指定连接池中保留的最小连接数 -->
<property name="minPoolSize" value="${jdbc.minPoolSize}" />
<!-- 指定连接池的初始化连接数 取值应在minPoolSize 与 maxPoolSize 之间.Default:3 -->
<property name="initialPoolSize" value="${jdbc.initialPoolSize}" />
<!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。 Default:0 -->
<property name="maxIdleTime" value="${jdbc.maxIdleTime}" />
<!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数. Default:3 -->
<property name="acquireIncrement" value="${jdbc.acquireIncrement}" />
<!-- JDBC的标准,用以控制数据源内加载的PreparedStatements数量。 但由于预缓存的statements属于单个connection而不是整个连接池所以设置这个参数需要考虑到多方面的因数.如果maxStatements与maxSt
7ff0
atementsPerConnection均为0,则缓存被关闭。Default:0 -->
<property name="maxStatements" value="${jdbc.maxStatements}" />
<!-- 每60秒检查所有连接池中的空闲连接.Default:0 -->
<property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<!-- hibernate.cache.use_second_level_cache=true hibernate.cache.use_query_cache=true -->
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.cache.region.factory_class=net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
hibernate.jdbc.fetch_size=10
hibernate.jdbc.batch_size=10
</value>
</property>
<property name="packagesToScan" value="com.htjf.*.entity" /><!-- com.htjf.* -->
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
<property name="cacheQueries" value="true" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>

spring-orm-baselh.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 基础框架数据库 -->
<bean id="lh_dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.baselh.driverClass}" />
<property name="jdbcUrl" value="${jdbc.baselh.jdbcUrl}" />
<property name="user" value="${jdbc.baselh.user}" />
<property name="password" value="${jdbc.baselh.password}" />
<!-- 指定连接池中保留的最大连接数. Default:15 -->
<property name="maxPoolSize" value="${jdbc.maxPoolSize}" />
<!-- 指定连接池中保留的最小连接数 -->
<property name="minPoolSize" value="${jdbc.minPoolSize}" />
<!-- 指定连接池的初始化连接数 取值应在minPoolSize 与 maxPoolSize 之间.Default:3 -->
<property name="initialPoolSize" value="${jdbc.initialPoolSize}" />
<!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。 Default:0 -->
<property name="maxIdleTime" value="${jdbc.maxIdleTime}" />
<!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数. Default:3 -->
<property name="acquireIncrement" value="${jdbc.acquireIncrement}" />
<!-- JDBC的标准,用以控制数据源内加载的PreparedStatements数量。 但由于预缓存的statements属于单个connection而不是整个连接池所以设置这个参数需要考虑到多方面的因数.如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default:0 -->
<property name="maxStatements" value="${jdbc.maxStatements}" />
<!-- 每60秒检查所有连接池中的空闲连接.Default:0 -->
<property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}" />
</bean>
<bean id="lh_sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="lh_dataSource" />
<property name="hibernateProperties">
<!-- hibernate.cache.use_second_level_cache=true hibernate.cache.use_query_cache=true -->
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.cache.region.factory_class=net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
hibernate.jdbc.fetch_size=10
hibernate.jdbc.batch_size=10
</value>
</property>
<property name="packagesToScan" value="com.htjf.*.lhentity" /><!-- com.htjf.* -->
</bean>
<bean id="lh_hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="lh_sessionFactory" />
<property name="cacheQueries" value="true" />
</bean>
<bean id="lh_transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="lh_sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="lh_transactionManager" />
</beans>

其他数据库连接同上类似

在spring-orm.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<import resource="classpath:spring/spring-orm-base.xml" />
<import resource="classpath:spring/spring-orm-report.xml" />
<import resource="classpath:spring/spring-orm-baselh.xml" />
<import resource="classpath:spring/spring-orm-reportlh.xml" />
<!-- <import resource="classpath:spring/spring-orm-quality.xml" /> -->
<import resource="classpath:spring/spring-orm-voice.xml" />
<bean id="baseHibernateTemplates"
class="com.htjf.dao.impl.BaseHibernateTemplates">
<property name="hibernateTemplateMap">
<map>
<entry key="base"><ref bean="hibernateTemplate"></ref></entry>
<entry key="report"><ref bean="r_hibernateTemplate"></ref></entry>
<entry key="baselh"><ref bean="lh_hibernateTemplate"></ref></entry>
<entry key="reportlh"><ref bean="rlh_hibernateTemplate"></ref></entry>
<!-- <entry key="quality"><ref bean="q_hibernateTemplate"></ref></entry> -->
<entry key="speech"><ref bean="v_hibernateTemplate"></ref></entry>
</map>
</property>
</bean>
</beans>

该xml通过自动装配辅助类
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: