zoukankan      html  css  js  c++  java
  • 数据库分库

    1、在applicationContext.xml中定义多个dataSource

    一、配置第一个dataSource

    1、第一个dataSource配置:

    <bean id="dataSource1" class="DataSource" p:driver="${hibernate.connection.driver_class}" p:driverUrl="${hibernate.connection.url}" p:user="${hibernate.connection.username}"
    p:password="${hibernate.connection.password}" p:alias="${proxool.pool_alias}" p:houseKeepingTestSql="${proxool.house-keeping-test-sql}" p:testBeforeUse="${proxool.test-before-use}"
    p:maximumConnectionCount="${proxool.maximum-connection-count}" p:minimumConnectionCount="${proxool.minimum-connection-count}" p:prototypeCount="${proxool.prototype-count}"
    p:trace="true" />

    2、第一个sessionFactory

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" p:dataSource-ref="dataSource1">
    <property name="configLocations">
    <list>
    <value>classpath:hibernate.cfg.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
    <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
    <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
    <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
    <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
    <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
    <prop key="hibernate.connection.autocommit">false</prop>
    <prop key="hibernate.connection.release_mode">after_transaction</prop>
    </props>
    </property>
    <property name="eventListeners">
    <map>
    <entry key="merge">
    <bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener" />
    </entry>
    </map>
    </property>
    <property name="entityInterceptor" ref="abaseEntityInterceptor" />
    </bean>

    3、配置template

    <bean id="pageHibernateTemplate" class=" com.extend.hibernate.template.PageHibernateTemplate"><property name="sessionFactory" ref="sessionFactory"/></bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource1"/>

    配置第二个dataSource

    1、配置第二个dataSource

    <bean id="dataSource2" class="DataSource" p:driver="${hibernate.connection.driver_class}" p:driverUrl="${hibernate.connection.url.test}" p:user="${hibernate.connection.username.test}"
    p:password="${hibernate.connection.password.test}" p:alias="${proxool.pool_alias.test}" p:houseKeepingTestSql="${proxool.house-keeping-test-sql}" p:testBeforeUse="${proxool.test-before-use}"
    p:maximumConnectionCount="${proxool.maximum-connection-count-test}" p:minimumConnectionCount="${proxool.minimum-connection-count-test}" p:prototypeCount="${proxool.prototype-count-test}"
    p:trace="true" />

    2、配置第二个sessionFactory

    <bean id="testSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" p:dataSource-ref="dataSource2">
    <property name="mappingLocations">
    <list>
    <value>classpath*:com/test/module/*/*.hbm.xml</value>
    <value>classpath*:hibernate/*.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
    <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
    <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
    <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
    <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
    <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
    <prop key="hibernate.connection.autocommit">false</prop>
    <prop key="hibernate.connection.release_mode">after_transaction</prop>
    <prop key="javax.persistence.validation.mode">none</prop>
    </props>
    </property>
    <property name="eventListeners">
    <map>
    <entry key="merge">
    <bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener" />
    </entry>
    </map>
    </property>
    <property name="entityInterceptor" ref="abaseEntityInterceptor" />
    </bean>

    3、配置template

    <bean id="testPageHibernateTemplate" class="com..extend.hibernate.template.PageHibernateTemplate">
    <property name="sessionFactory" ref="testSessionFactory" />
    </bean>

    <bean id="testJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource2" />

    二、使用数据源

    使用第一个数据源:

    第一个数据源的template配置的名字为:

    <bean id="pageHibernateTemplate" class=" com.extend.hibernate.template.PageHibernateTemplate"><property name="sessionFactory" ref="sessionFactory"/></bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource1"/>

    所以使用的时候,如下:

    <bean autowire="byName" class="com.test.test1.extend.impl.log.dao.OperationLogDaoImpl" id="operationLogDao"/>

    因为是按名字注入的,OperationLogDaoImpl的父类里面有jdbcTemplate或者pageHibernateTemplate属性,并且有注入方法,所以按照上面这种做法是可以的。

    使用第二个数据源:

    <bean class="com.test.ressync.dao.hahaDataJdbcDaoImpl" id="hahaDataJdbcDao" p:jdbcTemplate-ref="testJdbcTemplate" p:dataSource-ref="dataSource2"/>

  • 相关阅读:
    ASP.Net设计时需要考虑的性能优化问题 转载自http://blog.sina.com.cn/s/blog_3d7bed650100055p.html
    Jqeruy dropdownlist 联动
    Sound Recording in Windows Phone 7
    Windows Phone Performance 系列网址集合
    【xml]: Read XML with Namespace resolution using XLinq.XElement
    编程基础字符篇(2)
    每日总结一:
    每日总结5:
    Control usage: (1) Windows Phone 7: Popup control
    编程基础字符篇(3)
  • 原文地址:https://www.cnblogs.com/man-li/p/6405644.html
Copyright © 2011-2022 走看看