此EntityDao接口与EntityDaoImpl实现类,可取代了我们平时写的UserDao,NewsDao,CompanyDao等等,可直接供Service层调用.其中实现类的代码如下:
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/** *//**
* Copyright 2009-1012 the original author or authors.
* My Blog site: http://www.blogjava.net/rongxh7
*/
![](/Images/OutliningIndicators/None.gif)
package rong.common.dao;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;
import rong.common.utils.Pager;
import rong.util.MyHibernateDaoSupport;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/** *//**
* Dao层接口的实现类
* 许多人习惯根据不多的业务逻辑定义不同的DAO层接口,如UserDao,NewsDao,CompanyDao等等,
* 这样往往使得编码量十分庞大,而且带来了维护的困难,因此,抽取此DAO层接口,收录大部分
* DAO层必须的方法,以供Service层调用。
* @author rongxinhua
* @version 1.0
* @param <T> 范型,指实体类
* @param <PK> 范型,指实体类主键的数据类型,如Integer,Long
* @see rong.common.dao.EntityDao
*/
![](/Images/OutliningIndicators/None.gif)
@Repository(value="entityDao")
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class EntityDaoImpl<T,PK extends Serializable> extends MyHibernateDaoSupport implements EntityDao<T, PK>
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* 保存实体
* 包括添加和修改
* @param t 实体对象
*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void saveOrUpdate(T t)
{
getHibernateTemplate().saveOrUpdate(t);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* 更新实体
* 可用于添加、修改、删除操作
* @param hql 更新的HQL语句
* @param params 参数,可有项目或多项目,代替Hql中的"?"号
*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void update(final String hql,final Object
params)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
getHibernateTemplate().execute(new HibernateCallback()
{
![](/Images/OutliningIndicators/InBlock.gif)
public Object doInHibernate(Session session)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
throws HibernateException, SQLException
{
Query query = session.createQuery(hql);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(int i=0; i<params.length; i++)
{
query.setParameter(i, params[i]);
}
query.executeUpdate();
return null;
}
});
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* 删除实体
* @param t 实体对象
*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void delete(T t)
{
getHibernateTemplate().delete(t);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* 删除实体
* @param entityClass 实体类名
* @param id 实体的ID
*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void delete(Class<T> entityClass,PK id)
{
getHibernateTemplate().delete(get(entityClass,id));
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* 单查实体
* @param entityClass 实体类名
* @param id 实体的ID
* @return 实体对象
*/
@SuppressWarnings("unchecked")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public T get(Class<T> entityClass,PK id)
{
return (T)getHibernateTemplate().get(entityClass, id);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* 查询全部记录列表
* @param entityClass 实体类名
* @param propertyName 排序的参照属性
* @param isAsc 排序方式
* @param criterions 查询条件,可为0项或任意多项目
* @return 记录List集
*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public List<T> findAll(final Class<T> entityClass,final String propertyName,final boolean isAsc,final Criterion
criterions)
{
int firstResult = 0;
int maxResults = 0; //设置为0,则表示查询全部记录
return findByCriteria(entityClass, propertyName, isAsc, firstResult, maxResults, criterions);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* 查询列表
* @param entityClass 实体类名
* @param propertyName 排序的参照属性
* @param isAsc 排序方式,true表示升序,false表示降序,当propertyName赋值为null时,此参数无效
* @param firstResult 开始记录序号
* @param maxResults 最大记录数
* @param criterions 查询条件,可有0项或任意多项目
* @return 记录List集
*/
@SuppressWarnings("unchecked")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public List<T> findByCriteria(final Class<T> entityClass,final String propertyName,final boolean isAsc,final int firstResult,final int maxResults,final Criterion
criterions)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
List<T> list = (List<T>)getHibernateTemplate().execute(new HibernateCallback()
{
![](/Images/OutliningIndicators/InBlock.gif)
public Object doInHibernate(Session session)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
throws HibernateException, SQLException
{
Criteria criteria = session.createCriteria(entityClass);
//按属性条件查询
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(Criterion criterion : criterions)
{
criteria.add(criterion);
}
//按某个属性排序
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(null != propertyName)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(isAsc)
{
criteria.addOrder(Order.asc(propertyName));
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else
{
criteria.addOrder(Order.desc(propertyName));
}
}
//用于分页查询
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(maxResults != 0)
{
criteria.setFirstResult(firstResult);
criteria.setMaxResults(maxResults);
}
List<T> list = criteria.list();
return list;
}
});
return list;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* 查询总记录数
* @param entityClass 实体类名
* @param criterions 查询条件,可有0项或任意多项
* @return 总记录数
*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int findCountsByCriteria(final Class<T> entityClass,final Criterion
criterions)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
int totalCounts = (Integer)getHibernateTemplate().execute(new HibernateCallback()
{
![](/Images/OutliningIndicators/InBlock.gif)
public Object doInHibernate(Session session)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
throws HibernateException, SQLException
{
Criteria criteria = session.createCriteria(entityClass);
//按属性条件查询
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(Criterion criterion : criterions)
{
criteria.add(criterion);
}
int totalCounts = criteria.list().size();
return totalCounts;
}
});
return totalCounts;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* 分页查询
* @param entityClass 实体类名
* @param propertyName 排序参照属性
* @param isAsc 排序方式,true表示升序,false表示降序,当propertyName赋值为null时,此参数无效
* @param firstResult 开始记录序号
* @param maxResults 最大记录数
* @param criterions 查询条件,可为0项或任意多项目
* @return 封装List和totalCounts的Pager对象
*/
@SuppressWarnings("unchecked")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Pager<T> findForPager(final Class<T> entityClass,final String propertyName,final boolean isAsc,final int firstResult,final int maxResults,final Criterion
criterions)
{
int totalCounts = findCountsByCriteria(entityClass, criterions);
List<T> entityList = findByCriteria(entityClass, propertyName, isAsc, firstResult, maxResults, criterions);
Pager pager = new Pager();
pager.setTotalCounts(totalCounts);
pager.setEntityList(entityList);
return pager;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* 根据属性值查询列表
* @param entityClass 实体类名
* @param propertyName 属性名
* @param value 属性值
* @return List列表
*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public List<T> findByProperty(Class<T> entityClass,String propertyName,Object value)
{
Criterion criterion = Restrictions.eq(propertyName, value);
List<T> list = findAll(entityClass, null, true, criterion);
return list;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* 根据属性值查询单个对象
* @param entityClass 实体类名
* @param propertyName 属性名
* @param value 属性值
* @return 实体对象
*/
@SuppressWarnings("unchecked")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public T findUniqueByProperty(final Class<T> entityClass,final String propertyName,final Object value)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
T t = (T)getHibernateTemplate().execute(new HibernateCallback()
{
![](/Images/OutliningIndicators/InBlock.gif)
public Object doInHibernate(Session session)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
throws HibernateException, SQLException
{
Criteria criteria = session.createCriteria(entityClass).add(Restrictions.eq(propertyName, value));
T t = (T)criteria.uniqueResult();
return t;
}
});
return t;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* 根据属性值查询实体是否存在
* @param entityClass 实体类名
* @param propertyName 参照的属性名
* @param value 属性值
* @return 存在则返回true,不存在则返回false
*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public boolean isPropertyExist(final Class<T> entityClass,final String propertyName,final Object value)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
boolean isExist = (Boolean)getHibernateTemplate().execute(new HibernateCallback()
{
![](/Images/OutliningIndicators/InBlock.gif)
public Object doInHibernate(Session session)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
throws HibernateException, SQLException
{
Criteria criteria = session.createCriteria(entityClass).add(Restrictions.eq(propertyName, value));
boolean isEmpty = criteria.list().isEmpty();
return ! isEmpty;
}
});
return isExist;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
*
* @param hql 查询语句
* 用法如:可用于登录验证时,根据用户名、密码等信息查询用户
* @param params 参数数组,代替HQL中的"?"号,可有0项目或多项
* @return 唯一实体,返回null则表示不存在配置的实体
* @exception 如果查询的结果集不唯一,则抛异常
*/
@SuppressWarnings("unchecked")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public T findUniqueByHql(final String hql, final Object
params )
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
T t = (T)getHibernateTemplate().execute(new HibernateCallback()
{
![](/Images/OutliningIndicators/InBlock.gif)
public Object doInHibernate(Session session)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
throws HibernateException, SQLException
{
Query query = session.createQuery(hql);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(int i=0; i<params.length; i++)
{
query.setParameter(i, params[i]);
}
T t = (T)query.uniqueResult();
return t;
}
});
return t;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* 按HQL条件查询列表
* @param hql 查询语句,支持连接查询和多条件查询
* @param params 参数数组,代替hql中的"?"号
* @return 结果集List
*/
@SuppressWarnings("unchecked")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public List<T> findByHql(String hql,Object
params)
{
List list = getHibernateTemplate().find(hql, params);
return list;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* 按HQL分页查询
* @param firstResult 开始记录号
* @param maxResults 最大记录数
* @param hql 查询语句,支持连接查询和多条件查询
* @param params 参数数组,代替餐hql中的"?"号
* @return 封装List和total的Pager对象
*/
@SuppressWarnings("unchecked")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Pager<T> findForPagerByHql(final int firstResult, final int maxResults, final String hql, final Object
params)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Pager<T> pager = (Pager<T>)getHibernateTemplate().execute(new HibernateCallback()
{
![](/Images/OutliningIndicators/InBlock.gif)
public Object doInHibernate(Session session)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
throws HibernateException, SQLException
{
Query query = session.createQuery(hql);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(int position = 0; position < params.length; position ++)
{
query.setParameter(position, params[position]);
}
int totalCounts = query.list().size(); //总记录数
//用于分页查询
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(maxResults > 0)
{
query.setFirstResult(firstResult);
query.setMaxResults(maxResults);
}
List<T> list = query.list();
Pager<T> pager = new Pager<T>();
pager.setEntityList(list);
pager.setTotalCounts(totalCounts);
return pager;
}
});
return pager;
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
相关说明:MyHibernateDaoSupport类是我自定义的类,该类继承HibernateDaoSupport,相关代码,请参考我的
基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 (2) ,EntityDao接口与实现类EntityDao的方法声明一样,在此暂且不贴出来.而代码中用到的Pager类,是一个封装list<Entity>和totalCounts的POJO类,代码比较简单,在此也不贴出来.
开发本代码之前,参考过以下资料:
1)Springside的源码
2)一位北京网友发给我的项目中的代码
(*^-^*) 本文原创,转载请注明出处, http://www.blogjava.net/rongxh7谢谢! (*^-^*)
本文原创,转载请注明出处,谢谢!http://www.blogjava.net/rongxh7(心梦帆影JavaEE技术博客)
posted on 2009-05-19 09:56
心梦帆影 阅读(10425)
评论(20) 编辑 收藏 所属分类:
Hibernate 、
Spring