日期:2014-05-16  浏览次数:20434 次

hibernate中配置多个数据库
1
创建多个hibernate.cfg.xml文件名字要不一样具体内容如下:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory >
    <!-- 数据库联接配置 -->
    <property name="connection.username">root</property>
    <property name="connection.password">12345</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/xinli?useUnicode=true&amp;characterEncoding=utf-8</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
   <!--显示SQL语句-->
    <property name="show_sql">true</property>
        <!-- 数据表对应的映射文件 -->
        <mapping resource="com/sky/dto/User.hbm.xml" />
        <mapping resource="com/sky/dto/Columnt.hbm.xml" />
        <mapping resource="com/sky/dto/Textcontent.hbm.xml" />
        <mapping resource="com/sky/dto/Product.hbm.xml" />
        <mapping resource="com/sky/dto/ProductType.hbm.xml" />
        <mapping resource="com/sky/dto/News.hbm.xml" />
    </session-factory>
</hibernate-configuration>

现创建2个示例:
hibernate.cfg.xml,hibernate.en.xml

2 在applicationContext.xml里配置hibernate.cfg.xml等,内容如下:

<beans>
<!--定义Hibernate的SessionFactory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocations">
<value>/WEB-INF/hibernate.cfg.xml</value>
</property>
</bean>
<!--定义Hibernate en的SessionFactory-->
<bean id="sessionFactoryen" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocations">
<value>/WEB-INF/hibernate.en.xml</value>
</property>
</bean>
</beans>

3 在spring配置中配置对应的sessionFactory