日期:2014-05-16 浏览次数:20471 次
<?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>
<?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>
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