- ecsun
- 等级:

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

- 文章: 216
- 积分: 205

|
大约在一年前,看了 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);
- }
-
-
-
-
-
-
-
-
-
-
-
- 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);
- }
-
-
-
-
-
-
- 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;
- }
- });
- }
-
-
-
-
-
- public int getCont(String queryName, Object value) throws DBException {
-
- return this.getCount(queryName, new Object[]{value});
- }
-
- public int getCount(String queryName, Object[] values) throws DBException {
-
- 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 {
-
- return this.getPage(queryName,new Object[] {value},PaginationSupport.PAGESIZE,0);
- }
-
- public PaginationSupport getPage(String queryName, Object[] values) throws DBException {
-
- return this.getPage(queryName,values,PaginationSupport.PAGESIZE,0);
- }
-
- public PaginationSupport getPage(String queryName, Object value, int startIndex) throws DBException {
-
- return this.getPage(queryName,new Object[]{value},PaginationSupport.PAGESIZE,startIndex);
- }
-
- public PaginationSupport getPage(String queryName, Object value, int pageSize, int startIndex) throws DBException {
-
- return this.getPage(queryName,new Object[]{value},pageSize,startIndex);
- }
-
- public PaginationSupport getPage(String queryName, Object[] values, int startIndex) throws DBException {
-
- 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 {
-
-
- 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;
- }
- });
- }
-
-
-
- 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
声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|