今天把以前使用的的DAO层操作整理了下,包括分页,由于以前项目比较近,没有成为一个独立可复用的模块,整理后基本满足了可复用需求。
由于经验不足,DAO层设计比较单一,希望论坛里的大虾们,共同对此DAO修改,如果你的修改可以是DAO层更灵活,我将十分感谢,此DAO是由robin和HTT的思想和自己工作中的经验而成,再次感谢他们。
DAO顶级接口类:IBaseDao.java
package com.frame.ssh.dao.base;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import com.frame.ssh.util.PagesBean;
import com.sun.istack.internal.FinalArrayList;
/**
* Dao的顶级接口,为基本的操作提供接口
*
*
* @author zjc
* @version 1.0
* @since 1.0
*/
public interface IBaseDao<T,ID extends Serializable> {
/**
* 添加实体类
*
* @param entity:需要添加的实体类
* @return 无返回值
* */
Serializable saveEntity(final T entity);
/**
* 添加修改实体类
*
* @param entity:需要添加的实体类
* @return 无返回值
* */
void saveOrUpdateEntity(final T entity);
/**
* 修改实体类
*
* @param entity:修改持久化的对象
* @return 实现序列化的对象
* */
Serializable updateEntity(final T entity);
/**
* 删除持久化实体类
*
* @param entity:删除持久化的对象
* @return 实现序列化的对象
* */
void deleteEntity(final T entity);
/**
* 查询单个实体对象
*
* @param clazz:查询实体的CLASS
* @param id:查询实体的标示符
* @return 实体对象
* */
T getEntity(final Class<T> clazz,final ID id);
/**
* 通过主键查询实体类
*
* @param id:查询实体的标示符
* @return 实体对象
* */
T getEntityByID(final ID id);
/**
* 无分页查询所有的对象集合
*
* @param conditionMap:查询条件集合
* @return 实体对象集合
* */
List<T> findEntity(final Map<ID,T> conditionMap);
/**
* 查询所有对象
*
* @param conditionMap:查询条件集合
* @return 实体对象集合
* */
List<T> findAllEntity(final Class<T> clazz);
/**
* 带分页的查询方法,基于HQL
*
* @param entity:实体类
* @return 实体对象集合
* */
PagesBean<T> findEntityByPageHQL(final T entity,final PagesBean<T> page,final String HQL,final String condHQL);
/**
* 带分页的查询方法,基于SQL
*
* @param entity:实体类
* @return 实体对象集合
* */
PagesBean<T> findEntityByPageSQL(final T entity,final PagesBean<T> page,final String SQL,final String condSQL);
}
抽象类:AbstractBaseDaoImplement.java
package com.frame.ssh.dao.base;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import org.springframework.orm.hibernate3.HibernateTemplate;
import com.frame.ssh.util.PagesBean;
/**
* Spring 集成 Hibernate DAO 的抽象类,通过IOC将SessionFactory注入. 基本操作功能实现.
*
* @author zjc
* @version 1.0
* @since 1.0
*/
public abstract class AbstractBaseDaoImplement<T, ID extends Serializable> extends AbstractDividePage<T> implements IBaseDao<T, ID> {
private HibernateTemplate hibernateTemplate;
abstract Class<T> getEntityClazz();
HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
@Override
public void deleteEntity(final T entity) {
hibernateTemplate.delete(entity);
}
@SuppressWarnings("unchecked")
@Override
public List<T> findEntity(Map<ID, T> conditionMap) {
StringBuffer HQL = new StringBuffer();
HQL.append(" from "+getEntityClazz().getSimpleName());
HQL.append(" where 1=1 ");
if(conditionMap instanceof Map<?,?> && conditionMap.size()>0){
for (ID id : conditionMap.keySet()) {
HQL.append(" and").append(id).append(conditionMap.get(id));
}
}else{
throw new IllegalArgumentException();
}
return hibernateTemplate.find(HQL.toString());
}
@SuppressWarnings("unchecked")
@Override
public T getEntity(Class<T> clazz, ID id) {
return (T) hibernateTemplate.load(clazz, id);
}
@Override
public Serializable saveEntity(T entity) {
return hibernateTemplate.save(entity);
}
@SuppressWarnings("unchecked")
@Override
public T getEntityByID(ID id) {
return (T) hibernateTemplate.load(getEntityClazz(), id);
}
@Override
public Serializable updateEntity(T entity) {
return (Serializable) hibernateTemplate.merge(entity);
}
@SuppressWarnings("unchecked")
@Override
public List<T> findAllEntity(Class<T> clazz) {
return hibernateTemplate.loadAll(clazz);
}
@Override
public void saveOrUpdateEntity(T entity) {
hibernateTemplate.saveOrUpdate(entity);
}
@Override
public PagesBean<T> findEntityByPageHQL(T entity, PagesBean<T> page,String HQL,String condHQL) {
return super.dividePageOperation(entity, page, HQL, condHQL);
}
@Override
public PagesBean<T> findEntityByPageSQL(T entity, PagesBean<T> page,String SQL, String condSQL) {
return super.dividePageOperationSQL(entity, page, SQL, condSQL);
}
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
}
分页抽象类:AbstractDividePage.java
package com.frame.ssh.dao.base;
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.HibernateTemplate;
import com.frame.ssh.util.PagesBean;
/**
* 分页抽象类,提供HQL、SQL两种方式.
*
* @author zjc
* @version 1.0
* @since 1.0
*/
public abstract class AbstractDividePage<T> {
private static int TOTAL_COUNT = 0; // 总记录数
private static int BEGIN_INDEX = 0; // 起始位置
private static int TOTAL_PAGE = 0; // 总页数
private static int PAGE_COUNT = 0; // 每页记录数
private static int CURRENT_PAGE= 0; // 当前第几页
private PagesBean<T> pageBean;
abstract HibernateTemplate getHibernateTemplate();
/**
* 基于HQL的分页方法
* @param entity 实体分页对象
* @param page 分页类
* @return 分页后的对象
* */
@SuppressWarnings("unchecked")
PagesBean<T> dividePageOperation(T entity,PagesBean<T> page,final String HQL,String condHQL) {
if(entity == null || page == null || HQL == null || condHQL == null){
throw new IllegalArgumentException("传入参数为空!");
}
TOTAL_COUNT = ((Long) this.getHibernateTemplate().find(condHQL).iterator().next()).intValue();
BEGIN_INDEX = PagesBean.getIndex(page.getPageSize(), page.getCurrentPage());
TOTAL_PAGE = PagesBean.countTotalPage(page.getPageSize(), page.getTotalCount());
CURRENT_PAGE = PagesBean.countCurrentPage(page.getCurrentPage());
PAGE_COUNT = page.getPageSize();
pageBean = new PagesBean<T>();
pageBean.setTotalCount(TOTAL_COUNT);
pageBean.setCurrentPage(CURRENT_PAGE);
pageBean.setPageSize(PAGE_COUNT);
pageBean.setTotalPage(TOTAL_PAGE);
pageBean.setUrl(page.getUrl());
List<T> list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
return session.createQuery(HQL).setFirstResult(BEGIN_INDEX).setMaxResults(PAGE_COUNT);
}
});
pageBean.setList(list);
return pageBean;
}
/**
* 基于HQL的分页方法
* @param entity 实体分页对象
* @param page 分页类
* @return 分页后的对象
* */
@SuppressWarnings("unchecked")
PagesBean<T> dividePageOperationSQL(T entity,PagesBean<T> page,final String SQL,final String condSQL) {
if(entity == null || page == null || SQL == null || condSQL == null){
throw new IllegalArgumentException("传入参数为空!");
}
Object object = this.getHibernateTemplate().execute(new HibernateCallback() {
@Override
public Object doInHibernate(Session session) throws HibernateException,SQLException {
return session.createSQLQuery(condSQL).uniqueResult();
}
});
TOTAL_COUNT = Integer.valueOf(object.toString());
BEGIN_INDEX = PagesBean.getIndex(page.getPageSize(), page.getCurrentPage());
TOTAL_PAGE = PagesBean.countTotalPage(page.getPageSize(), page.getTotalCount());
CURRENT_PAGE = PagesBean.countCurrentPage(page.getCurrentPage());
PAGE_COUNT = page.getPageSize();
pageBean = new PagesBean<T>();
pageBean.setTotalCount(TOTAL_COUNT);
pageBean.setCurrentPage(CURRENT_PAGE);
pageBean.setPageSize(PAGE_COUNT);
pageBean.setTotalPage(TOTAL_PAGE);
pageBean.setUrl(page.getUrl());
List<T> list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
return session.createSQLQuery(SQL).setFirstResult(BEGIN_INDEX).setMaxResults(PAGE_COUNT);
}
});
pageBean.setList(list);
return pageBean;
}
}
分页基本类:PagesBean.java
package com.frame.ssh.util;
import java.util.List;
/**
* 分页基本类
*
* @author zjc
* @version 1.0
* @since 1.0
*/
public final class PagesBean<T> {
private int totalCount;
private int pageSize;
private int currentPage;
private int totalPage;
private List<T> list;
private String url;
private String key;
private String value;
private String remark;
private String remark1;
private String remark2;
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
/**
* 计算总页数
* @param pageSize 每页记录数
* @param totalCount 总记录数
* @return 总页数
*/
public static int countTotalPage(final int pageSize,final int totalCount){
int totalPage = totalCount % pageSize == 0 ? totalCount/pageSize : totalCount/pageSize+1;
return totalPage;
}
/**
* 计算当前页开始记录
* @param pageSize 每页记录数
* @param currentPage 当前第几页
* @return 当前页开始记录号
*/
public static int getIndex(final int pageSize,final int currentPage){
final int startCount = pageSize*(currentPage-1);
return startCount;
}
/**
* 计算当前页,若为0或者请求的URL中没有"?page=",则用1代替
* @param page 传入的参数(可能为空,即0,则返回1)
* @return 当前页
*/
public static int countCurrentPage(int page){
final int curPage = (page==0?1:page);
return curPage;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getRemark1() {
return remark1;
}
public void setRemark1(String remark1) {
this.remark1 = remark1;
}
public String getRemark2() {
return remark2;
}
public void setRemark2(String remark2) {
this.remark2 = remark2;
}
}
再次希望大家对此提出你宝贵意见,我将非常感谢。