这是以前写的一个HibernateBaseDao ,方法较为全面,本身功能强大,欢迎大家拍砖
import java.util.Collection; import java.util.List; import java.util.Map; import org.hibernate.criterion.Criterion; /** * @author wanglf * 该BaseDao使用有一个限制,就是实体类的主键名推荐位id,姓名推荐为name * 便于使用主键,和实体名进行查询 */ public interface BaseDao<T> { public void insert(T entity); public void update(T entity); public void delete(T entity); public void deleteAll(Collection<T> entities); public T select(Integer id); public T select(String entityName); public int countAll(); public int conditionCount(Criterion... cs); public int getCount(String sql); public List<T> findAll(); public List<T> queryByhql(String hql); public List<T> findBy(Criterion... cs); public List<T> findByPage(int pageNo,int pageSize); public List<T> findByPage(int pageNo,int pageSize,Criterion... cs); public List<Object> findBySql(String sql); public List<Object> findBySql(String sql,Object... objects); public List<Map<String, Object>> findBySqlGetListMap(String sql,Object... objects); }
import java.util.Collection; import java.util.List; import java.util.Map; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.criterion.Criterion; import org.hibernate.criterion.DetachedCriteria; import org.hibernate.criterion.Order; import org.hibernate.criterion.Projections; import org.hibernate.criterion.Restrictions; import org.hibernate.transform.Transformers; import org.springframework.orm.hibernate3.HibernateTemplate; import com.wanglf.dao.BaseDao; import com.wanglf.util.ReflectUtil; @SuppressWarnings({ "rawtypes", "unchecked" }) public class BaseDaoImpl<T> implements BaseDao<T> { protected HibernateTemplate hibernateTemplate; public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } private Class entityClass; private String entityName; public BaseDaoImpl() { // 获取泛型T类型 entityClass = ReflectUtil.getClassGenricType(getClass());// 依据ReflectUtil工具类,获取类对象 entityName = entityClass.getSimpleName();// 获取类的类名(不带包名) } /** 插入实体类 */ public void insert(T entity) { hibernateTemplate.save(entity); } /** 更新实体类 */ public void update(T entity) { hibernateTemplate.update(entity); } /** 删除实体类 */ public void delete(T entity) { hibernateTemplate.delete(entity); } /** 依据Id查询实体类 */ public T select(Integer id) { return (T) hibernateTemplate.get(entityClass, id); } /** 依据Name查询实体类 */ public T select(String entityName) { DetachedCriteria dc = DetachedCriteria.forClass(entityClass); dc.add(Restrictions.eq("name", entityName)); List<T> list = hibernateTemplate.findByCriteria(dc); if (list != null) { return list.get(0); } return null; } /** 查询所有实体类 ,返回一个集合 */ public List<T> findAll() { return hibernateTemplate.find("from " + entityName); } /** 查询所有实体类数量 */ public int countAll() { List<Long> list = hibernateTemplate.find("select count(*) from " + entityName); if (list != null) { return list.get(0).intValue(); } return 0; } /** 按条件查询所有符合条件的实体类数量 */ public int conditionCount(Criterion... cs) { DetachedCriteria dc = DetachedCriteria.forClass(entityClass); for (int i = 0; i < cs.length; i++) { dc.add(cs[i]); } dc.setProjection(Projections.rowCount()); List<Long> list = hibernateTemplate.findByCriteria(dc); if (list != null) { Long l = (Long) list.get(0); return l.intValue(); } return 0; } /** 依据约束条件查询所有符合条件的实体类 ,返回符合分页条件的实体集合 */ public List<T> findBy(Criterion... cs) { DetachedCriteria dc = DetachedCriteria.forClass(entityClass); for (int i = 0; i < cs.length; i++) { dc.add(cs[i]); } return hibernateTemplate.findByCriteria(dc); } /** 依据pageNo和 pageSize,查询所有符合条件的实体类 ,返回符合分页条件的实体集合 */ public List<T> findByPage(int pageNo, int pageSize) { Session session = hibernateTemplate.getSessionFactory() .getCurrentSession(); Criteria criteria = session.createCriteria(entityClass); return criteria.addOrder(Order.asc("id")) .setFirstResult((pageNo - 1) * pageSize) .setMaxResults(pageSize).list(); // DetachedCriteria dc = DetachedCriteria.forClass(entityClass); // if (pageSize == 0 && pageNo < 1) { // return hibernateTemplate.findByCriteria(dc); // } // return hibernateTemplate.findByCriteria(dc, (pageNo - 1) * pageSize, // pageSize); } /** 依据条件查询所有符合条件的实体类 ,返回符合分页条件的实体集合 */ public List<T> findByPage(int pageNo, int pageSize, Criterion... cs) { DetachedCriteria dc = DetachedCriteria.forClass(entityClass); for (int i = 0; i < cs.length; i++) { dc.add(cs[i]); } if (pageSize == 0 && pageNo < 1) { return hibernateTemplate.findByCriteria(dc); } return hibernateTemplate.findByCriteria(dc, (pageNo - 1) * pageSize, pageSize); } /** 依据HQL语句,返回符合条件的List集合 */ public List<T> queryByhql(String hql) { return hibernateTemplate.find(hql); } /** 依据SQL语句和参数,返回符合条件的数据条件数 */ public int getCount(String sql) { Session session = hibernateTemplate.getSessionFactory() .getCurrentSession(); Query query = session.createSQLQuery(sql); query.setCacheable(true); return query.list().size(); } /** 依据SQL语句和参数,返回一个List<Map<String, Object>>集合 */ public List<Map<String, Object>> findBySqlGetListMap(String sql, Object... objects) { Session session = hibernateTemplate.getSessionFactory() .getCurrentSession(); Query query = session.createSQLQuery(sql).setResultTransformer( Transformers.ALIAS_TO_ENTITY_MAP); for (int i = 0; i < objects.length; i++) { query.setString(i, objects[i].toString()); } List<Map<String, Object>> maps = query.list(); return maps; } /** 依据SQL语句和参数,返回一个List<Object>集合 */ public List<Object> findBySql(String sql, Object... objects) { Session session = hibernateTemplate.getSessionFactory() .getCurrentSession(); Query query = session.createSQLQuery(sql).setResultTransformer( Transformers.TO_LIST); for (int i = 0; i < objects.length; i++) { query.setString(i, objects[i].toString()); } List<Object> list = query.list(); return list; } /** 依据SQL语句,返回一个List<Object>集合 */ public List<Object> findBySql(String sql) { Session session = hibernateTemplate.getSessionFactory() .getCurrentSession(); Query query = session.createSQLQuery(sql).setResultTransformer( Transformers.TO_LIST); List<Object> list = query.list(); return list; } public void deleteAll(Collection<T> entities) { hibernateTemplate.deleteAll(entities); } }
不过要用到相关的反射工具类ReflectUtil.java来获取相应的类类型
import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; /** * 反射工具类 * @author WSQ */ @SuppressWarnings({"unchecked","rawtypes"}) public class ReflectUtil { /** * 获得超类的参数类型,取第一个参数类型 * @param <T> 类型参数 * @param clazz 超类类型 */ public static <T> Class<T> getClassGenricType(final Class clazz) { return getClassGenricType(clazz, 0); } /** * 根据索引获得超类的参数类型 * @param clazz 超类类型 * @param index 索引 */ public static Class getClassGenricType(final Class clazz, final int index) { Type genType = clazz.getGenericSuperclass(); if (!(genType instanceof ParameterizedType)) { return Object.class; } Type[] params = ((ParameterizedType)genType).getActualTypeArguments(); if (index >= params.length || index < 0) { return Object.class; } if (!(params[index] instanceof Class)) { return Object.class; } return (Class) params[index]; } }
public class AttrGroupDaoImpl extends BaseDaoImpl<AttrGroup> implements AttrGroupDao {}
原文地址:http://blog.csdn.net/u014793936/article/details/45130867