本文主要是介绍Dorado7 MultiHibernateDao.java McfBaseDAO 多数据源支持(单Hibernate双数据库),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
单Hibernate双数据库
MultiHibernateDao.java
package com.gs.oms.dao;import java.sql.Connection;
import java.sql.DriverManager;
import java.util.List;
import java.util.Map;
import java.util.Properties;import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;import com.bstek.dorado.data.provider.Page;
import com.gs.mcf.common.McfBaseDAO;/**** MultiHibernateDao 多数据源支持(单Hibernate双数据库)* 创建日期: 20151125* @author like12* @param <T>* */
@SuppressWarnings("rawtypes")
@Repository
public class MultiHibernateDao<T> extends McfBaseDAO {//like12 add,20151224,全局Sessionprivate static Session sessionGlobal = null;/*** 保存* @param entity*/public void saveMulti(T entity) {String entityName = this.getEntityName(entity);Session session = this.getSessionMulti();session.saveOrUpdate(entityName, entity);session.flush();session.clear();}/*** 修改* * @param hql* @param params*/public void excuteUpdate(final String hql, Object[] params) {Query query = this.getSessionMulti().createQuery(hql);if (params != null) {int i = 0;for (Object o : params) {query.setParameter(i, o);i++;}}query.executeUpdate();}/*** 单个查询* @param hql* @param parameters* @return*/@SuppressWarnings("unchecked")public <X> X findUniqueMulti(String hql, Map<String, ?> parameters) {return (X) createQueryMulti(hql, parameters).uniqueResult();}/*** like12 add,20160127* 全部查询* @param hql* @param parameters* @return*/@SuppressWarnings("unchecked")public <X> List<X> findMulti(String hql, Map<String, ?> parameters) {return createQueryMulti(hql, parameters).list();}/*** 分页查询* @param page* @param hql* @param parameters* @return*/@SuppressWarnings("unchecked")public Page<T> findMulti(Page<T> page, String hql, Map<String, ?> parameters) {notNull(page, "page");Query q = this.createQueryMulti(hql, parameters);long totalCount = countHqlResultMulti(hql, parameters);page.setEntityCount((int) totalCount);setPageParameterToQuery(q, page);page.setEntities(q.list());return page;}public Query createQueryMulti(String queryString, Map<String, ?> parameters) {Query query = this.getSessionMulti().createQuery(queryString);if (parameters != null) {query.setProperties(parameters);}return query;}protected long countHqlResultMulti(String hql, Map<String, ?> parameters) {String countHql = generateCountHqlMulti(hql);return ((Number) findUniqueMulti(countHql, parameters)).longValue();}private String generateCountHqlMulti(String hql) {hql = "from " + StringUtils.substringAfter(hql, "from");hql = StringUtils.substringBefore(hql, "order by");String countHql = "select count(*) " + hql;return countHql;}/*** 获取多数据源Session* @return*/public Session getSessionMulti() {//like12 modofied,20151224,每一次 增删改查 都要产生一次新的Session 数据库会很快死掉if(sessionGlobal == null){Properties prop = new Properties();String driver = null;String url = null;String username = null;String password = null;Connection connection = null;try{//读配置文件prop.load(this.getClass().getClassLoader().getResourceAsStream("DBConfigCygl.properties"));driver = prop.getProperty("driver");url = prop.getProperty("url");username = prop.getProperty("username");password = prop.getProperty("password");Class.forName(driver);//1.加载驱动connection = DriverManager.getConnection(url,username,password);//2.得到连接}catch (Exception e){e.printStackTrace();}//中途拦截,正规默认为:this.sessionFactory.openSession()sessionGlobal = this.sessionFactory.openSession(connection);}//like12 add,20151228,bug,需清除上一次操作的遗留信息,否则编辑不了sessionGlobal.clear();return sessionGlobal;}/*** like12 add,20160127,关闭Session*/public void closeSessionMulti() {if(sessionGlobal != null){sessionGlobal.close();//like12 add,20160302,bug,session被关闭后不是null 而是‘SessionImpl(<closed>)’,下次将报错sessionGlobal = null;}}
}
这篇关于Dorado7 MultiHibernateDao.java McfBaseDAO 多数据源支持(单Hibernate双数据库)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!