实现分页查询功能
建立PageBean实体类,用于记录页面信息
通过构造方法public PageBean(Integer currentPage, Integer totalCount, Integer pageSize)
传入当前页面数,总记录数,总页数。
package cn.lzm.util;
import java.util.List;
public class PageBean {
//当前页数
private Integer currentPage;
//总记录数
private Integer totalCount;
//页面记录数
private Integer pageSize;
// 总页数
private Integer totalPage;
//返回查询对象
private List list;
public PageBean(Integer currentPage, Integer totalCount, Integer pageSize) {
this.totalCount = totalCount;
this.pageSize=pageSize;
this.currentPage=currentPage;
//Integer返回null;
if(this.currentPage==null)
{
this.currentPage=1;
}
if(this.pageSize==null)
{
//若每页条数没有指定,默认3条
this.pageSize = 3;
}
//计算总页数
this.totalPage=(this.totalCount+this.pageSize-1)/this.pageSize;
//不能小于1
if(this.currentPage<1)
{
this.currentPage=1;
}
//判断当前页数是否超出范围
if(this.currentPage>this.totalPage)
{
this.currentPage=totalPage;
}
}
//计算起始索引
public int getStart()
{
return (this.currentPage-1)*this.pageSize;
}
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getTotalCount() {
return totalCount;
}
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
CustomerAction中方法
DetachedCriteria翻译为离线查询,他是可以脱离session条件查询的对象,
大家都知道Criteria查询需要通过session才能查询,而DetachedCriteria离线查询
可以在其他层进行封装,这个比较有用的,在ssh整合的时候,在做一些特别复杂的查询的时候
我们可以直接在WEB层进行封装,然后再传递给Dao层,这样就会省去很多麻烦,不用在WEB获取很多数据,
然后传送给service层,再传给Dao层了
package cn.lzm.web.action;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.type.IntegerType;
import org.junit.Test;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import cn.lzm.domain.Customer;
import cn.lzm.domain.User;
import cn.lzm.service.CustomerService;
import cn.lzm.service.UserService;
import cn.lzm.util.PageBean;
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
//微不足道,不用spring
private Customer customer= new Customer();
private CustomerService customerservice;
public void setCustomerservice(CustomerService customerservice) {
this.customerservice = customerservice;
}
private Integer currentPage;
private Integer pageSize;
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
@Override
public Customer getModel() {
// TODO Auto-generated method stub
return customer;
}
public String list() throws Exception {
//封装离线查询对象
DetachedCriteria dCriteria=DetachedCriteria.forClass(Customer.class);
//判断并封装参数
if(StringUtils.isNoneBlank(customer.getCust_name()))
{
dCriteria.add(Restrictions.like("cust_name", "%"+customer.getCust_name()+"%"));
}
//1 调用Service查询分页数据(PageBean)
PageBean pb =customerservice.getPageBean(dCriteria,currentPage,pageSize);
//2 将PageBean放入request域,转发到列表页面显示
ActionContext.getContext().put("pageBean", pb);
return "list";
}
}
CustomerServiceImplement
package cn.lzm.service.Implement;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import cn.lzm.dao.CustomerDao;
import cn.lzm.domain.Customer;
import cn.lzm.service.CustomerService;
import cn.lzm.util.PageBean;
public class CustomerServiceImplement implements CustomerService {
private CustomerDao customerdao;
public void setCustomerdao(CustomerDao customerdao) {
this.customerdao = customerdao;
}
@Override
public void list() {
// TODO Auto-generated method stub
}
@Override
public PageBean getPageBean(DetachedCriteria detachedCriteria, Integer currentPage, Integer pageSize) {
// TODO Auto-generated method stub
// 1.调用dao查询总记录数
Integer totalCount=customerdao.geTotalCount(detachedCriteria);
// 2.创建PageBean对象
PageBean pBean=new PageBean(currentPage, totalCount, pageSize);
//3.调用dao查询分页列表数据
List<Customer> list=customerdao.getPageList(detachedCriteria,pBean.getStart(),pBean.getPageSize());
// 4.放入pageBean中,并返回
pBean.setList(list);
return pBean;
}
}
CustomerDaoImplement
package cn.lzm.dao.Implement;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import cn.lzm.dao.CustomerDao;
import cn.lzm.domain.Customer;
public class CustomerDaoImplement extends HibernateDaoSupport implements CustomerDao {
@Override
public Integer geTotalCount(DetachedCriteria detachedCriteria) {
// TODO Auto-generated method stub
detachedCriteria.setProjection(Projections.rowCount());
List<Long> list = (List<Long>) getHibernateTemplate().findByCriteria(detachedCriteria);
//清空聚合函数
detachedCriteria.setProjection(null);
if(list!=null&& list.size()>0) {
Long count = list.get(0);
return count.intValue();
}else {
return null;
}
}
@Override
public List<Customer> getPageList(DetachedCriteria detachedCriteria, int start, Integer pageSize) {
// TODO Auto-generated method stub
List<Customer> list = (List<Customer>) getHibernateTemplate().findByCriteria(detachedCriteria, start, pageSize);
return list;
}
}
hibernate Criteria setProjection
https://blog.csdn.net/u010485319/article/details/23202389