Hibernate访问多个数据库

开发 后端
本文描述Hibernate访问多个数据库的操作步骤。思路就是,利用Hibernate可以加载不同数据库配置信息的原理,编写一个数据库操作类,再编写一个数据库管理程序[map],将加载的数据库连接实例put早数据库管理程序中。

Hibernate访问多个数据库的设计思路:利用 Hibernate中config = new Configuration().configure(configFile);可以加载不同数据库配置信息的原理,编写一个数据库操作类,再编写一个数据库管理程序[map],将加载的数据库连接实例put早数据库管理程序中,具体实现见下面:

Hibernate访问多个数据库步骤一:hibernate配置文件

localhost.cfg.xml

  1. < ?xml version="1.0" encoding="utf-8"?> 
  2. < !DOCTYPE hibernate-configuration  
  3.     PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"  
  4.     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
  5.  
  6. < hibernate-configuration> 
  7.     < session-factory > 
  8.  
  9.   < !-- local connection properties --> 
  10.   < property name="hibernate.connection.url">jdbc:mysql://localhost:3306/bookshop?zeroDateTimeBehavior=convertToNull< /property> 
  11.   < property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver< /property> 
  12.   < property name="hibernate.connection.username">root< /property> 
  13.   < property name="hibernate.connection.password">12345678< /property> 
  14.   < !-- property name="hibernate.connection.pool_size">< /property --> 
  15.  
  16.   < !-- dialect for MySQL --> 
  17.         < property name="dialect">org.hibernate.dialect.MySQLDialect< /property> 
  18.  
  19.         < property name="hibernate.show_sql">true< /property> 
  20.         < property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory< /property>          
  21.         < property name="hbm2ddl.auto">update< /property> 
  22.  
  23.      < mapping resource="org/jskyme/data/local/po/Shop.hbm.xml"/> 
  24.     < /session-factory> 
  25. < /hibernate-configuration> 

data_server.cfg.xml

  1. < ?xml version="1.0" encoding="utf-8"?> 
  2.  
  3. < !DOCTYPE hibernate-configuration  
  4.  
  5.     PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"  
  6.  
  7.     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
  8.  
  9. < hibernate-configuration> 
  10.  
  11.     < session-factory > 
  12.  
  13.   < !-- local connection properties --> 
  14.  
  15.   < property name="hibernate.connection.url">jdbc:mysql://192.168.0.10:3306/bookshop?zeroDateTimeBehavior=convertToNull< /property> 
  16.  
  17.   < property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver< /property> 
  18.  
  19.   < property name="hibernate.connection.username">root< /property> 
  20.  
  21.   < property name="hibernate.connection.password">12345678< /property> 
  22.  
  23.   < !-- property name="hibernate.connection.pool_size">< /property --> 
  24.  
  25.   < !-- dialect for MySQL --> 
  26.  
  27.         < property name="dialect">org.hibernate.dialect.MySQLDialect< /property> 
  28.  
  29.         < property name="hibernate.show_sql">true< /property> 
  30.  
  31.         < property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory< /property>          
  32.  
  33.         < property name="hbm2ddl.auto">update< /property> 
  34.  
  35.      < mapping resource="org/jskyme/data/local/po/Shop.hbm.xml"/> 
  36.  
  37.     < /session-factory> 
  38.  
  39. < /hibernate-configuration> 

Hibernate访问多个数据库步骤二:数据库访问类:

数据库管理类:DataBaseManager

  1. package org.jskyme.hibernate.util;  
  2.  
  3. import java.util.HashMap;  
  4.  
  5. public class DataBaseManager extends HashMap {  
  6.  private static final long serialVersionUID = 6491666983237498097L;  
  7.  private static DataBaseManager inst = new DataBaseManager();  
  8.  
  9.  public static DataBaseManager getInst() {  
  10.   return inst;  
  11.  }  
  12.  
  13.  public SessionManager get(Object key) {  
  14.   return (SessionManager) super.get(key);  
  15.  }  
  16.  
  17.  @Override  
  18.  public Object put(Object key, Object value) {  
  19.   return super.put(key, value);  
  20.  }  
  21.  
  22.  public static void setInst(DataBaseManager inst) {  
  23.   DataBaseManager.inst = inst;  
  24.  }  
  25.    
  26. }  

