hibernate怎么 连接两个数据源

解决方案 »

  1.   

    hibernate.cfg.xml 就是配置数据源的几个数据源就有几个配置文件
      

  2.   

    <hibernate-configuration>
    <session-factory>
    <!-- database -->
    <property name="connection.datasource">
    java:comp/env/jdbc/mysql
    </property>
    <property name="show_sql">
    true
    </property>
    <property name="dialect">
    net.sf.hibernate.dialect.MySQLDialect
    </property>
    <!-- *.hbm.xml register here -->
    <mapping resource="cn/com/chengang/sms/model/model.hbm.xml"/>
    </session-factory>
    </hibernate-configuration>public class HibernateUtil { private static Log log = LogFactory.getLog(HibernateUtil.class); private static final SessionFactory sessionFactory; static {
    try {
    // Create the SessionFactory
    sessionFactory = new Configuration().configure().buildSessionFactory();
    } catch (Throwable ex) {
    log.error("Initial SessionFactory creation failed.", ex);
    throw new ExceptionInInitializerError(ex);
    }
    } public static final ThreadLocal session = new ThreadLocal(); public static Session currentSession() throws HibernateException {
    Session s = (Session) session.get();
    // Open a new Session, if this Thread has none yet
    if (s == null||!s.isOpen()) {
    s = sessionFactory.openSession();
    session.set(s);
    }
    return s;
    } public static void closeSession() throws HibernateException {
    Session s = (Session) session.get();
    session.set(null);
    if (s != null)
    s.close();
    }
    }这个是陈刚的代码,,要配置两个数据源  要怎么改??
      

  3.   

    加载不同的配置文件
    xml1,xml2代表两个数据源配置文件sessionFactory1 = new Configuration().configure("xml1").buildSessionFactory();
    sessionFactory1 = new Configuration().configure("xml2").buildSessionFactory();
      

  4.   

    关注啊!!!
    interpb(曾曾胡,深怕情多累美人!!!) ( ) 信誉:105 
    我有改成像你贴出来的这样但还是找不到xml1或xml2文件呀
    xml1或xml2文件存放目录是放在哪里?