package com.redbaby.dao;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javacommon.xsqlbuilder.XsqlBuilder;
import javacommon.xsqlbuilder.XsqlBuilder.XsqlFilterResult;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.redbaby.util.Page;
public abstract class HibernateGenericDao extends HibernateDaoSupport {
/**
* 得到一条数据(整个Object 对象)
*
* hql: from Object where .....
*
* @param hql
* @param values
* @return
* @throws DataAccessException
*/
public Object getInfo(String hql, Object... values) throws DataAccessException {
List list = null;
list = super.getHibernateTemplate().find(hql, values);
if (list != null && list.size() > 0) {
return list.get(0);
}
return null;
}
/**
* 得到对象Object 某一个属性 select 中 必选指定要查询 字段名称(一个)
*
* hql: select p.id from object where 1=1 .....
*/
public Object getInfoParam(final String hql, Object... values) throws DataAccessException {
Session session = super.getSession();
Query query = session.createQuery(hql);
if (values != null) {
for (int i = 0; i < values.length; i++) {
query.setParameter(i, values[i]);
}
}
Object obj = query.uniqueResult();
return obj;
}
/**
* 得到 List
*
* @param hql
* @param values
* @return
* @throws DataAccessDataAccessException
*/
public List<?> getListByParam(String hql, Object... values) throws DataAccessException {
return super.getHibernateTemplate().find(hql, values);
}
/**
* 执行 带参数的 DML UPDATE DELETE
*
* @param hql
* @param values
* @return
* @throws DataAccessDataAccessException
*/
public void updateForParam(final String hql, final Object... values) throws DataAccessException {
super.getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) {
Query query = session.createQuery(hql);
if (values != null) {
for (int i = 0; i < values.length; i++) {
query.setParameter(i, values[i]);
}
}
return query.executeUpdate();
}
});
}
/**
* HQL分页(带查询)
*
* @param hql
* @param params
* @param page
* @return
* @throws DataAccessDataAccessException
*/
public List<?> getListPage(String hql, Map<Object, Object> params, Page page) throws DataAccessException {
if (params == null) {
params = new HashMap<Object, Object>();
}
int first = 0;
int max = 0;
if (null != page) {
first = (int) ((page.getCurrentPage() - 1) * page.getPageSize());
max = (int) page.getPageSize();
}
final int firstResult = first;
final int maxResults = max;
XsqlFilterResult result = new XsqlBuilder().generateHql(hql, params);
Long count = getTotalCount(result.getXsql(), 1);
if (page != null) {
page.setTotalSize(count);
}
final Page pageFin = page;
final String newHql = result.getXsql();
return super.getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) {
Query query = session.createQuery(newHql);
if (pageFin != null) {
query.setFirstResult(firstResult);
query.setMaxResults(maxResults);
}
return query.list();
}
});
}
/**
* SQL分页查询
*/
public List<?> getListPageSQL(String sql, Map<Object, Object> params, Page page, Class<?> tclass) throws DataAccessException {
XsqlFilterResult result = new XsqlBuilder().generateHql(sql, params);
if (page != null) {
Long count = getTotalCount(result.getXsql(), 2);
page.setTotalSize(count); //
}
Session session = super.getSession();
Query queryList = session.createSQLQuery(result.getXsql()).addEntity(tclass);
if (page != null) {
queryList.setFirstResult((int) ((page.getCurrentPage() - 1) * page.getPageSize())); //
queryList.setMaxResults((int) page.getPageSize()); //
}
List<?> tList = queryList.list();
return (List<?>) tList;
}
// 返回总记录数
public long getTotalCount(String hql, long num) {
long count = 0;
int sql_from = hql.toLowerCase().indexOf("from");
int sql_orderby = hql.toLowerCase().indexOf("order by");
String countStr = "";
if (sql_orderby > 0) {
countStr = "select count(*) " + hql.substring(sql_from, sql_orderby);
} else {
countStr = "select count(*) " + hql.substring(sql_from);
}
Session session = super.getSession();
if (num == 1) {// 1,HQL查询
Query query = session.createQuery(countStr);
count = (Long) query.uniqueResult();
} else if (num == 2) {// 2,SQL查询
count = ((BigDecimal) (session.createSQLQuery(countStr)).uniqueResult()).longValue(); //
}
return count;
}
/**
* 通过sql查询Object... values形式且无分页
*
* @param sql
* @param tclass
* @param values
* @return
* @throws DataAccessException
*/
public List<?> getListByParamSQL(String sql, Class<?> tclass, Object... values) throws DataAccessException {
Session session = super.getSession();
Query queryList = session.createSQLQuery(sql).addEntity(tclass);
if (values != null) {
for (int i = 0; i < values.length; i++) {
queryList.setParameter(i, values[i]);
}
}
List<?> tList = queryList.list();
return (List<?>) tList;
}
/**
* 通过sql查询params形式且无分页
*
* @param hql
* @param params
* @return
* @throws DataAccessException
*/
public List<?> getListByParam(String hql, Map<Object, Object> params) throws DataAccessException {
if (params == null) {
params = new HashMap<Object, Object>();
}
XsqlFilterResult result = new XsqlBuilder().generateHql(hql, params);
final String newHql = result.getXsql();
return super.getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) {
Query query = session.createQuery(newHql);
return query.list();
}
});
}
/**
* 无分页,无所属类的SQL查询
*/
public List<Map> getListMapBySQL(String sql, Map<Object, Object> params) throws DataAccessException {
XsqlFilterResult result = new XsqlBuilder().generateHql(sql, params);
Session session = super.getSession();
Query queryList = session.createSQLQuery(result.getXsql());
List<Map> tListMap = queryList.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
return tListMap;
}
/**
* 有分页,无所属类的SQL查询
*/
public List<Map> getListMapByPageSQL(String sql, Map<Object, Object> params, Page page) throws DataAccessException {
XsqlFilterResult result = new XsqlBuilder().generateHql(sql, params);
if (page != null) {
Long count = getTotalCount(result.getXsql(), 2);
page.setTotalSize(count); //
}
Session session = super.getSession();
Query queryList = session.createSQLQuery(result.getXsql());
if (page != null) {
queryList.setFirstResult((int) ((page.getCurrentPage() - 1) * page.getPageSize())); //
queryList.setMaxResults((int) page.getPageSize()); //
}
List<Map> tListMap = queryList.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
return tListMap;
}
}