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

hibernate连接两个数据库(mysql、sqlservrer)

一、(在src下)写两个Hibernate.cfg.xml文件:
            hbn-mysql.cfg.xml和hbn-sqlserver.cfg.xml
二、分别解析上面的两个.cfg.xml文件建两个sessionFactory,
三、使用session时哪个sessionFactory打开的session就能访问哪个数据库。


详细步骤:-----------------------------------------
一、(在src下)建两个Hibernate.cfg.xml文件

(1.)hbn-mysql.cfg.xml的内容:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<!--hibernate配置文件,定义所使用的MySQL数据库的配置信息 -->
<hibernate-configuration>
<session-factory>
<property name="myeclipse.connection.profile">mysql</property>
<property name="connection.url">jdbc:mysql://localhost:3306/dataSource?characterEncoding=utf-8</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.username">root</property>
<property name="connection.password">password</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping
   resource="com/fourstar/starTransport/daomain/orderIdStatus.hbm.xml" />
<mapping
   resource="com/fourstar/starTransport/daomain/freightCompany.hbm.xml" />
</session-factory>
</hibernate-configuration>

(2.)hbn-sqlserver.cfg.xml的内容:

<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="connection.driver_class">
   com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="connection.url">
   jdbc:sqlserver://192.168.2.16:1433; DatabaseName=DB
</property>
<property name="connection.username">root</property>
<property name="connection.password">password</property>
<property name="connection.isolation">2</property>
<property name="dialect">
   org.hibernate.dialect.SQLServerDialect
</property>
<mapping
   resource="com/fourstar/starTransport/daomain/transactions.hbm.xml" />
   <mapping
   resource="com/fourstar/starTransport/daomain/transactionDetail.hbm.xml" />
</session-factory>
</hibernate-configuration>


二、建一个类(HbnUtil)用于连接数据库(用于创建sessionFactory和openSession()静态方法)

package com.fourstar.starTransport.dao.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HbnUtil {// 根据hibernate.cfg.xml创建一个静态sessionFactory

private static SessionFactory sf;
private static SessionFactory MSsf;
static {
   sf = new Configuration().configure("/hbn-sqlserver.cfg.xml")
     .buildSessionFactory();
   MSsf = new Configuration().configure("/hbn-mysql.cfg.xml")
   .buildSessionFactory();
}

/**根据DBName判断调用哪个sessionFactory的openSession()方法*/
public static Session getSessionByDB(String DBName) {
   Session s = null;

   if (DBName == "mysql") {
    if (!MSsf.isClosed())
     s = MSsf.openSession();
   } else if (DBName == "sqlserver") {
    if (!sf.isClosed())
     s = sf.openSession();
   } else {
    System.out.println("错误的 DBName!");
   }
   return s;
}
/**根据DBName判断调用哪个sessionFactory的close()方法*/
public static void closeSessionFactoryByDB(String DBName) {
   if (DBName == "mysql") {
    if (!MSsf.isClosed()) {
     MSsf.close();
    }
   } else if (DBName == "sqlserver") {
    if (!sf.isClosed()) {
     sf.close();
    }
   } else {
    Syst