Hibernate访问多个数据库

Hibernate访问多个数据库

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. < hibernate-configuration>
  6.     < session-factory >
  7.   < !-- local connection properties -->
  8.   < property name="hibernate.connection.url">jdbc:mysql://localhost:3306/bookshop?zeroDateTimeBehavior=convertToNull< /property>
  9.   < property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver< /property>
  10.   < property name="hibernate.connection.username">root< /property>
  11.   < property name="hibernate.connection.password">12345678< /property>
  12.   < !-- property name="hibernate.connection.pool_size">< /property -->
  13.   < !-- dialect for MySQL -->
  14.         < property name="dialect">org.hibernate.dialect.MySQLDialect< /property>
  15.         < property name="hibernate.show_sql">true< /property>
  16.         < property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory< /property>        
  17.         < property name="hbm2ddl.auto">update< /property>
  18.      < mapping resource="org/jskyme/data/local/po/Shop.hbm.xml"/>
  19.     < /session-factory>
  20. < /hibernate-configuration>

data_server.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. < hibernate-configuration>
  6.     < session-factory >
  7.   < !-- local connection properties -->
  8.   < property name="hibernate.connection.url">jdbc:mysql://192.168.0.10:3306/bookshop?zeroDateTimeBehavior=convertToNull< /property>
  9.   < property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver< /property>
  10.   < property name="hibernate.connection.username">root< /property>
  11.   < property name="hibernate.connection.password">12345678< /property>
  12.   < !-- property name="hibernate.connection.pool_size">< /property -->
  13.   < !-- dialect for MySQL -->
  14.         < property name="dialect">org.hibernate.dialect.MySQLDialect< /property>
  15.         < property name="hibernate.show_sql">true< /property>
  16.         < property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory< /property>        
  17.         < property name="hbm2ddl.auto">update< /property>
  18.      < mapping resource="org/jskyme/data/local/po/Shop.hbm.xml"/>
  19.     < /session-factory>
  20. < /hibernate-configuration>

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

数据库管理类:DataBaseManager

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

Hibernate连接数据库操作类:

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

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

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

你可能感兴趣的:(Hibernate访问多个数据库)