一、 在src下写两个Hibernate.cfg.xml文件:
如 hbn-MySQL.cfg.xml和hbn-oracle.cfg.xml
配置两个数据的连接。
(1)hbn-MySQL.cfg.xml配置内容
<hibernate-configuration>
<!-- 使用hibernate技术连接本地数据库 -->
<session-factory>
<property name="show_sql">true</property>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/base</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- -->
<mapping resource="com/hnlx/entity/Pres.hbm.xml"/>
<mapping resource="com/hnlx/entity/Rain.hbm.xml"/>
<mapping resource="com/hnlx/entity/Stationbase.hbm.xml"/>
<mapping resource="com/hnlx/entity/Temperature.hbm.xml"/>
<mapping resource="com/hnlx/entity/TongjiDayReal.hbm.xml"/>
<mapping resource="com/hnlx/entity/WindMinrelh.hbm.xml"/>
</session-factory>
</hibernate-configuration>
(2)hbn-oracle.cfg.xml配置内容
<hibernate-configuration>
<session-factory>
<!-- 使用hibernate技术远程连接数据库 -->
<property name="show_sql">true</property>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://192.168.0.118:3306/Agriculture</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- -->
<mapping resource="com/hnlx/entity/Pres.hbm.xml"/>
<mapping resource="com/hnlx/entity/Rain.hbm.xml"/>
<mapping resource="com/hnlx/entity/Stationbase.hbm.xml"/>
<mapping resource="com/hnlx/entity/Temperature.hbm.xml"/>
<mapping resource="com/hnlx/entity/TongjiDayReal.hbm.xml"/>
<mapping resource="com/hnlx/entity/WindMinrelh.hbm.xml"/>
</session-factory>
</hibernate-configuration>
二、 分别解析两个数据库文件建立两个SessionFactory
(1)建立SessionFactoryOne
private static SessionFactorysessionFactory;
private HibernateUtil(){
}
static{
Configuration cfg = new Configuration();
cfg.configure("/hibernate.cfg.xml");
sessionFactory = cfg.buildSessionFactory();
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
/**
* 开启一个Session
* @return
*/
public static Session getSession(){
return sessionFactory.openSession();
}
(2)建立SessionFTwo
private static SessionFactorysessionFactory;
private HibernateUtilTwo(){
}
static{
Configuration cfg = new Configuration();
//加载配置的数据库映射
cfg.configure("/Mysql-hibernate.cfg.xml");
sessionFactory = cfg.buildSessionFactory();
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
/**
* 开启session
* @return
*/
public static Session getSession(){
return sessionFactory.openSession();
}
public static void main(String[] args) {
HibernateTest t = new HibernateTest();
t.getTest();
}
public void getTest(){
Session session = null;
try {
session = HibernateUtil.getSession();
String hql = "from Pres";
Query query = session.createQuery(hql);
query.setFirstResult(0);
query.setMaxResults(30);
List<Pres> list = query.list();
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getStationnum());
}
} catch (Exception e) {
e.printStackTrace();
}
session.close();
}
三、 测试两个数据库连接
public static void main(String[] args) {
HibernateTest t = new HibernateTest();
t.getTest();
}
public void getTest(){
Session session = null;
try {
session = HibernateUtil.getSession();
String hql = "from Pres";
Query query = session.createQuery(hql);
query.setFirstResult(0);
query.setMaxResults(30);
List<Pres> list = query.list();
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getStationnum());
}
} catch (Exception e) {
e.printStackTrace();
}
session.close();
}