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

定义一个辅助类: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与maxStatementsPerConnection均为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通过自动装配辅助类 

转载于:https://my.oschina.net/xiaoshoubingliang/blog/740556

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值