本文主要是介绍Dorado7 MultiMcfBaseDao.java HibernateDao 多数据源支持(双Hibernate单数据库),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
双Hibernate单数据库
MultiMcfBaseDao.java
package com.mini.dao;import java.util.List;
import java.util.Map;import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;import com.bstek.dorado.data.provider.Page;
import com.bstek.dorado.hibernate.HibernateDao;/**** MultiBaseDAO 多数据源支持(双Hibernate单数据库)* 创建日期: 20151125* @author like12* @param <T>* */
@SuppressWarnings("rawtypes")
public class MultiMcfBaseDao<T, Serializable> extends HibernateDao {/*** 保存* @param entity*/public void saveMulti(T entity, Session session) {String entityName = this.getEntityName(entity);//like12 modified,20170401,bug,保存不起,不提交Transaction tran = session.beginTransaction();try{session.saveOrUpdate(entityName, entity);//提交tran.commit();}catch (Exception e){e.printStackTrace();//回滚tran.rollback();}finally{}}/*** 更新* @param hql* @param params*/public void excuteUpdateMulti(final String hql, Object[] params,Session session) {Query query = session.createQuery(hql);//like12 modified,20170401,bug,保存不起,不提交Transaction tran = session.beginTransaction();try{if (params != null) {int i = 0;for (Object o : params) {query.setParameter(i, o);i++;}}query.executeUpdate();//提交tran.commit();}catch (Exception e){e.printStackTrace();//回滚tran.rollback();}finally{}}/*** like12 add,20200815,支持Object* 单个查询Object* @param hql* @param session* @param parameters* @return*/@SuppressWarnings("unchecked")public <X> X findUniqueMulti(String hql, Session session,Object... parameters) {//查询X X = (X) createQueryMulti(hql, session, parameters).uniqueResult();//返回return X;}/*** 单个查询* @param hql* @param parameters* @return*/@SuppressWarnings("unchecked")public <X> X findUniqueMulti(String hql, Map<String, ?> parameters, Session session) {//查询X X = (X) createQueryMulti(hql, parameters, session).uniqueResult();//返回return X;}/*** like12 add,20200815,支持Object* 全部查询Object* @param hql* @param session* @param parameters* @return*/@SuppressWarnings("unchecked")public <X> List<X> findMulti(String hql, Session session, Object... parameters) {//查询List<X> X = createQueryMulti(hql, session, parameters).list();//返回return X;}/*** like12 add,20160127* 全部查询Map* @param hql* @param parameters* @return*/@SuppressWarnings("unchecked")public <X> List<X> findMulti(String hql, Map<String, ?> parameters, Session session) {//查询List<X> X = createQueryMulti(hql, parameters, session).list();//返回return X;}/*** 分页查询* @param page* @param hql* @param parameters* @return*/@SuppressWarnings("unchecked")public void findMulti(Page<T> page, String hql, Map<String, ?> parameters, Session session) {notNull(page, "page");//总条数long totalCount = countHqlResultMulti(hql, parameters, session);//正式查询Query q = this.createQueryMulti(hql, parameters, session);page.setEntityCount((int) totalCount);setPageParameterToQuery(q, page);page.setEntities(q.list());}/*** like12 add,20200815,支持Object* 查询用子函数* @param hql* @param session* @param parameters* @return*/public Query createQueryMulti(String hql, Session session,Object... parameters) {Query q = session.createQuery(hql);if (parameters != null) {for (int i = 0; i < parameters.length; ++i) {q.setParameter(i, parameters[i]);}}return q;}/*** 查询用子函数* @param queryString* @param parameters* @param session* @return*/public Query createQueryMulti(String queryString, Map<String, ?> parameters, Session session) {Query query = session.createQuery(queryString);if (parameters != null) {query.setProperties(parameters);}return query;}/*** 分页查询用* @param hql* @param parameters* @param session* @return*/protected long countHqlResultMulti(String hql, Map<String, ?> parameters, Session session) {String countHql = generateCountHqlMulti(hql);return ((Number) findUniqueMulti(countHql, parameters, session)).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;}
}
这篇关于Dorado7 MultiMcfBaseDao.java HibernateDao 多数据源支持(双Hibernate单数据库)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!