Spring Hibernate 实现BaseDao详解编程语言

Spring4.0, Hibernate4.0

 

BaseDao    

package com.school.dao; 
 
import java.io.Serializable; 
import java.util.List; 
 
import com.school.util.PageList; 
 
/** 
 *  
 * @author Fandy 
 * 
 */ 
public interface BaseDao { 
 
	/** 
	 *  
	 * @param hql 
	 * @return 
	 */ 
	<T> T findObject(String hql); 
	 
	/** 
	 *  
	 * @param hql 
	 * @param objects 
	 * @return 
	 */ 
	<T> T findObject(String hql, Object...objects); 
	 
	/** 
	 *  
	 * @param cls 
	 * @param id 
	 * @return 
	 */ 
	<T> T findObject(Class<T> cls, Serializable id); 
	 
	/** 
	 *  
	 * @param sql 
	 * @return 
	 */ 
	<T> T findObjectBySql(String sql); 
	 
	/** 
	 *  
	 * @param sql 
	 * @param objects 
	 * @return 
	 */ 
	<T> T findObjectBySql(String sql, Object...objects); 
	 
	/** 
	 *  
	 * @param hql 
	 * @return 
	 */ 
	<T> List<T> findList(String hql); 
	 
	/** 
	 *  
	 * @param hql 
	 * @param objects 
	 * @return 
	 */ 
	<T> List<T> findList(String hql, Object...objects); 
	 
	/** 
	 *  
	 * @param cls 
	 * @return 
	 */ 
	<T> List<T> findList(Class<T> cls); 
	 
	/** 
	 *  
	 * @param sql 
	 * @return 
	 */ 
	<T> List<T> findListBySql(String sql); 
	 
	/** 
	 *  
	 * @param sql 
	 * @param objects 
	 * @return 
	 */ 
	<T> List<T> findListBySql(String sql, Object...objects); 
	 
	/** 
	 *  
	 * @param obj 
	 */ 
	<T> void saveObject(T obj); 
	 
	/** 
	 *  
	 * @param obj 
	 */ 
	<T> void updateObject(T obj); 
	 
	/** 
	 *  
	 * @param obj 
	 */ 
	<T> void saveOrUpdateObject(T obj); 
	 
	/** 
	 *  
	 * @param sql 
	 * @return 
	 */ 
	int executeSql(String sql); 
	 
	/** 
	 *  
	 * @param sql 
	 * @param objects 
	 * @return 
	 */ 
	int executeSql(String sql, Object...objects); 
	 
	/** 
	 *  
	 * @param hql 
	 * @return 
	 */ 
	int coutObjects(String hql); 
	 
	/** 
	 *  
	 * @param hql 
	 * @param objects 
	 * @return 
	 */ 
	int countObjects(String hql, Object...objects); 
	 
	/** 
	 *  
	 * @param hql 
	 * @param page 
	 * @param rows 
	 * @return 
	 */ 
	<T> PageList<T> findPageList(String hql, int page, int rows); 
	 
	/** 
	 *  
	 * @param hql 
	 * @param page 
	 * @param rows 
	 * @param objects 
	 * @return 
	 */ 
	<T> PageList<T> findPageList(String hql, int page, int rows, Object...objects); 
	 
	 
} 

BaseDao实现类    

package com.school.dao.impl; 
 
/** 
 *  
 * @author Fandy 
 * 
 */ 
 
import java.io.Serializable; 
import java.util.List; 
 
import org.hibernate.Query; 
import org.hibernate.ScrollableResults; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Repository; 
 
import com.school.dao.BaseDao; 
import com.school.util.PageList; 
 
@SuppressWarnings("unchecked") 
@Repository("baseDao") 
public class BaseDaoImpl implements BaseDao { 
 
	@Autowired 
	private SessionFactory sessionFactory; 
 
