我的hibernate通用dao层

1.BaseDao类:底层的dao接口,提供最基本的CRUD和分页


import java.io.Serializable;
import java.util.List;

import com.xxx.utils.PageUtil;

public interface BaseDao {
	public void saveEntity(E e);  

	public void updateEntity(E e);  

	public void deleteEntity(E e);
	
	public void deleteEntityById(Serializable id);

	public E loadEntity(final Class class1, final Serializable id);

	public E loadEntityById(Serializable id);

	public List loadListEntities(PageUtil page);
}

2.BaseDaoImpl类:BaseDao的实现类

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.xxx.utils.PageUtil;

public class BaseDaoImpl extends HibernateDaoSupport implements BaseDao {
	
	private Class cls;   
	
	
	public BaseDaoImpl(){
		// 就是要获取E的真实数据类型
		this.cls =  (Class)((ParameterizedType)this.getClass().getGenericSuperclass())
        .getActualTypeArguments()[0];
		
	}


	public void saveEntity(E e) {
		this.getHibernateTemplate().save(e);
		
	}


	public void updateEntity(E e) {
		this.getHibernateTemplate().update(e);
		
	}


	public void deleteEntity(E e) {
		this.getHibernateTemplate().delete(e);
		
	}


	public E loadEntity(final Class class1,final Serializable id){
		return this.getHibernateTemplate().get(class1, id);
		
	}

	@SuppressWarnings("unchecked")
	public E loadEntityById(Serializable id) {
		return (E) loadEntity(cls,id);
	}


	@SuppressWarnings("unchecked")
	public List loadListEntities(final PageUtil page) {
		return this.getHibernateTemplate().executeFind(new HibernateCallback>() {
			public List doInHibernate(Session session) throws HibernateException, SQLException {
				Long totalL = (Long) session.createQuery("select count(*) from "+page.getModelName()).uniqueResult();
				page.setTotalCount(totalL.intValue());
				return session.createQuery("from " +page.getModelName())
						.setFirstResult((page.getPageNow()-1)*page.getPageSize())
						.setMaxResults(page.getPageSize()).list();
			}
		});
	}

	public void deleteEntityById(Serializable id) {
		deleteEntity(loadEntityById(id));
	}

	
}

3.UserDao:User对应的dao接口

import com.xxx.model.User;
public interface UserDao extends BaseDao{
    //这里还可以自己添加特殊的接口
}

4.UserDaoImpl:UserDao的实现类

import org.springframework.stereotype.Component;

import com.xxx.model.User;
@Component("userDao")   //Spring的注解给新手的说明哦
public class UserDaoImpl extends BaseDaoImpl implements UserDao{
    //这里还可以自己添加特殊的接口的实现
}

5.PageUtil,自己写得分页辅助类,为hibernate的分页使用

import org.springframework.stereotype.Component;

@Component("page")  //spring注解,交给spring管理师最好不过的了,因为是单例嘛,呵呵
public class PageUtil {
	private static PageUtil page = null;
	private String modelName;   //实体名字,比如User,就是写User
	private int pageNow;        //当前页
	private int pageSize;       //每页显示个数
	private int totalCount;     //总数

	private PageUtil() {

	}

	public int getPages() {     //获取总页数
		return totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
	}

	public static PageUtil getInstance() {
		if (null == page) {
			page = new PageUtil();
		}
		return page;
	}

	public static PageUtil getPage() {
		return page;
	}

	public static void setPage(PageUtil page) {
		PageUtil.page = page;
	}

	public String getModelName() {
		return modelName;
	}

	public void setModelName(String modelName) {
		this.modelName = modelName;
	}

	public int getPageNow() {
		return pageNow;
	}

	public void setPageNow(int pageNow) {
		this.pageNow = pageNow;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

}

以上就是个人的通用dao层,只是给大家抛砖,希望大侠们给点意见

至于ssh的引入jar包和各配置文件,请关注我另外的文章,谢谢


你可能感兴趣的:(我的hibernate通用dao层)