zoukankan      html  css  js  c++  java
  • 关于分页前台页面+后台实现

    后台分页工具类

    package com.entity;
    
    /**
     * 分页工具类
     * 
     * @author H.ros
     *
     */
    public class PageUtils {
        // 当前页默认第1页(从页面获取的当前页码,未计算)
        private int currentPage = 1;
        // 前一页
        private int prevPage;
        // 下一页
        private int nextPage;
        // 尾页
        private int lastPage;
        // 总记录数
        private int count;
        // 每页的条数,默认10条
        private int pageSize = 1;
        // 开始记录数,分页计入数(使用时调用的初始页,计算后)
        private int pageRecord;
        // 页面分页html模型代码
        private String page;
    
        // 有参构造器
        public PageUtils(String currentPage, int count, String pageSize) {
            init(currentPage, count, pageSize);
            initLastPage();
            initCurrentPage();
            initPrevPage();
            initNextPage();
            initPageRecord();
            initPage();
        }
    
        // 初始化三个重要元素
        private void init(String currentPage, int count, String pageSize) {
            if (currentPage != null && currentPage != "" && currentPage != "0") {
                this.currentPage = Integer.parseInt(currentPage);
            }
    
            this.count = count;
    
            if (pageSize != null && pageSize != "" && pageSize != "0") {
                this.pageSize = Integer.parseInt(pageSize);
                ;
            }
        }
    
        // 初始化尾页
        private void initLastPage() {
            if (count % pageSize == 0) {
                lastPage = count / pageSize;
            } else {
                lastPage = count / pageSize + 1;
            }
        }
    
        // 初始化并矫正当前页(防止外部访问出错)
        private void initCurrentPage() {
            if (currentPage < 1) {
                currentPage = 1;
            } else if (currentPage > lastPage) {
                currentPage = lastPage;
            }
        }
    
        // 初始化上一页
        private void initPrevPage() {
            if (currentPage != 1) {
                prevPage = currentPage - 1;
            } else {
                prevPage = 1;
            }
        }
    
        // 初始化下一页
        private void initNextPage() {
            if (currentPage != lastPage) {
                nextPage = currentPage + 1;
            } else {
                nextPage = lastPage;
            }
        }
    
        // 初始化分页计入数
        private void initPageRecord() {
            pageRecord = (currentPage - 1) * pageSize;
            if (pageRecord < 0) {
                pageRecord = 0;
            }
        }
    
        // 初始化html页面分页模型代码
        private void initPage() {
            page = "<div style='text-align: right;'>";
            page += "第" + currentPage + "/" + lastPage + "页,共" + count + "条记录。";
            page += "<select style=' 70px;' id='pageSize' onchange='page(1)'><option>1</option><option>2</option><option>5</option></select>";
            page += "<input type='button' value='首页' onclick='page(1)' />";
            page += "<input type='button' value='上一页' onclick='page(" + prevPage + ")' />";
            page += "<input type='button' value='下一页' onclick='page(" + nextPage + ")' />";
            page += "<input type='button' value='尾页' onclick='page(" + lastPage + ")' />";
            page += "</div>";
        }
    
        /*
         * 对外访问通道
         */
        public int getCurrentPage() {
            return currentPage;
        }
    
        public int getPrevPage() {
            return prevPage;
        }
    
        public int getNextPage() {
            return nextPage;
        }
    
        public int getLastPage() {
            return lastPage;
        }
    
        public int getCount() {
            return count;
        }
    
        public int getPageSize() {
            return pageSize;
        }
    
        public int getPageRecord() {
            return pageRecord;
        }
    
        public String getPage() {
            return page;
        }
    
    }

    后台代码

    查询语句中
        int count = bannerService.selectCount();
            PageUtils pageUtils = new PageUtils(currentPage, count, pageSize);
            // 开始记录数,由工具类中计算得出
            int begin = pageUtils.getPageRecord();
            // 查几条记录
            int number = pageUtils.getPageSize();
            // 前台页面分页模型(传入页面使用的html代码)
            String page = pageUtils.getPage();
            List<Banner2> list = bannerService.selectAll(begin, number);
    
            map.put("banner", list);
            map.put("page", page);
            map.put("number", number);

    dao。xml层

     <select id="selectAll" resultMap="BaseResultMap" >
     
        select * from banner limit ${begin},${number}
      </select>
      
      <select id="selectCount" resultType="int">
      select count(*) count from banner
      </select>

    前台页面撰写

    $(function(){
        $("#pageSize").val("${number}");
    })
    function page(i){
        //获取到下拉框选中的值
        var pageSize = $("#pageSize").find("option:selected").text()
        window.location.href="/ShoppingBackstage/banner/indexs?currentPage="+i+"&pageSize="+pageSize+" "
    }
    
    ${page}

     实现层

    public interface bannerService {
        public List<Banner2> selectAll(int begin, int number);
    
        public int selectCount();
    
        public void insert(Banner2 banner);
    
        public void delete(int id);
    
        public void update(Banner2 banner2);
    
        public Banner2 selectByid(int id);
    
    }
  • 相关阅读:
    CSS基础学习(二)
    CSS基础学习(一)
    《人月神话》读后感(第五六章)
    Python基础学习
    团队项目介绍
    全国疫情数据可视化展示(详细介绍)
    《人月神话》读后感(第三四章)
    拓展欧几里德
    Siano
    操作系统内存大页(THP)对redis性能的影响
  • 原文地址:https://www.cnblogs.com/yangfanfan/p/11574249.html
Copyright © 2011-2022 走看看