Hibernate连接数据库操作类:

  1. package org.jskyme.hibernate.util;  
  2.  
  3. import java.util.List;  
  4.  
  5. import org.hibernate.Criteria;  
  6. import org.hibernate.Query;  
  7. import org.hibernate.SQLQuery;  
  8. import org.hibernate.Session;  
  9. import org.hibernate.SessionFactory;  
  10. import org.hibernate.Transaction;  
  11. import org.hibernate.cfg.Configuration;  
  12.  
  13. public final class SessionManager {  
  14.  private Configuration config;  
  15.  
  16.  private SessionFactory sessionFactory;  
  17.  
  18.  private Session session;  
  19.  
  20.  public Criteria createCriteria(Class persistentClass) {  
  21.   return session.createCriteria(persistentClass);  
  22.  }  
  23.  
  24.  private void buildSession() {  
  25.   sessionFactory = config.buildSessionFactory();  
  26.   session = sessionFactory.openSession();  
  27.  }  
  28.  
  29.  public SessionManager(String configFile) {  
  30.   config = new Configuration().configure(configFile);  
  31.   buildSession();  
  32.  }  
  33.  
  34.  public Session getSession() {  
  35.   return session;  
  36.  }  
  37.  
  38.  public void save(Object obj) {  
  39.   Transaction tx = session.beginTransaction();  
  40.   session.save(obj);  
  41.   tx.commit();  
  42.  }  
  43.  
  44.  public Object load(Class clas, Integer priId) {  
  45.   return session.get(clas, priId);  
  46.  }  
  47.  
  48.  public Query findbyhql(String hql) {  
  49.   return session.createQuery(hql);  
  50.  }  
  51.  
  52.  public List pageSizeByhql(String hql) {  
  53.   return findbyhql(hql).list();  
  54.  }  
  55.  
  56.  public SQLQuery findbysql(String sql) {  
  57.   return session.createSQLQuery(sql);  
  58.  }  
  59.  
  60.  public void update(Object obj) {  
  61.   Transaction tx = session.beginTransaction();  
  62.   session.saveOrUpdate(obj);  
  63.   tx.commit();  
  64.  }  
  65.  public void delete(Class clas, Integer inte) {  
  66.   session.delete(load(clas, inte));  
  67.  }  
  68.  
  69.  public void delete(Object obj) {  
  70.   session.delete(obj);  
  71.  }  
  72.  
  73.  public void deletebyhql(String hql) {  
  74.   Query query = session.createQuery(hql);  
  75.   query.executeUpdate();  
  76.  }  
  77.  
  78.  public Query createQuery(String hql) {  
  79.   return session.createQuery(hql);  
  80.  }  
  81.  
  82. }  

Hibernate访问多个数据库步骤三:测试类

  1. package org.jskyme.data.test;  
  2.  
  3. import junit.framework.TestCase;  
  4.  
  5. import org.hibernate.Query;  
  6. import org.jskyme.hibernate.util.DataBaseManager;  
  7. import org.jskyme.hibernate.util.SessionManager;  
  8.  
  9. public class DataBaseManagerTest extends TestCase {  
  10.  DataBaseManager dbm = DataBaseManager.getInst();  
  11.  
  12.  public void testDatabase() {  
  13.   setDatabase();  
  14.   SessionManager tempSess = dbm.get("dataLocal");  
  15.   Query query = tempSess.createQuery("from  Shop");  
  16.   query.list();  
  17.     
  18.   SessionManager tempSess27 = dbm.get("dateManage");  
  19.   Query query27 = tempSess27.createQuery("from Shop");  
  20.   query27.list();  
  21.  }  
  22.  
  23.  private void setDatabase() {  
  24.   SessionManager dateManageLocal = new SessionManager("localhost.cfg.xml");  
  25.   SessionManager dateManage27 = new SessionManager("data_server.cfg.xml");  
  26.   dbm.put("dateManage", dateManage27);  
  27.   dbm.put("dataLocal", dateManageLocal);  
  28.  
  29.  }  
  30. }  

【编辑推荐】

  1. Hibernate的lazy属性总结
  2. Hibernate中hbm的generator子元素
  3. 简单理解Hibernate三种状态的概念及互相转化
  4. 操作Hibernate类:增加,删除,修改及查询
  5. 对于选择Hibernate还是iBatis的看法
责任编辑:book05 来源: 百度博客
相关推荐

2009-09-28 13:33:48

Hibernate访问

2010-04-19 10:00:02

Oracle SQL

2009-09-28 13:29:41

加载过程Hibernate访问

2019-08-14 07:59:15

SQLite数据库SQL

2011-03-24 16:01:30

数据库管理

2023-10-29 17:15:57

2009-07-10 16:54:50

Jython安装连接多个数据库jython

2009-09-25 13:18:15

Hibernate数据

2009-09-24 14:12:22

Hibernate数据

2023-12-29 22:39:25

Golang应用程序数据库

2009-06-24 07:53:47

Hibernate数据

2024-01-30 15:29:20

Django数据库Python

2009-06-24 07:58:52

Hibernate多数

2009-06-02 14:36:28

HibernateMySQLEclipse

2013-11-26 09:47:47

ORM

2011-03-16 17:26:22

动态数据库

2009-09-15 10:02:44

Linq to SQL

2011-03-03 11:07:57

Spring数据库访问ORM

2010-05-20 14:52:42

MySQL数据库

2018-07-13 09:20:30

SQLite数据库存储
点赞
收藏

51CTO技术栈公众号