---
title: "一个基于hibernate的超类（仿springside） 已被以后借鉴"
author: "yaoweinan"
date: "2011-09-06T09:43:02Z"
tags: []
categories: ["Web前端"]
slug: "hibernate-springside"
draft: false
lastmod: "2011-09-06T14:43:36Z"
id: 1166307
url: "https://ilyon.iteye.com/blog/1166307"
---

package com.dao;

import java.io.Serializable;  
import java.lang.reflect.ParameterizedType;  
import java.lang.reflect.Type;  
import java.util.List;

import org.hibernate.Criteria;  
import org.hibernate.SessionFactory;  
import org.hibernate.criterion.Criterion;  
import org.hibernate.criterion.Example;  
import org.hibernate.criterion.Order;  
import org.hibernate.criterion.Property;  
import org.hibernate.criterion.Restrictions;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.transaction.annotation.Transactional;  
/\**  
 * 实现增删改查的超类  
 * @author Administrator  
 *  
 * @param &lt;T&gt;  
 * @param &lt;I&gt;  
 \*/  
@Transactional  
public class DaoSupport&lt;T,I extends Serializable&gt; {  
 @Autowired protected SessionFactory factory;  
 private Class&lt;T&gt; entityClass;  
 @SuppressWarnings("unchecked")  
 public DaoSupport() {  
  Type genType = getClass().getGenericSuperclass();  
  if(genType instanceof ParameterizedType){  
   ParameterizedType pra=(ParameterizedType)genType;  
   Type\[] t=pra.getActualTypeArguments();  
   if(t.length&gt;0){  
   entityClass=(Class&lt;T&gt;) t\[0];  
   }else{  
   entityClass=(Class&lt;T&gt;) Object.class;  
   }  
  }else{  
   entityClass=(Class&lt;T&gt;) Object.class;  
  }  
 }  
 /\**  
  * 保存数据  
  * @param entity  
  \*/  
 public void save(final T entity) {  
  factory.getCurrentSession().save(entity);  
 }  
 /\**  
  * 更新数据  
  * @param entity  
  \*/  
 public void update(final T entity){  
  factory.getCurrentSession().update(entity);  
 }  
 /\**  
  * 删除数据  
  * @param entity  
  \*/  
 public void delete(final T entity){  
  factory.getCurrentSession().delete(entity);  
 }  
 /\**  
  * 按照一定顺序获得所有记录  
  * @param orderByProperty  
  * @param isAsc  
  * @return  
  \*/  
 @SuppressWarnings("unchecked")  
 public List&lt;T&gt; listAll(String orderByProperty, boolean isAsc){  
  Criteria c = factory.getCurrentSession().createCriteria(entityClass);  
  if(isAsc){  
   return c.addOrder(Order.asc(orderByProperty)).list();  
  }else{  
   return c.addOrder(Order.desc(orderByProperty)).list();  
  }  
    
    
 }  
   
 /\**  
  * 查找t对象中属性值（非零）相同的数据记录  
  * @param t  
  * @return  
  \*/  
 @SuppressWarnings("unchecked")  
 public List&lt;T&gt; queryByAttribute(final T t){  
  Example exam=Example.create(t).excludeZeroes();  
  return  factory.getCurrentSession().createCriteria(entityClass).add(exam).list();  
    
 }  
 /\**  
  * 条件查询语句  
  * @param criterions  
  * @return  
  \*/  
 @SuppressWarnings("unchecked")  
 public &lt;X&gt; List&lt;X&gt; query(final List&lt;Criterion&gt;  criterions){  
  Criteria cri=factory.getCurrentSession().createCriteria(entityClass);  
  for(Criterion criterion:criterions){  
   cri.add(criterion);  
  }  
  return cri.list();  
    
 }  
 /\**  
  * 根据唯一编号查找记录  
  * @param id  
  * @return  
  \*/  
 @SuppressWarnings("unchecked")  
 public T get(final I id){  
  return (T) factory.getCurrentSession().get(entityClass, id);  
 }  
 public final SessionFactory getFactory() {  
  return factory;  
 }

 public final void setFactory(final SessionFactory factory) {  
  this.factory = factory;  
 }  
}