	Session getSession() { 
		return sessionFactory.getCurrentSession(); 
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see com.school.dao.BaseDao#findObject(java.lang.String) 
	 */ 
	@Override 
	public <T> T findObject(String hql) { 
		List<T> list = findList(hql); 
		return (null == list || list.size() == 0) ? null : list.get(0); 
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see com.school.dao.BaseDao#findObject(java.lang.String, 
	 * java.lang.Object[]) 
	 */ 
	@Override 
	public <T> T findObject(String hql, Object... objects) { 
		List<T> list = findList(hql, objects); 
		return (null == list || list.size() == 0) ? null : list.get(0); 
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see com.school.dao.BaseDao#findObject(java.lang.Class, 
	 * java.io.Serializable) 
	 */ 
	@Override 
	public <T> T findObject(Class<T> cls, Serializable id) { 
		return (T) getSession().get(cls, id); 
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see com.school.dao.BaseDao#findObjectBySql(java.lang.String) 
	 */ 
	@Override 
	public <T> T findObjectBySql(String sql) { 
		List<T> list = findListBySql(sql); 
		return (null == list || list.size() == 0) ? null : list.get(0); 
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see com.school.dao.BaseDao#findObjectBySql(java.lang.String, 
	 * java.lang.Object[]) 
	 */ 
	@Override 
	public <T> T findObjectBySql(String sql, Object... objects) { 
		List<T> list = findListBySql(sql, objects); 
		return (null == list || list.size() == 0) ? null : list.get(0); 
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see com.school.dao.BaseDao#findList(java.lang.String) 
	 */ 
	@Override 
	public <T> List<T> findList(String hql) { 
		Query query = getSession().createQuery(hql); 
		return query.list(); 
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see com.school.dao.BaseDao#findList(java.lang.String, 
	 * java.lang.Object[]) 
	 */ 
	@Override 
	public <T> List<T> findList(String hql, Object... objects) { 
		Query query = getSession().createQuery(hql); 
		setParameter(query, objects); 
		return query.list(); 
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see com.school.dao.BaseDao#findList(java.lang.Class) 
	 */ 
	@Override 
	public <T> List<T> findList(Class<T> cls) { 
		String hql = "FROM " + cls.getName(); 
		return findList(hql); 
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see com.school.dao.BaseDao#findListBySql(java.lang.String) 
	 */ 
	@Override 
	public <T> List<T> findListBySql(String sql) { 
		Query query = getSession().createSQLQuery(sql); 
		return query.list(); 
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see com.school.dao.BaseDao#findListBySql(java.lang.String, 
	 * java.lang.Object[]) 
	 */ 
	@Override 
	public <T> List<T> findListBySql(String sql, Object... objects) { 
		Query query = getSession().createSQLQuery(sql); 
		setParameter(query, objects); 
		return query.list(); 
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see com.school.dao.BaseDao#saveObject(java.lang.Object) 
	 */ 
	@Override 
	public <T> void saveObject(T obj) { 
		getSession().save(obj); 
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see com.school.dao.BaseDao#updateObject(java.lang.Object) 
	 */ 
	@Override 
	public <T> void updateObject(T obj) { 
		getSession().update(obj); 
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see com.school.dao.BaseDao#saveOrUpdateObject(java.lang.Object) 
	 */ 
	@Override 
	public <T> void saveOrUpdateObject(T obj) { 
		getSession().saveOrUpdate(obj); 
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see com.school.dao.BaseDao#executeSql(java.lang.String) 
	 */ 
	@Override 
	public int executeSql(String sql) { 
		Query query = getSession().createSQLQuery(sql); 
		return query.executeUpdate(); 
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see com.school.dao.BaseDao#executeSql(java.lang.String, 
	 * java.lang.Object[]) 
	 */ 
	@Override 
	public int executeSql(String sql, Object... objects) { 
		Query query = getSession().createSQLQuery(sql); 
		setParameter(query, objects); 
		return query.executeUpdate(); 
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see com.school.dao.BaseDao#coutObjects(java.lang.String) 
	 */ 
	@Override 
	public int coutObjects(String hql) { 
		Query query = getSession().createQuery(hql); 
		ScrollableResults sr = query.scroll(); 
		sr.last(); 
		return sr.getRowNumber() == -1 ? 0 : sr.getRowNumber() + 1; 
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see com.school.dao.BaseDao#countObjects(java.lang.String, 
	 * java.lang.Object[]) 
	 */ 
	@Override 
	public int countObjects(String hql, Object... objects) { 
		Query query = getSession().createQuery(hql); 
		setParameter(query, objects); 
		ScrollableResults sr = query.scroll(); 
		sr.last(); 
		return sr.getRowNumber() == -1 ? 0 : sr.getRowNumber() + 1; 
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see com.school.dao.BaseDao#findPageList(java.lang.String, int, int) 
	 */ 
	@Override 
	public <T> PageList<T> findPageList(String hql, int page, int rows) { 
		Query query = getSession().createQuery(hql); 
		return findPageList(query, page, rows); 
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see com.school.dao.BaseDao#findPageList(java.lang.String, int, int, 
	 * java.lang.Object[]) 
	 */ 
	@Override 
	public <T> PageList<T> findPageList(String hql, int page, int rows, 
			Object... objects) { 
		Query query = getSession().createQuery(hql); 
		setParameter(query, objects); 
		return findPageList(query, page, rows); 
	} 
 
	<T> PageList<T> findPageList(Query query, int page, int rows) { 
		ScrollableResults sr = query.scroll(); 
		sr.last(); 
		int count = sr.getRowNumber() == -1 ? 0 : sr.getRowNumber() + 1; 
		query.setFirstResult((page - 1) * rows); 
		query.setMaxResults(page * rows); 
		return new PageList<T>(page, rows, count, query.list()); 
	} 
 
	void setParameter(Query query, Object... objects) { 
		for (int i = 0; i < objects.length; i++) { 
			query.setParameter(i, objects[i]); 
		} 
	} 
} 

[代码]PageList    

package com.school.util; 
/** 
 * @author Fandy 
 */ 
import java.util.List; 
 
public class PageList<T> { 
 
	private int page; 
	private int rows; 
	private int total; 
	private List<T> list; 
 
	public PageList() { 
		super(); 
	} 
 
	public PageList(int page, int rows, int total, List<T> list) { 
		super(); 
		this.page = page; 
		this.rows = rows; 
		this.total = total; 
		this.list = list; 
	} 
 
	public int getPage() { 
		return page; 
	} 
 
	public void setPage(int page) { 
		this.page = page; 
	} 
 
	public int getRows() { 
		return rows; 
	} 
 
	public void setRows(int rows) { 
		this.rows = rows; 
	} 
 
	public List<T> getList() { 
		return list; 
	} 
 
	public void setList(List<T> list) { 
		this.list = list; 
	} 
 
	public int size(){ 
		return null==list?0:list.size(); 
	} 
 
	public int getTotal() { 
		return total; 
	} 
 
	public void setTotal(int total) { 
		this.total = total; 
	} 
 
} 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/7990.html

(0)
上一篇 2021年7月18日 19:15
下一篇 2021年7月18日 19:15

相关推荐

发表回复

登录后才能评论