Hibernate实现通用DAO


package com.myerp.dao;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.myerp.dto.QueryResult;

public abstract class SimpleDaoSupport<T> extends HibernateDaoSupport {

private Class<T> entityClass;

protected SimpleDaoSupport(Class<T> entityClass) {
this.entityClass = entityClass;
}

/**
* 将sessionFactory注入Spring的模板
*/
@Resource(name="sessionFactory")
protected void setSF(SessionFactory sf) {
setSessionFactory(sf);
}

public void save(T transientInstance) {
getHibernateTemplate().save(transientInstance);
}

public void delete(T persistentInstance) {
getHibernateTemplate().delete(persistentInstance);
}

public void delete(Serializable id) {
getSession().delete(load(id));

}

public void update(T persistentInstance) {
getHibernateTemplate().update(persistentInstance);
}

public void merge(T persistentInstance) {
getHibernateTemplate().merge(persistentInstance);
}

public T findById(Serializable id) {
return (T) getHibernateTemplate().get(entityClass.getName(), id);
}
public T load(Serializable id) {
return (T) getSession().load(entityClass, id);
}

public List<T> findByExample(T instance) {
return getHibernateTemplate().findByExample(instance);
}

public List<T> findByProperty(String propertyName, Object value) {
String queryString = "from "+ entityClass.getSimpleName() +" as model where model." + propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
}

public List<T> findAll() {
String queryString = "from " + entityClass.getSimpleName();
return getHibernateTemplate().find(queryString);
}
/**
* 执行HQL语句
*/
public void execute(String hql) {
getSession().createQuery(hql).executeUpdate();
}
/**
* 指定过滤条件的分页数据
*/
public QueryResult<T> findScrollData(int start, int maxResults, List<String> whereClause){
return this.findScrollData(start, maxResults, whereClause, null);
}
/**
* 无任何条件的分页
*/
public QueryResult<T> findScrollData(int pageNo, int pageSize){
return this.findScrollData(pageNo, pageSize, null, null);
}
/**
* 有排序规则的分页数据
*/
public QueryResult<T> findScrollData(int pageNo, int pageSize, LinkedHashMap<String, String> orderbyClause) {
return this.findScrollData(pageNo, pageSize, null, orderbyClause);
}
/**
* 分页查询数据
* @param start 开始记录
* @param end 结束记录
* @param whereClause 过滤条件
* @param orderbyClause 排序子句
* @return 查询结果, 里面包含了指定页的记录和总共的页数
*/
public QueryResult<T> findScrollData(int pageNo, int pageSize, List<String> whereClause, LinkedHashMap<String, String> orderbyClause) {
int start = pageNo > 0 ?(pageNo - 1) * pageSize : 0;
QueryResult<T> qr = new QueryResult<T>();
Query q = getSession().createQuery("select count(*) from " + entityClass.getSimpleName() + " entity " + buildWhereClause(whereClause));
int count = Integer.parseInt(q.uniqueResult() + "");
if(start >= count) start = count - 1 - pageSize;
q = getSession().createQuery("from " + entityClass.getSimpleName() + " entity " + buildWhereClause(whereClause) + buildOrderByClause(orderbyClause));
q.setFirstResult(start).setMaxResults(pageSize);
qr.setPageNo(pageNo).setPageSize(pageSize).setRecordCount(count).setRecords(q.list());
return qr;
}

/**
* 建立排序子句
*/
protected String buildOrderByClause(LinkedHashMap<String, String> orderbyClause) {
StringBuilder orderby = new StringBuilder("");
if(orderbyClause != null && orderbyClause.size() > 0) {
orderby.append(" order by ");
for(String prop : orderbyClause.keySet()) {
orderby.append(" entity.").append(prop).append(" ").append(orderbyClause.get(prop)).append(",");
}
orderby.deleteCharAt(orderby.length()-1);
}
return orderby.toString();
}

/**
* 根据过滤条件收集器建立where子句
*/
protected String buildWhereClause(List<String> whereClause) {
StringBuilder where = new StringBuilder("");
if(whereClause != null && whereClause.size() > 0) {
where.append(" ").append("where ");
for(String str : whereClause) {
where.append(" entity.").append(str).append(" and ");
}
where.delete(where.lastIndexOf("and"), where.length()-1);
}
return where.toString();
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值