1.分页用的辅助类 import java.io.Serializable; import java.util.List; public class PageBean<T> implements Serializable { private int curPage = 1; // 当前页 private int rowsPerPage;// 每页记录条数 private int totalRows;// 总记录条数 private int totalPages;// 总页数 private List<T> list;// 当前页的记录集合 /** default constructor */ public PageBean() { } /** full constructor */ public PageBean(int curPage, int rowsPerPage, int totalRows, int totalPages, List<T> list) { this.curPage = curPage; this.rowsPerPage = rowsPerPage; this.totalRows = totalRows; this.totalPages = totalPages; this.list = list; } public long getCurPage() { return curPage < 1 ? 1 : curPage; } public void setCurPage(int curPage) { this.curPage = curPage; } public int getRowsPerPage() { return rowsPerPage; } public void setRowsPerPage(int rowsPerPage) { this.rowsPerPage = rowsPerPage; } public int getTotalRows() { return totalRows; } public void setTotalRows(int totalRows) { this.totalRows = totalRows; } public int getTotalPages() { return totalPages; } public void setTotalPages(int totalPages) { this.totalPages = totalPages; } public List<T> getList() { return list; } public void setList(List<T> list) { this.list = list; } } 2.IBaseDAO接口 import java.util.LinkedHashMap; import com.macau.util.PageBean; public interface BaseDao { /** * 添加实体 * * @param item * 实体 */ public void save(Object item); /** * 更新实体 * * @param item * 实体 */ public void update(Object item); /** * 删除实体 * * @param <T> * @param clazz * 实体类型 * @param itemId * 实体id */ public <T> void delete(Class<T> clazz, java.io.Serializable itemId); /** * 删除实体 * * @param <T> * @param clazz * 实体类型 * @param itemIds * 实体id数组 */ public <T> void delete(Class<T> clazz, java.io.Serializable[] itemIds); /** * 获取实体 * * @param <T> * @param clazz * 实体类 * @param itemId * 实体id * @return */ public <T> T find(Class<T> clazz, java.io.Serializable itemId); /** * 查询所有 * * @param <T> * @param clazz * 要查询的类型 * @return */ public <T> PageBean<T> getPageBean(Class<T> clazz); /** * 带筛选查询所有 * * @param <T> * @param clazz * 要查询的类型 * @param where * 筛选语句 eg: and o.property1 = ? and o.property2 like ? * @param queryParams * 筛选语句的参数数组 * @return */ public <T> PageBean<T> getPageBean(Class<T> clazz, String where, Object[] queryParams); /** * 无条件分页查询 * * @param <T> * @param calzz * 要查询的类型 * @param curPage * 查询页数 * @param rowsPerPage * 每页记录条数 * @return */ public <T> PageBean<T> getPageBean(Class<T> clazz, int curPage, int rowsPerPage); /** * * 带排序功能的分页查询 * * @param <T> * @param calzz * 要查询的类型 * @param curPage * 查询页数 * @param rowsPerPage * 每页记录条数 * @param orderBy * 排序条件<key:属性名, value:ASC/DESC> 使用LinkHashMap实现先添加的先排,后添加的后排 * eg: order by key1 ASC, key2 DESC * @return */ public <T> PageBean<T> getPageBean(Class<T> clazz, int curPage, int rowsPerPage, LinkedHashMap<String, String> orderBy); /** * * 带筛选功能的分页查询 * * @param <T> * @param calzz * 要查询的类型 * @param curPage * 查询页数 * @param rowsPerPage * 每页记录条数 * @param where * 筛选语句 eg: and o.property1 = ? and o.property2 like ? * @param queryParams * 筛选语句的参数数组 * @return */ public <T> PageBean<T> getPageBean(Class<T> clazz, int curPage, int rowsPerPage, String where, Object[] queryParams); /** * * 带排序、筛选功能的分页查询 * * @param <T> * @param calzz * 要查询的类型 * @param curPage * 查询页数 * @param rowsPerPage * 每页记录条数 * @param where * 筛选语句 eg: o.property1 = ?1 and o.property2 like ?2 * @param queryParams * 筛选语句的参数数组 * @param orderBy * 排序条件<key:属性名, value:ASC/DESC> 使用LinkHashMap实现先添加的先排,后添加的后排 * eg: order by key1 ASC, key2 DESC * @return */ public <T> PageBean<T> getPageBean(Class<T> clazz, int curPage, int rowsPerPage, String where, Object[] queryParams, LinkedHashMap<String, String> orderBy); } 3.BaseDAOImpl实现类 import java.util.LinkedHashMap; import org.hibernate.Query; import org.hibernate.Session; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; public class BaseDaoImpl extends HibernateDaoSupport implements BaseDao { /** * 删除实体 * * @param <T> * @param clazz * 实体类型 * @param itemId * 实体id */ public <T> void delete(Class<T> clazz, java.io.Serializable itemId) { delete(clazz, new java.io.Serializable[] { itemId }); } /** * 删除实体 * * @param <T> * @param clazz * 实体类型 * @param itemIds * 实体id数组 */ public <T> void delete(Class<T> clazz, java.io.Serializable[] itemIds) { for (java.io.Serializable id : itemIds) { super.getHibernateTemplate().delete(find(clazz, id)); } } /** * 获取实体 * * @param <T> * @param clazz * 实体类 * @param itemId * 实体id * @return */ @SuppressWarnings("unchecked") public <T> T find(Class<T> clazz, java.io.Serializable itemId) { return (T) super.getHibernateTemplate().get(clazz, itemId); } /** * 添加实体 * * @param item * 实体 */ public void save(Object item) { super.getHibernateTemplate().save(item); } /** * 更新实体 * * @param item * 实体 */ public void update(Object item) { super.getHibernateTemplate().update(item); } /** * 查询所有 * * @param <T> * @param clazz * 要查询的类型 * @return */ public <T> PageBean<T> getPageBean(Class<T> clazz) { return getPageBean(clazz, -1, -1); } /** * 带筛选查询所有 * * @param <T> * @param clazz * 要查询的类型 * @param where * 筛选语句 eg: and o.property1 = ? and o.property2 like ? * @param queryParams * 筛选语句的参数数组 * @return */ public <T> PageBean<T> getPageBean(Class<T> clazz, String where, Object[] queryParams) { return getPageBean(clazz, -1, -1, where, queryParams, null); } /** * 无条件分页查询 * * @param <T> * @param calzz * 要查询的类型 * @param curPage * 查询页数 * @param rowsPerPage * 每页记录条数 * @return */ public <T> PageBean<T> getPageBean(Class<T> clazz, int curPage, int rowsPerPage) { return getPageBean(clazz, curPage, rowsPerPage, null, null, null); } /** * * 带排序功能的分页查询 * * @param <T> * @param calzz * 要查询的类型 * @param curPage * 查询页数 * @param rowsPerPage * 每页记录条数 * @param orderBy * 排序条件<key:属性名, value:ASC/DESC> 使用LinkHashMap实现先添加的先排,后添加的后排 * eg: order by key1 ASC, key2 DESC * @return */ public <T> PageBean<T> getPageBean(Class<T> clazz, int curPage, int rowsPerPage, LinkedHashMap<String, String> orderBy) { return getPageBean(clazz, curPage, rowsPerPage, null, null, orderBy); } /** * * 带筛选功能的分页查询 * * @param <T> * @param calzz * 要查询的类型 * @param curPage * 查询页数 * @param rowsPerPage * 每页记录条数 * @param where * 筛选语句 eg: and o.property1 = ? and o.property2 like ? * @param queryParams * 筛选语句的参数数组 * @return */ public <T> PageBean<T> getPageBean(Class<T> clazz, int curPage, int rowsPerPage, String where, Object[] queryParams) { return getPageBean(clazz, curPage, rowsPerPage, where, queryParams, null); } /** * * 带排序、筛选功能的分页查询 * * @param <T> * @param calzz * 要查询的类型 * @param curPage * 查询页数 * @param rowsPerPage * 每页记录条数 * @param where * 筛选语句 eg: o.property1 = ?1 and o.property2 like ?2 * @param queryParams * 筛选语句的参数数组 * @param orderBy * 排序条件<key:属性名, value:ASC/DESC> 使用LinkHashMap实现先添加的先排,后添加的后排 * eg: order by key1 ASC, key2 DESC * @return */ @SuppressWarnings("unchecked") public <T> PageBean<T> getPageBean(Class<T> clazz, int curPage, int rowsPerPage, String where, Object[] queryParams, LinkedHashMap<String, String> orderBy) { PageBean<T> pb = new PageBean<T>(); Session session = this.getSession(); String name = getClassName(clazz);// 根据实体类型获得实体名称 String hql = "select o from " + name + " o " + (where == null ? "" : " where 1=1 " + where) + buildOrderBy(orderBy); //System.out.println(hql); Query query = session.createQuery(hql);// 创建查询对象 setQueryParams(query, queryParams);// 设置where子句中的参数 /** 根据curPage和rowsPerPage判断是否分页 */ if (curPage != -1 && rowsPerPage != -1) { int rowBegin = (curPage - 1) * rowsPerPage;// 根据要查询的页数获得开始查询的行数 query.setFirstResult(rowBegin).setMaxResults(rowsPerPage);// 设置查询开始行号和要查询记录条数 } pb.setList(query.list());// 获得记录集合 query = session.createQuery("select count (o) from " + name + " o " + (where == null ? "" : " where 1=1 " + where));// 查询对应记录总条数 setQueryParams(query, queryParams);// 设置where子句中的参数 pb.setTotalRows(((Integer) query.uniqueResult()));// 获得对应记录总条数 session.close(); /** 设定当前页其余属性值 */ if (curPage != -1 && rowsPerPage != -1) { pb.setCurPage(curPage);// 当前页数 pb.setRowsPerPage(rowsPerPage);// 每页记录条数 pb.setTotalPages(pb.getTotalRows() % pb.getRowsPerPage() > 0 ? pb .getTotalRows() / pb.getRowsPerPage() + 1 : pb.getTotalRows() / pb.getRowsPerPage());// 总页数 } else { pb.setCurPage(1);// 当前页数 pb.setRowsPerPage(pb.getTotalRows());// 每页记录条数 pb.setTotalPages(1);// 总页数 } return pb; } /** * 获得类型实体名 * * @param <T> * @param clazz * 实体类型 * @return */ protected <T> String getClassName(Class<T> clazz) { String name = clazz.getSimpleName(); return name; } /** * 拼接order by 子句 * * @param orderBy * 排序条件<key:属性名, value:ASC/DESC> * @return */ protected String buildOrderBy(LinkedHashMap<String, String> orderBy) { StringBuffer order = new StringBuffer(); if (orderBy != null && orderBy.size() > 0) { order.append(" order by "); // order by o.key1 desc, o.key2 asc for (String key : orderBy.keySet()) { order.append("o.").append(key).append(" ").append( orderBy.get(key)).append(","); } order.deleteCharAt(order.length() - 1);// 去除最后一个逗号 } return order.toString(); } /** * 设置筛选查询条件的参数 * * @param query * 查询对象 * @param queryParams * 筛选条件的参数数组 */ protected void setQueryParams(Query query, Object[] queryParams) { if (queryParams != null && queryParams.length > 0) { for (int i = 0; i < queryParams.length; i++) { query.setParameter(i, queryParams[i]); } } } }