本文描述Hibernate访问多个数据库的操作步骤。思路就是,利用Hibernate可以加载不同数据库配置信息的原理,编写一个数据库操作类,再编写一个数据库管理程序[map],将加载的数据库连接实例put早数据库管理程序中。
Hibernate访问多个数据库的设计思路:利用 Hibernate中config = new Configuration().configure(configFile);可以加载不同数据库配置信息的原理,编写一个数据库操作类,再编写一个数据库管理程序[map],将加载的数据库连接实例put早数据库管理程序中,具体实现见下面:
Hibernate访问多个数据库步骤一:hibernate配置文件
localhost.cfg.xml
< ?xml version="1.0" encoding="utf-8"?> < !DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> < hibernate-configuration> < session-factory > < !-- local connection properties --> < property name="hibernate.connection.url">jdbc:mysql://localhost:3306/bookshop?zeroDateTimeBehavior=convertToNull< /property> < property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver< /property> < property name="hibernate.connection.username">root< /property> < property name="hibernate.connection.password">12345678< /property> < !-- property name="hibernate.connection.pool_size">< /property --> < !-- dialect for MySQL --> < property name="dialect">org.hibernate.dialect.MySQLDialect< /property> < property name="hibernate.show_sql">true< /property> < property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory< /property> < property name="hbm2ddl.auto">update< /property> < mapping resource="org/jskyme/data/local/po/Shop.hbm.xml"/> < /session-factory> < /hibernate-configuration>
data_server.cfg.xml
< ?xml version="1.0" encoding="utf-8"?> < !DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> < hibernate-configuration> < session-factory > < !-- local connection properties --> < property name="hibernate.connection.url">jdbc:mysql://192.168.0.10:3306/bookshop?zeroDateTimeBehavior=convertToNull< /property> < property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver< /property> < property name="hibernate.connection.username">root< /property> < property name="hibernate.connection.password">12345678< /property> < !-- property name="hibernate.connection.pool_size">< /property --> < !-- dialect for MySQL --> < property name="dialect">org.hibernate.dialect.MySQLDialect< /property> < property name="hibernate.show_sql">true< /property> < property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory< /property> < property name="hbm2ddl.auto">update< /property> < mapping resource="org/jskyme/data/local/po/Shop.hbm.xml"/> < /session-factory> < /hibernate-configuration>Hibernate访问多个数据库步骤二:数据库访问类:
数据库管理类:DataBaseManager
package org.jskyme.hibernate.util;
import java.util.HashMap;
public class DataBaseManager extends HashMap {
private static final long serialVersionUID = 6491666983237498097L;
private static DataBaseManager inst = new DataBaseManager();
public static DataBaseManager getInst() {
return inst;
}
public SessionManager get(Object key) {
return (SessionManager) super.get(key);
}
@Override
public Object put(Object key, Object value) {
return super.put(key, value);
}
public static void setInst(DataBaseManager inst) {
DataBaseManager.inst = inst;
}
}
Hibernate连接数据库操作类:
package org.jskyme.hibernate.util;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public final class SessionManager {
private Configuration config;
private SessionFactory sessionFactory;
private Session session;
public Criteria createCriteria(Class persistentClass) {
return session.createCriteria(persistentClass);
}
private void buildSession() {
sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
}
public SessionManager(String configFile) {
config = new Configuration().configure(configFile);
buildSession();
}
public Session getSession() {
return session;
}
public void save(Object obj) {
Transaction tx = session.beginTransaction();
session.save(obj);
tx.commit();
}
public Object load(Class clas, Integer priId) {
return session.get(clas, priId);
}
public Query findbyhql(String hql) {
return session.createQuery(hql);
}
public List pageSizeByhql(String hql) {
return findbyhql(hql).list();
}
public SQLQuery findbysql(String sql) {
return session.createSQLQuery(sql);
}
public void update(Object obj) {
Transaction tx = session.beginTransaction();
session.saveOrUpdate(obj);
tx.commit();
}
public void delete(Class clas, Integer inte) {
session.delete(load(clas, inte));
}
public void delete(Object obj) {
session.delete(obj);
}
public void deletebyhql(String hql) {
Query query = session.createQuery(hql);
query.executeUpdate();
}
public Query createQuery(String hql) {
return session.createQuery(hql);
}
}
Hibernate访问多个数据库步骤三:测试类
package org.jskyme.data.test;
import junit.framework.TestCase;
import org.hibernate.Query;
import org.jskyme.hibernate.util.DataBaseManager;
import org.jskyme.hibernate.util.SessionManager;
public class DataBaseManagerTest extends TestCase {
DataBaseManager dbm = DataBaseManager.getInst();
public void testDatabase() {
setDatabase();
SessionManager tempSess = dbm.get("dataLocal");
Query query = tempSess.createQuery("from Shop");
query.list();
SessionManager tempSess27 = dbm.get("dateManage");
Query query27 = tempSess27.createQuery("from Shop");
query27.list();
}
private void setDatabase() {
SessionManager dateManageLocal = new SessionManager("localhost.cfg.xml");
SessionManager dateManage27 = new SessionManager("data_server.cfg.xml");
dbm.put("dateManage", dateManage27);
dbm.put("dataLocal", dateManageLocal);
}
}