一、概述

今天自己做了个项目练练,然后有一些分页的功能,自己把分页的功能做了一个简单的总结,然后,为了以后能够方便自己的开发,做了一个baseDao的实现。

二、代码实现

2.1、分页的实体类pageBean

package org.sihai.utils;

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;

        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 = this.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;
    }

}

2.2、Action的实现

package org.sihai.web.action;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import org.sihai.domain.user;
import org.sihai.domain.User;
import org.sihai.service.userService;
import org.sihai.service.UserService;
import org.sihai.utils.PageBean;

public class userAction extends ActionSupport implements ModelDriven {
    private user user = new user();

    private userService cs;

    private Integer currentPage;
    private Integer pageSize;
    public String list() throws Exception {
        //封装离线查询对象
        DetachedCriteria dc = DetachedCriteria.forClass(user.class);
        //判断并封装参数
        if(StringUtils.isNotBlank(user.getuser_name())){
            dc.add(Restrictions.like("user_name", "%"+user.getuser_name()+"%"));
        }

        //1 调用Service查询分页数据(PageBean)
        PageBean pb = cs.getPageBean(dc,currentPage,pageSize);
        //2 将PageBean放入request域,转发到列表页面显示
        ActionContext.getContext().put("pageBean", pb);
        return "list";
    }

    @Override
    public user getModel() {
        return user;
    }

    public void setCs(userService cs) {
        this.cs = cs;
    }

    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;
    }

}

2.3、service的实现

package org.sihai.service.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;

import org.sihai.dao.userDao;
import org.sihai.domain.user;
import org.sihai.service.userService;
import org.sihai.utils.PageBean;

public class userServiceImpl implements userService {
    private userDao cd;
    @Override
    public PageBean getPageBean(DetachedCriteria dc, Integer currentPage, Integer pageSize) {
        //1 调用Dao查询总记录数
        Integer totalCount = cd.getTotalCount(dc);
        //2 创建PageBean对象
        PageBean pb = new PageBean(currentPage, totalCount, pageSize);
        //3 调用Dao查询分页列表数据

        List list = cd.getPageList(dc,pb.getStart(),pb.getPageSize());
        //4 列表数据放入pageBean中.并返回
        pb.setList(list);
        return pb;
    }
    public void setCd(userDao cd) {
        this.cd = cd;
    }

}

2.4、baseDao的具体实现(通用基类)

package org.sihai.dao.impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import org.sihai.dao.BaseDao;
import org.sihai.domain.user;

public class BaseDaoImpl extends HibernateDaoSupport implements BaseDao {

    private Class clazz;//用于接收运行期泛型类型

    public BaseDaoImpl() {
        //获得当前类型的带有泛型类型的父类
        ParameterizedType ptClass = (ParameterizedType) this.getClass().getGenericSuperclass();
        //获得运行期的泛型类型
        clazz = (Class) ptClass.getActualTypeArguments()[0];
    }

    @Override
    public void save(T t) {
        getHibernateTemplate().save(t);
    }

    @Override
    public void delete(T t) {

        getHibernateTemplate().delete(t);

    }

    @Override
    public void delete(Serializable id) {
        T t = this.getById(id);//先取,再删
        getHibernateTemplate().delete(t);
    }

    @Override
    public void update(T t) {
        getHibernateTemplate().update(t);
    }

    @Override
    public T getById(Serializable id) {

        return (T) getHibernateTemplate().get(clazz, id);
    }

    @Override
    public Integer getTotalCount(DetachedCriteria dc) {
        //设置查询的聚合函数,总记录数
        dc.setProjection(Projections.rowCount());

        List list = (List) getHibernateTemplate().findByCriteria(dc);

        //清空之前设置的聚合函数
        dc.setProjection(null);

        if(list!=null && list.size()>0){
            Long count = list.get(0);
            return count.intValue();
        }else{
            return null;
        }

    }

    @Override
    public List getPageList(DetachedCriteria dc, Integer start, Integer pageSize) {

        List list = (List) getHibernateTemplate().findByCriteria(dc, start, pageSize);

        return list;
    }

}

2.5、baseService的具体实现

package org.sihai.qualitycontrol.util.base;

import java.io.Serializable;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;
@Transactional
public interface BaseEbi {

    public void save(T t);

    public void update(T t);

    public void delete(T t);

    public List getAll();

    public T get(Serializable uuid);

    public List getAll(BaseQueryModel qm, Integer pageNum,Integer pageCount);

    public Integer getCount(BaseQueryModel qm);

}

2.6、baseAction的具体实现

package org.sihai.qualitycontrol.util.base;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import org.sihai.qualitycontrol.auth.emp.vo.EmpModel;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction  extends ActionSupport{
    public static final String LIST = "list";
    public static final String TO_LIST = "toList";
    public static final String INPUT = "input";

    public Integer pageNum = 1;
    public Integer pageCount = 10;
    public Integer maxPageNum;
    public Integer dataTotal;

    public String getActionName(){
        //动态
        //DepAction ->dep
        //EmpAction ->emp
        String actionName = this.getClass().getSimpleName();
        //DepAction ->Dep
        String temp = actionName.substring(0, actionName.length()-6);
        //Dep ->dep     OrderDetailAction   ->orderDetail  orderdetail
        return temp.substring(0,1).toLowerCase()+temp.substring(1);
    }

    protected void setDataTotal(int dataTotal){
        this.dataTotal = dataTotal ;
        maxPageNum = (dataTotal + pageCount -1) / pageCount;
    }

    protected void put(String name,Object obj){
        ActionContext.getContext().put(name,obj);
    }

    protected Object get(String name){
        return ActionContext.getContext().get(name);
    }

    protected void putSession(String name,Object obj){
        ActionContext.getContext().getSession().put(name,obj);
    }

    protected Object getSession(String name){
        return ActionContext.getContext().getSession().get(name);
    }

    protected EmpModel getLogin(){
        return (EmpModel) getSession(EmpModel.EMP_LOGIN_USER_OBJECT_NAME);
    }

    protected HttpServletRequest getRequest(){
        return ServletActionContext.getRequest();
    }

    protected HttpServletResponse getResponse(){
        return ServletActionContext.getResponse();
    }

}

2.7、页面的实现

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib  prefix="s" uri="/struts-tags" %>



客户列表 










        
当前位置:客户管理 > 客户列表
" /> " />
客户名称:
客户名称 客户级别 客户来源 联系人 电话 手机 操作
修改    删除
共[ ]条记录,[]页 ,每页显示 <%-- changePageSize($('#pageSizeSelect option').filter(':selected').val()) --%> 条 [前一页] [后一页] 到 " /> 页

三、通用分页实现

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
  共${dataTotal}条记录 首  页 上一页 末  页 当前第${pageNum}/${maxPageNum }页

四、总结

以前这样的总结还是比较少的,以至于在以后的开发,很多的模块的小的东西还需要重新的来思考,很是浪费时间,希望以后能够坚持。。。

欢迎关注我的微信公众号 好好学java,在公众号里,回复:java基础、html5、javaEE基础、struts2、spring、redis、luncene、oracle等,将可获得以上的优质视频教程及源码。