首页>代码>spring+springsecurity+hibernate+struts2 权限代码数据库>/TestSecurity7/src/com/test/dao/impl/BaseDaoImpl.java
                
                package com.test.dao.impl;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;
import com.test.dao.BaseDao;
/**
 * Dao实现类 - Dao实现类基类
 */
@Repository
public class BaseDaoImpl<T, PK extends Serializable> extends HibernateDaoSupport implements BaseDao<T, PK> {
	private Class<T> entityClass;
	protected SessionFactory mySessionFactory;
	@SuppressWarnings("unchecked")
	public BaseDaoImpl() {
		this.entityClass = null;
        Class c = getClass();
        Type type = c.getGenericSuperclass();
        if (type instanceof ParameterizedType) {
            Type[] parameterizedType = ((ParameterizedType) type).getActualTypeArguments();
            this.entityClass = (Class<T>) parameterizedType[0];
        }
	}
	@Resource
	public void setMySessionFactory(SessionFactory sessionFactory) {
		this.mySessionFactory = sessionFactory;
		super.setSessionFactory(sessionFactory);
	}
	protected Session getMySession() {
		return mySessionFactory.getCurrentSession();
	}
	@SuppressWarnings("unchecked")
	public T get(PK id) {
		Assert.notNull(id, "id is required");
		return (T) getHibernateTemplate().get(entityClass, id);
	}
	@SuppressWarnings("unchecked")
	public T load(PK id) {
		Assert.notNull(id, "id is required");
		return (T) getHibernateTemplate().load(entityClass, id);
	}
	@SuppressWarnings("unchecked")
	public List<T> get(PK[] ids) {
		Assert.notEmpty(ids, "ids must not be empty");
		String hql = "from " + entityClass.getName() + " as model where model.id in("+ids+")";
		List<T> list = getHibernateTemplate().find(hql);
		return list;
	}
	@SuppressWarnings("unchecked")
	public T get(String propertyName, Object value) {
		Assert.hasText(propertyName, "propertyName must not be empty");
		Assert.notNull(value, "value is required");
		String hql = "from " + entityClass.getName() + " as model where model." + propertyName + " = '"+value+"'";
		List<T> list =  getHibernateTemplate().find(hql);
		if(list.size()>0){
			return list.get(0);
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<T> getList(String propertyName, Object value) {
		Assert.hasText(propertyName, "propertyName must not be empty");
		Assert.notNull(value, "value is required");
		String hql = "from " + entityClass.getName() + " as model where model." + propertyName + " = '"+value+"'";
		System.out.println(hql);
		List<T> list = getHibernateTemplate().find(hql);
		return list;
	}
	@SuppressWarnings("unchecked")
	public List<T> getAll() {
		String hql = "from " + entityClass.getName();
		List<T> list = getHibernateTemplate().find(hql);
		return list;
	}
	
	public Long getTotalCount() {
		String hql = "select count(*) from " + entityClass.getName();
		return (Long) getHibernateTemplate().find(hql).get(0);
	}
	public boolean isUnique(String propertyName, Object oldValue, Object newValue) {
		Assert.hasText(propertyName, "propertyName must not be empty");
		Assert.notNull(newValue, "newValue is required");
		if (newValue == oldValue || newValue.equals(oldValue)) {
			return true;
		}
		if (newValue instanceof String) {
			if (oldValue != null && StringUtils.equalsIgnoreCase((String) oldValue, (String) newValue)) {
				return true;
			}
		}
		T object = get(propertyName, newValue);
		return (object == null);
	}
	
	public boolean isExist(String propertyName, Object value) {
		Assert.hasText(propertyName, "propertyName must not be empty");
		Assert.notNull(value, "value is required");
		T object = get(propertyName, value);
		return (object != null);
	}
	@SuppressWarnings("unchecked")
	public PK save(T entity) {
		Assert.notNull(entity, "entity is required");
		return (PK) getHibernateTemplate().save(entity);
	}
	public void update(T entity) {
		Assert.notNull(entity, "entity is required");
		getHibernateTemplate().update(entity);
	}
	public void delete(T entity) {
		Assert.notNull(entity, "entity is required");
		getHibernateTemplate().delete(entity);
	}
	public void delete(PK id) {
		Assert.notNull(id, "id is required");
		T entity = load(id);
		getHibernateTemplate().delete(entity);
	}
	public void delete(PK[] ids) {
		Assert.notEmpty(ids, "ids must not be empty");
		for (PK id : ids) {
			T entity = load(id);
			getHibernateTemplate().delete(entity);
		}
	}
	public void flush() {
		getHibernateTemplate().flush();
	}
	public void clear() {
		getHibernateTemplate().clear();
	}
	public void evict(Object object) {
		Assert.notNull(object, "object is required");
		getHibernateTemplate().evict(object);
	}
}
最近下载更多
                
                wanglinddad     LV55
                2022年4月25日
            
            
        
                ouyangzhiming     LV1
                2020年8月26日
            
            
        
                1057706707     LV9
                2020年6月14日
            
            
        
                ccknow     LV4
                2019年8月12日
            
            
        
                aa991215     LV17
                2019年6月1日
            
            
        
                sweetyy     LV8
                2019年5月20日
            
            
        
                nephele     LV3
                2019年3月14日
            
            
        
                baihongdoudou     LV5
                2019年3月8日
            
            
        
                我一直在笑     LV10
                2018年10月15日
            
            
        
                纳兰倾城     LV4
                2018年8月25日
            
            
        
最近浏览更多
                
                ttg2025     LV3
                7月19日
            
            
        
                ma406805131     LV19
                2024年6月15日
            
            
        
                751615606    
                2024年3月14日
            
            
                    暂无贡献等级
            
        
                ewan007     LV30
                2023年7月4日
            
            
        
                hesu2020     LV1
                2023年3月22日
            
            
        
                zdmxjxj     LV11
                2022年12月7日
            
            
        
                wanglinddad     LV55
                2022年4月24日
            
            
        
                regedit418     LV12
                2022年2月10日
            
            
        
                17704627276     LV17
                2021年11月30日
            
            
        
                212828939     LV16
                2021年11月30日
            
            
        
                
                
                