使用Hibernate连接多个数据库

一、 src下写两个Hibernate.cfg.xml文件:

  hbn-MySQL.cfg.xmlhbn-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();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值