public <T> List<T> listForPagenate(Criteria criteria,PaginatedListImpl command){
if ( command.getSortCriterion() != null) {
if (command.getSortDirection().equals(SortOrderEnum.ASCENDING)) {
criteria.addOrder(Order.asc(command.getSortCriterion()));
}
if (command.getSortDirection().equals(SortOrderEnum.DESCENDING)) {
criteria.addOrder(Order.desc(command.getSortCriterion()));
}
}
criteria.setFirstResult(command.getFirstRecordIndex());
criteria.setMaxResults(command.getPageSize());
List<T> list = list(criteria);
return list;
}
public <T> List<T> listForNonPagenate(Criteria criteria,PaginatedListImpl command){
if ( command.getSortCriterion() != null) {
if (command.getSortDirection().equals(SortOrderEnum.ASCENDING)) {
criteria.addOrder(Order.asc(command.getSortCriterion()));
}
if (command.getSortDirection().equals(SortOrderEnum.DESCENDING)) {
criteria.addOrder(Order.desc(command.getSortCriterion()));
}
}
List<T> list = list(criteria);
return list;
}
public int getRowNumberByCriteria(Criteria criteria){
Projection projection = null;
ResultTransformer resultTransformer = null;
if( criteria instanceof CriteriaImpl ){
projection = ((CriteriaImpl)criteria).getProjection();
resultTransformer = ((CriteriaImpl)criteria).getResultTransformer();
}
Integer count = (Integer)criteria.setProjection(Projections.rowCount()).uniqueResult();
criteria.setProjection(projection)
.setResultTransformer(resultTransformer);
return count;
}
dataDictionaryCommand.setTotalNumberOfRows(getRowNumberByCriteria(criteria));
List<SystemDictionary> results = listForPagenate(criteria,dataDictionaryCommand);
dataDictionaryCommand.setList(results);
package com.accor.crm.util;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.displaytag.properties.SortOrderEnum;
/**
* <code>com.gorti.project.web.ui.action.PaginatedListImpl</code> implemnts
* </code>com.gorti.project.web.ui.action.IExtendedPaginatedList</code> This
* class can be used for pagination purpose. This class depends upon
* HttpServletRequest object. To be used by Controllers in case of Http
* requests.
*
* @author
*/
public class PaginatedListImpl implements ExtendedPaginatedList {
private Integer pageInput;
/** current page index, starts at 0 */
private int index;
/** number of results per page (number of rows per page to be displayed ) */
private int pageSize = 20;
/** total number of results (the total number of rows ) */
private int fullListSize;
/** list of results (rows found ) in the current page */
@SuppressWarnings("unchecked")
private List list;
/** default sorting order */
private SortOrderEnum sortDirection = SortOrderEnum.ASCENDING;
/** sort criteria (sorting property name) */
private String sortCriterion;
/** Http servlet request * */
private HttpServletRequest request;
/** default constructor * */
public PaginatedListImpl() {
}
/**
* Create <code>PaginatedListImpl</code> instance using the
* <code>HttpServletRequest</code> object.
*
* @param request
* <code>HttpServletRequest</code> object.
*/
/**
* Create <code>PaginatedListImpl</code> instance using the
* <code>HttpServletRequest</code> object.
*
* @param request
* <code>HttpServletRequest</code> object.
* @param pageSize
* the page size - the total number of rows per page.
*/
public PaginatedListImpl(HttpServletRequest request, int pageSize) {
sortCriterion = request
.getParameter(ExtendedPaginatedList.IRequestParameters.SORT);
sortDirection = ExtendedPaginatedList.IRequestParameters.DESC
.equals(request
.getParameter(ExtendedPaginatedList.IRequestParameters.DIRECTION)) ? SortOrderEnum.DESCENDING
: SortOrderEnum.ASCENDING;
pageSize = pageSize != 0 ? pageSize : DEFAULT_PAGE_SIZE;
String page = request
.getParameter(ExtendedPaginatedList.IRequestParameters.PAGE);
index = page == null ? 0 : Integer.parseInt(page) - 1;
}
/**
* Create <code>PaginatedListImpl</code> instance .
*
* @param pageSize
* the page size - the total number of rows per page.
* @return <code>ExtendedPaginatedList</code> instance.
* @throws Exception -
* problem while creating paginatedlist object.
*/
public ExtendedPaginatedList getPaginatedListObject(int pageSize)
throws Exception {
if (request == null) {
throw new Exception(
"Cannot create paginated list. Depends upon HttpServletRequest.");
}
return new PaginatedListImpl(request, pageSize);
}
/**
* Set the non-null <code>HttpServletRequest</code> object.
*
* @param request
* a <code>HttpServletRequest</code> object.
*/
public void setRequest(HttpServletRequest request) {
this.request = request;
}
public int getFirstRecordIndex() {
return index * pageSize;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
@SuppressWarnings("unchecked")
public List getList() {
return list;
}
@SuppressWarnings("unchecked")
public void setList(List results) {
this.list = results;
}
public int getFullListSize() {
return fullListSize;
}
public void setTotalNumberOfRows(int total) {
this.fullListSize = total;
}
public int getTotalPages() {
return (int) Math.ceil(((double) fullListSize) / pageSize);
}
public int getObjectsPerPage() {
return pageSize;
}
public int getPageNumber() {
return index + 1;
}
public String getSearchId() {
// Not implemented for now.
// This is required, if we want the ID to be included in the paginated
// purpose.
return null;
}
public String getSortCriterion() {
return sortCriterion;
}
public SortOrderEnum getSortDirection() {
return sortDirection;
}
public void setSortCriterion(String sortCriterion) {
this.sortCriterion = sortCriterion;
}
public void setSortDirection(SortOrderEnum sortDirection) {
this.sortDirection = sortDirection;
}
public Integer getPageInput() {
return pageInput;
}
public void setPageInput(Integer pageInput) {
this.pageInput = pageInput;
}
}
有时间再进行改进...
分享到:
相关推荐
NULL 博文链接:https://zhouhaitao.iteye.com/blog/1123917
使用hibernate对分页的一个小封装,方便初学者参考.
HibernateUtil 分页 增删改查 封装 HibernateUtil 分页 增删改查 封装 HibernateUtil 分页 增删改查 封装
对hibernate的数据库操作封装: 1.分页查询 2.保存对象 3.保存对象组 4. 更新对象 5.更新对象组 6.保存或更新对象 7.保存或更新对象组 8. 删除对象 9. 删除对象组 10.删除对象(此类的所有对象) 11.删除(HQL) 12....
教学-传智播客-项目视频经典之作巴巴运动网106集-28将Web层分页封装成通用模块源代码 所需要的jar文件: (一)、Hibernate: 位于 "\hibernate-distribution-3.3.2.GA" 目录下的jar文件: hibernate3.jar 位于 "\...
教学-传智播客-项目视频经典之作巴巴运动网106集-28将Web层分页封装成通用模块源代码 所需要的jar文件: (一)、Hibernate: 位于 "\hibernate-distribution-3.3.2.GA" 目录下的jar文件: hibernate3.jar 位于 "\...
spring4+hibernate4 整合,封装BaseDao增删改查以及分页,,支持jdk1.7,不支持jdk1.8,数据源使用c3p0
利用ext3+struts2+hibernate+spring的CRUD+分页这四个框架实现crud,详细说明grid的用法,
hibernate概述,hibernate入门Demo,hibernate配置文件详解(全局配置,实体类映射配置),配置...hibernate查询方式概述,HQL查询,QBC查询,分页,结果集封装方式 ,高级查询 查询的优化,一级缓存,二级缓存,批量查询,注解方式
这个jar包太好了 真是神通啊,它封装了hibernate分页 请大家在项目中 用一下 挺好用的 我自己创建的名称:FenYe.jar 欢迎大家下载!!!!
该文档提供了两种分页方法:其中一种是hibernate提供的原始分页方法,另一种是自己封装的使用于 spring hibernate 的通用分页方法。如有不妥,欢迎指正 !
利用Hibernate技术实现DAO模式;利用存储过程封装SQL语句实现分页
整合ssh的分页,简单易用,使用hibernate的分页,封装了分页组件,轻松调用
SpringSide对Hibernate做了三层封装: 第一层:HibernateGenericDao,基于spring的HibernateDaoSupport,但加入了分页函数与各种Finder函数,并使用泛型避免了返回值强制类型转换。 第二层:HibernateEntityDao,...
struts2,hibernate,mybatis实现登录验证码,分页封装。条件查询需改善,如有不对的地方,还望大家给出意见
提供常用的数据查询,修改,查询,分页查询接口,简化在使用Hibernate的配置,只需要配置数据连接和entity的扫描包即可使用,支持直接在spring boot中使用。详细的文档查看 wechat-plugin 对微信公众号中的...
jdbc封装(实现对实体的增删改查[分页]),辅助学习Hibernate 包含三个文件夹,分别是: code-access实现 是用access实现的,本意是access方便,就一个文件,方便部署。但access有好多不支持,就写成这样.主要是可参考Dao...
ssh 分页,作为了基类。它的子类可以只要继承父类就行了。封装性强。
基于hibernate封装的BaseDao,有完整的增删改查,分页,执行hql sql 方法 约 20个方法,详情地址 https://blog.csdn.net/qq_41861261/article/details/85595872
不同数据库中分页的代码实现,常用的是方案1.这个不需要多说! 方案2有局限性。 hibernate对于分页就比较直接,Query接口提供有封装好的接口。