package com.huxin.project.dao; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Session; import org.springframework.orm.hibernate3.HibernateCallback; import com.zuomj.cs.model.SysUser; import com.zuomj.cs.util.dao.GenericHibernateDao; //在这里指定所用到的范型 public class UserDAO extends GenericHibernateDao{ // save , saveAll // delete , // update , updateAll // find // List listByConditions(detachedCriteria,start,limit) 返回所有满足条件的记录 // List listByConditions(hql,start,limit) 查询所有的对象 // Long getRecordCount(detachedCriteria) 获得所有的记录数 public Integer batchDelete(final List<Long> ids, final int state) { return (Integer) getHibernateTemplate().execute(new HibernateCallback<Object>() { public Object doInHibernate(Session session) throws HibernateException { String hql = "update SysUser set statusFlag = :state where userId in(:ids)"; return session.createQuery(hql).setParameterList("ids", ids).setParameter("state", state) .executeUpdate(); } }); } }
package com.huxin.project.dao; import java.io.Serializable; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.Collection; import java.util.List; import org.hibernate.Criteria; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.criterion.CriteriaSpecification; import org.hibernate.criterion.DetachedCriteria; import org.hibernate.criterion.Projections; import org.springframework.orm.hibernate3.HibernateCallback; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; @SuppressWarnings("rawtypes") public class GenericHibernateDao<T extends Serializable, PK extends Serializable> extends HibernateDaoSupport implements GenericDao<T, PK> { private Class<T> entityClass; @SuppressWarnings("unchecked") public GenericHibernateDao() { this.entityClass = null; Class c = getClass(); Type t = c.getGenericSuperclass(); if (t instanceof ParameterizedType) { Type[] p = ((ParameterizedType) t).getActualTypeArguments(); this.entityClass = (Class<T>) p[0]; } } //得到一个对象的方法 @Override public T get(PK id) { return (T) getHibernateTemplate().get(entityClass, id); } @Override public List<T> loadAll() { return (List<T>) getHibernateTemplate().loadAll(entityClass); } //更新单个对象的方法 @Override public void update(T entity) { getHibernateTemplate().update(entity); } //更新所有对象的方法 @Override public void updateAll(List<T> entityList) throws Exception { Session session = getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); try { for (T entity : entityList) { session.update(entity); } session.flush(); session.clear(); tx.commit(); } catch (Exception e) { tx.rollback(); throw e; } session.close(); } //保存单个对象的方法 @Override public void save(T entity) { getHibernateTemplate().save(entity); } //保存所有对象的方法 @Override public void saveAll(List<T> entityList) throws Exception { Session session = getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); try { for (T entity : entityList) { session.save(entity); } session.flush(); session.clear(); tx.commit(); } catch (Exception e) { tx.rollback(); throw e; } session.close(); } //保存和修改一个对象的方法 @Override public void saveOrUpdate(T entity) { getHibernateTemplate().saveOrUpdate(entity); } @Override public void saveOrUpdateAll(Collection<T> entities) { getHibernateTemplate().saveOrUpdateAll(entities); } //删除一个对象的方法 @Override public void delete(T entity) { getHibernateTemplate().delete(entity); } @Override public void deleteAll(Collection<T> entities) { getHibernateTemplate().deleteAll(entities); } @Override public int bulkUpdate(String queryString) { return getHibernateTemplate().bulkUpdate(queryString); } @Override public int bulkUpdate(String queryString, Object[] values) { return getHibernateTemplate().bulkUpdate(queryString, values); } @Override public List find(String queryString) { return getHibernateTemplate().find(queryString); } //查询所有对象的方法 @Override public List find(String queryString, String values) { return getHibernateTemplate().find(queryString, values); } @Override public List find(String queryString, Object[] values) { return getHibernateTemplate().find(queryString, values); } @Override public Criteria criteriaByConditions(final DetachedCriteria detachedCriteria, final int startResult, final int limitResult) { return (Criteria) getHibernateTemplate().execute(new HibernateCallback<Object>() { public Object doInHibernate(Session session) throws HibernateException { Criteria criteria = detachedCriteria.getExecutableCriteria(session); criteria.setFirstResult(startResult); criteria.setMaxResults(limitResult); return criteria; } }); } //根据limit查询所有的对象 @Override public List listByConditions(final DetachedCriteria detachedCriteria, final int startResult, final int limitResult) { return (List) getHibernateTemplate().execute(new HibernateCallback<Object>() { public Object doInHibernate(Session session) throws HibernateException { Criteria criteria = detachedCriteria.getExecutableCriteria(session); criteria.setFirstResult(startResult); criteria.setMaxResults(limitResult); criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY); return criteria.list(); } }); } //根据 limit,查询的所有对象 @Override public List listByConditions(final String hql, final int firstResult, final int maxResults) { return (List) getHibernateTemplate().execute(new HibernateCallback<Object>() { public Object doInHibernate(Session session) throws HibernateException { return session.createQuery(hql).setFirstResult(firstResult).setMaxResults(maxResults).list(); } }); } //根据DetachedCriteria对象返回向对应的记录数 @Override public Long getRecordCount(final DetachedCriteria detachedCriteria) { return (Long) getHibernateTemplate().execute(new HibernateCallback<Object>() { public Object doInHibernate(Session session) throws HibernateException { Criteria criteria = detachedCriteria.getExecutableCriteria(session); long count = Long.parseLong((criteria.setProjection(Projections.rowCount()).uniqueResult()).toString()); criteria.setProjection(null); return Long.valueOf("" + count); } }); } @Override public Long getRecordCount(String hql) { int beginIndex = hql.indexOf("from"); hql = "select count(*) " + hql.substring(beginIndex); logger.info(hql); return _getRecordCount(hql); } private Long _getRecordCount(final String hql) { return (Long) getHibernateTemplate().execute(new HibernateCallback<Object>() { public Object doInHibernate(Session session) throws HibernateException { return session.createQuery(hql).uniqueResult(); } }); } // 根据hql语句查询所有的对象,返回List对象 @Override public List queryByHql(String hql) { return getHibernateTemplate().find(hql); } }
package com.huxin.project.dao; import java.io.Serializable; import java.util.Collection; import java.util.List; import org.hibernate.Criteria; import org.hibernate.criterion.DetachedCriteria; @SuppressWarnings("rawtypes") public interface GenericDao<T extends Serializable, PK extends Serializable> { T get(PK id); List<T> loadAll(); void update(T entity); void updateAll(List<T> entityList) throws Exception; void save(T entity); void saveAll(List<T> entityList) throws Exception; void saveOrUpdate(T entity); void saveOrUpdateAll(Collection<T> entities); void delete(T entity); void deleteAll(Collection<T> entities); // -------------------- HSQL -------------------- int bulkUpdate(String queryString); int bulkUpdate(String queryString, Object[] values); List find(String queryString); List find(String queryString, String values); List find(String queryString, Object[] values); Criteria criteriaByConditions(final DetachedCriteria detachedCriteria, final int startResult, final int limitResult); List listByConditions(final DetachedCriteria detachedCriteria, final int startResult, final int limitResult); List listByConditions(final String hql, final int firstResult, final int maxResults); Long getRecordCount(final DetachedCriteria detachedCriteria); Long getRecordCount(String hql); List queryByHql(String hql); }
关于DAO层一个封装
最新推荐文章于 2024-08-12 17:02:49 发布