码迷,mamicode.com
首页 > 其他好文 > 详细

通用分页(一)

时间:2019-06-20 12:58:11      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:span   返回   获得   abstract   font   port   new   connect   nbsp   

通用分页核心思路:
将上一次查询请求再发一次,只是当前页变了而已。


首页 1 2 3 4 … 100 末页 跳转

MySQL分页:select * from 表 limit 3,3

 

每页显示记录数      自己设置
当前页          来自前端
总页数          总记录数%每页显示记录数==0?总记录数/每页显示记录数:总记录数/每页显示记录数+1
总记录数         数据库统计count()
每页起始记录数      =(当前页-1)*每页显示记录数+1

总共101条记录,每页显示10条
第一页: 1-10
第二页: 11-20
第三页: 21-30

每页结尾记录数      =当前页*每页显示记录数

 

1、通用的查询方法代码实现

导入jar包

连接数据库之前查看自己的用户名、密码、数据库名是否正确

 

Book.java

package com.huangyucan.util;


public class PageBean {

    private int page = 1;// 页码

    private int rows = 10;// 页大小

    private int total = 0;// 总记录数

    private boolean pagination = true;// 是否分页

    public PageBean() {
        super();
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public void setTotal(String total) {
        this.total = Integer.parseInt(total);
    }

    public boolean isPagination() {
        return pagination;
    }

    public void setPagination(boolean pagination) {
        this.pagination = pagination;
    }

    /**
     * 获得起始记录的下标
     * 
     * @return
     */
    public int getStartIndex() {
        return (this.page - 1) * this.rows;
    }

    @Override
    public String toString() {
        return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]";
    }

}

 

 

BookDao.java

package com.huangyucan.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zking.pageBean.entity.Book;
import com.zking.pageBean.util.BaseDao;
import com.zking.pageBean.util.BaseEntityDao;
import com.zking.pageBean.util.DBAccess;
import com.zking.pageBean.util.PageBean;
import com.zking.pageBean.util.StringUtils;

public class BookDao extends BaseEntityDao<Book>{
    
//    /**
//     * 
//     * @param book    带条件查询需要用上
//     * @param pageBean    分页
//     * @return
//     * @throws SQLException 
//     */
//    public List<Book> list(Book book,PageBean pageBean) throws SQLException{
//        List<Book> list = new ArrayList<>();
//        String sql = "select * from t_mvc_book where true";
//        if(StringUtils.isNotBlank(book.getBname())) {
//            sql += " and bname like ‘%"+book.getBname()+"%‘";
//        }
////        System.out.println(sql);
//        Connection con = DBAccess.getConnection();
//        PreparedStatement pst = con.prepareStatement(sql);
//        ResultSet rs = pst.executeQuery();
//        while(rs.next()) {
//            list.add(new Book(rs.getInt("bid"), rs.getString("bname"), rs.getFloat("price")));
//        }
//        return list;
//    }
//    /**
//     * oop改造后的方法
//     * @param book
//     * @param pageBean
//     * @return
//     * @throws SQLException
//     * @throws IllegalAccessException 
//     * @throws InstantiationException 
//     */
//    public List<Book> list(Book book,PageBean pageBean) throws SQLException, InstantiationException, IllegalAccessException{
//        String sql = "select * from t_mvc_book where true";
//        if(StringUtils.isNotBlank(book.getBname())) {
//            sql += " and bname like ‘%"+book.getBname()+"%‘";
//        }
//        return executeQuery(sql, pageBean, new CallBack() {
//            @Override
//            public List<Book> foreach(ResultSet rs) throws SQLException {
//                List<Book> list = new ArrayList<>();
//                while(rs.next()) {
//                    list.add(new Book(rs.getInt("bid"), rs.getString("bname"), rs.getFloat("price")));
//                }
//                return list;
//            }
//        });
//    }
    
    /**
     * 利用反射对查询进行二次增强
     * @param book
     * @param pageBean
     * @return
     * @throws SQLException
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public List<Book> list(Book book,PageBean pageBean) throws SQLException, InstantiationException, IllegalAccessException{
        String sql = "select * from t_mvc_book where true";
        if(StringUtils.isNotBlank(book.getBname())) {
            sql += " and bname like ‘%"+book.getBname()+"%‘";
        }
        return executeQuery(sql, pageBean, Book.class);
    }
    
}

BaseDao

 

package com.huangyucan.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zking.pageBean.entity.Book;

public class BaseDao<T> {
    /**
     * 借鉴ajax的回调函数现象
     * 在调用方去处理返回的结果集
     * @author Administrator
     *
     */
    public abstract class CallBack{
        public abstract List<T> foreach(ResultSet rs) throws SQLException, InstantiationException, IllegalAccessException;
    }
    /**
     * 利用oop思想对原有的查询方法向上抽取共性部分
     * @param sql
     * @param pageBean
     * @return
     * @throws SQLException
     * @throws IllegalAccessException 
     * @throws InstantiationException 
     */
    public List<T> executeQuery(String sql, PageBean pageBean, CallBack callBack) throws SQLException, InstantiationException, IllegalAccessException{
        List<T> list = new ArrayList<>();
        Connection con = DBAccess.getConnection();
        String countSql = getCountSql(sql);
        PreparedStatement pst = con.prepareStatement(countSql);
        ResultSet rs = pst.executeQuery();
        if(rs.next()) {
            pageBean.setTotal(rs.getString(1));
        }
        DBAccess.close(null, pst, rs);
        String pageSql = getPageSql(sql, pageBean);
        PreparedStatement psmt = con.prepareStatement(pageSql);
        ResultSet rst = psmt.executeQuery();
        return callBack.foreach(rst);
    }
    /**
     * 获取分页的sql语句
     * @param sql    符合条件的查询语句
     * @param pageBean    分页的实体类
     * @return
     */
    private String getPageSql(String sql,PageBean pageBean) {
        return sql + " limit "+ pageBean.getStartIndex() +"," + pageBean.getRows();
    }
    /**
     * 获取符合查询条件的总记录数的sql
     * @param sql
     * @return
     */
    private String getCountSql(String sql) {
        return "select count(*) from ("+ sql +") t";
    }
}

 

 

分页工具类

PageBase

package com.huangyucan.util;

/**
 * 分页工具类
 *
 */
public class PageBean {

    private int page = 1;// 页码

    private int rows = 10;// 页大小

    private int total = 0;// 总记录数

    private boolean pagination = true;// 是否分页

    public PageBean() {
        super();
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public void setTotal(String total) {
        this.total = Integer.parseInt(total);
    }

    public boolean isPagination() {
        return pagination;
    }

    public void setPagination(boolean pagination) {
        this.pagination = pagination;
    }

    /**
     * 获得起始记录的下标
     * 
     * @return
     */
    public int getStartIndex() {
        return (this.page - 1) * this.rows;
    }

    @Override
    public String toString() {
        return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]";
    }

}

 

通用分页(一)

标签:span   返回   获得   abstract   font   port   new   connect   nbsp   

原文地址:https://www.cnblogs.com/bf6rc9qu/p/11057860.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!