2007-04-21
贡献一点自己的DAO设计代码(部分代码来自网络)
大约在一年前,看了 robbin 关于Hibernate中DAO分页方案的实现,于是整理了一下,又做了一些扩展,做了一个BaseDAO,这两天看到DAO中都在讨论DAO怎么设计,贡献出来,大家一块讨论一下了,我自己也正在做优化和改进工作
java 代码
- package com.***.core.dao.hibernate.controller;
- import java.io.Serializable;
- import java.sql.Connection;
- import java.sql.SQLException;
- import java.util.Collection;
- import java.util.Iterator;
- import java.util.List;
- import org.apache.commons.beanutils.PropertyUtils;
- import org.apache.commons.lang.StringUtils;
- import org.hibernate.Criteria;
- import org.hibernate.HibernateException;
- import org.hibernate.LockMode;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.criterion.CriteriaSpecification;
- import org.hibernate.criterion.DetachedCriteria;
- import org.hibernate.criterion.Projections;
- import org.hibernate.criterion.Restrictions;
- import org.springframework.orm.hibernate3.HibernateCallback;
- import org.springframework.orm.hibernate3.HibernateTemplate;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
- import org.springframework.util.Assert;
- import com.***.core.dao.hibernate.iface.IBaseHibernateDao;
- import com.***.core.util.exception.DBException;
- import com.***.core.util.helper.PaginationSupport;
- import com.***.core.util.helper.StringUtil;
- public class BaseHibernateDao extends HibernateDaoSupport implements IBaseHibernateDao {
- /**
- * ============================================================================
- * 取得记录集数目
- * ===========================================================================
- */
- public int getCount(String queryName) throws DBException {
- return this.getCount(queryName, (String[]) null, (Object[]) null);
- }
- public int getCount(String queryName, String param, Object value)
- throws DBException {
- return this.getCount(queryName, new String[] { param },
- new Object[] { value });
- }
- public int getCount(String queryName, String[] params, Object[] values)
- throws DBException {
- int result = 0;
- try {
- result = ((Long) this.getHibernateTemplate()
- .findByNamedQueryAndNamedParam(queryName, params, values)
- .iterator().next()).intValue();
- return result;
- } catch (Exception ex) {
- return result;
- }
- }
- public int getCount(final DetachedCriteria detachedCriteria)
- throws DBException {
- Integer count = ((Long) getHibernateTemplate().execute(
- new HibernateCallback() {
- public Object doInHibernate(Session session)
- throws HibernateException {
- Criteria criteria = detachedCriteria
- .getExecutableCriteria(session);
- return criteria.setProjection(Projections.rowCount())
- .uniqueResult();
- }
- }, true)).intValue();
- return count.intValue();
- }
- /**
- * =========================================================================
- * 取得一页数据
- *
- * =========================================================================
- */
- // ---------------------------------------------------------------------
- // 无参数取得一页数据
- // --------------------------------------------------------------------
- public PaginationSupport getPage(String queryName) throws DBException {
- return getPage(queryName, (String[]) null, (Object[]) null,
- PaginationSupport.PAGESIZE, 0);
- }
- public PaginationSupport getPage(String queryName, int startIndex)
- throws DBException {
- return getPage(queryName, (String[]) null, (Object[]) null,
- PaginationSupport.PAGESIZE, startIndex);
- }
- public PaginationSupport getPage(String queryName, int pageSize,
- int startIndex) throws DBException {
- return getPage(queryName, (String[]) null, (Object[]) null, pageSize,
- startIndex);
- }
- // ---------------------------------------------------------------------------
- // 使用带一个参数的HQL取得一页数据
- // ---------------------------------------------------------------------------
- public PaginationSupport getPage(String queryName, String param,
- Object value) throws DBException {
- return getPage(queryName, new String[] { param },
- new Object[] { value }, PaginationSupport.PAGESIZE, 0);
- }
- public PaginationSupport getPage(String queryName, String param,
- Object value, int startIndex) throws DBException {
- return getPage(queryName, new String[] { param },
- new Object[] { value }, PaginationSupport.PAGESIZE, startIndex);
- }
- public PaginationSupport getPage(String queryName, String param,
- Object value, int pageSize, int startIndex) throws DBException {
- return getPage(queryName, new String[] { param },
- new Object[] { value }, pageSize, startIndex);
- }
- // ------------------------------------------------------------------------------
- // 使用带有一系列参数的HQL取得一页数据
- // .------------------------------------------------------------------------------
- public PaginationSupport getPage(String queryName, String[] params,
- Object[] values) throws DBException {
- return getPage(queryName, params, values, PaginationSupport.PAGESIZE, 0);
- }
- public PaginationSupport getPage(String queryName, String[] params,
- Object[] values, int startIndex) throws DBException {
- return getPage(queryName, params, values, PaginationSupport.PAGESIZE,
- startIndex);
- }
- public PaginationSupport getPage(final String queryName,
- final String[] params, final Object[] values, final int pageSize,
- final int startIndex) throws DBException {
- if (params != null && values != null && params.length != values.length) {
- throw new IllegalArgumentException(
- "Length of paramNames array must match length of values array");
- }
- return (PaginationSupport) getHibernateTemplate().execute(
- new HibernateCallback() {
- public Object doInHibernate(Session session)
- throws HibernateException {
- StringUtil stringUtil = new StringUtil();
- Query queryObject = session.getNamedQuery(queryName);
- String hql = stringUtil.createCountHQL(queryObject
- .getQueryString());
- Query countQuery = session.createQuery(hql);
- if (values != null) {
- for (int i = 0; i < values.length; i++) {
- queryObject.setParameter(params[i], values[i]);
- countQuery.setParameter(params[i], values[i]);
- }
- }
- queryObject.setFirstResult(startIndex).setMaxResults(
- pageSize);
- int totalCount = ((Long) countQuery.list()
- .iterator().next()).intValue();
- List items = queryObject.list();
- PaginationSupport ps = new PaginationSupport(items,
- totalCount, pageSize, startIndex);
- return ps;
- }
- });
- }
- /**
- *-----------------------------------------------------------
- *无参数名HQL操作
- *-------------------------------------------------------------
- */
- public int getCont(String queryName, Object value) throws DBException {
- // TODO Auto-generated method stub
- return this.getCount(queryName, new Object[]{value});
- }
- public int getCount(String queryName, Object[] values) throws DBException {
- // TODO Auto-generated method stub
- int result = 0;
- try {
- result = ((Long) this.getHibernateTemplate().findByNamedQuery(queryName, values)
- .iterator().next()).intValue();
- } catch (DBException ex) {
- ex.printStackTrace();
- }
- return result;
- }
- public PaginationSupport getPage(String queryName, Object value) throws DBException {
- // TODO Auto-generated method stub
- return this.getPage(queryName,new Object[] {value},PaginationSupport.PAGESIZE,0);
- }
- public PaginationSupport getPage(String queryName, Object[] values) throws DBException {
- // TODO Auto-generated method stub
- return this.getPage(queryName,values,PaginationSupport.PAGESIZE,0);
- }
- public PaginationSupport getPage(String queryName, Object value, int startIndex) throws DBException {
- // TODO Auto-generated method stub
- return this.getPage(queryName,new Object[]{value},PaginationSupport.PAGESIZE,startIndex);
- }
- public PaginationSupport getPage(String queryName, Object value, int pageSize, int startIndex) throws DBException {
- // TODO Auto-generated method stub
- return this.getPage(queryName,new Object[]{value},pageSize,startIndex);
- }
- public PaginationSupport getPage(String queryName, Object[] values, int startIndex) throws DBException {
- // TODO Auto-generated method stub
- return this.getPage(queryName,values,PaginationSupport.PAGESIZE,startIndex);
- }
- public PaginationSupport getPage(final String queryName,final Object[] values,final int pageSize,final int startIndex) throws DBException {
- // TODO Auto-generated method stub
- return (PaginationSupport) getHibernateTemplate().execute(
- new HibernateCallback() {
- public Object doInHibernate(Session session)
- throws HibernateException {
- StringUtil stringUtil = new StringUtil();
- Query queryObject = session.getNamedQuery(queryName);
- String hql = stringUtil.createCountHQL(queryObject
- .getQueryString());
- Query countQuery = session.createQuery(hql);
- if (values != null) {
- for (int i = 0; i < values.length; i++) {
- queryObject.setParameter(i, values[i]);
- countQuery.setParameter(i, values[i]);
- }
- }
- queryObject.setFirstResult(startIndex).setMaxResults(
- pageSize);
- int totalCount = ((Long) countQuery.list()
- .iterator().next()).intValue();
- List items = queryObject.list();
- PaginationSupport ps = new PaginationSupport(items,
- totalCount, pageSize, startIndex);
- return ps;
- }
- });
- }
- // -------------------------------------------------------------------------
- // 使用DetachedCriteria取得一页数据,条件包装在DetachedCriteria中
- // -----------------------------------------------------------------------
- public PaginationSupport getPage(DetachedCriteria detachedCriteria)
- throws DBException {
- return getPage(detachedCriteria, PaginationSupport.PAGESIZE, 0);
- }
- public PaginationSupport getPage(DetachedCriteria detachedCriteria,
- int startIndex) throws DBException {
- return getPage(detachedCriteria, PaginationSupport.PAGESIZE, startIndex);
- }
- public PaginationSupport getPage(final DetachedCriteria detachedCriteria,
- final int pageSize, final int startIndex) throws DBException {
- return (PaginationSupport) getHibernateTemplate().execute(
- new HibernateCallback() {
- public Object doInHibernate(Session session)
- throws HibernateException {
- Criteria criteria = detachedCriteria
- .getExecutableCriteria(session);
- int totalCount = ((Long) criteria.setProjection(
- Projections.rowCount()).uniqueResult())
- .intValue();
- criteria.setProjection(null);
- criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
- List items = criteria.setFirstResult(startIndex)
- .setMaxResults(pageSize).list();
- PaginationSupport ps = new PaginationSupport(items,
- totalCount, pageSize, startIndex);
- return ps;
- }
- }, true);
- }
- // -------------------------------------------------------------------------
- // 取得所有数据。(根据条件)
- // -------------------------------------------------------------------------
- public List getAll(final DetachedCriteria detachedCriteria)
- throws DBException {
- return (List) getHibernateTemplate().execute(new HibernateCallback() {
- public Object doInHibernate(Session session)
- throws HibernateException {
- Criteria criteria = detachedCriteria
- .getExecutableCriteria(session);
- return criteria.list();
- }
- }, true);
- }
- public List getAll(String queryName) throws DBException {
- return this.getHibernateTemplate().findByNamedQuery(queryName);
- }
- public List getAll(String queryName, String param, Object value)
- throws DBException {
- return this.getHibernateTemplate().findByNamedQueryAndNamedParam(
- queryName, param, value);
- }
- public List getAll(String queryName, String[] params, Object[] values)
- throws DBException {
- return this.getHibernateTemplate().findByNamedQueryAndNamedParam(
- queryName, params, values);
- }
- public List getAll(String queryName, Object value) throws DBException {
- return this.getAll(queryName, new Object[] { value });
- }
- public List getAll(String queryName, Object[] values) throws DBException {
- return this.getHibernateTemplate().findByNamedQuery(queryName, values);
- }
- /**
- * ============================================================================
- * 数据记录操作 包括新增,修改,删除数据
- * ============================================================================
- */
- &nbs
- 10:28
- 浏览 (3388)
- 论坛浏览 (4088)
- 评论 (2)
- 分类: 技术
- 相关推荐
发表评论
该博客是同时发布到论坛的,无法评论在论坛已被锁定的帖子
我的相册
Faceye基础版(开源)新建标签页
共 24 张
共 24 张
链接
最新评论
-
SNMP基础开发
大哥。。。不行哦。。。。。搞一些军事联接来骗俺。。。。太坏了。。。5555555 ...
-- by 郭亲华 -
一棵完整的异步加载的Ext ...
http://faceye.googlecode.com
-- by ecsun -
一棵完整的异步加载的Ext ...
似乎没有看到收尾的工作,干脆把代码传上来得了。可以学习学习
-- by phenom -
结束半月假期,开始工作
这不刚第一天来上班嘛~
-- by ecsun -
结束半月假期,开始工作
国庆节假日不办理户口的吧?我还得专门请假去弄户口,麻烦啊。
-- by geminiyellow

![ecsun的博客: [海鹏Blog]--{FaceYe开源} 用户头像](http://www.javaeye.com/upload/logo/user/36668/bcfaff38-8200-4288-88e6-f588c3138e36.gif?1196653519)





评论排行榜