通用分页《一》

我们就来说说分页的好处,为什么要进行分页?
a、提高了用户的体验度
b、网络加载的速度的优化等等
分页好处有这么多,今天我们就来了解下通用分页的好处

首先我们写一个实体类 例如

Book

package com.wangshaoyang.entity;
public class Book {
	private int bid;
	private String bname;
	private float price;
	public int getBid() {
		return bid;
	}
	public void setBid(int bid) {
		this.bid = bid;
	}
	public String getBname() {
		return bname;
	}
	public void setBname(String bname) {
		this.bname = bname;
	}
	public float getPrice() {
		return price;
	}
	public void setPrice(float price) {
		this.price = price;
	}
	public Book(int bid, String bname, float price) {
		super();
		this.bid = bid;
		this.bname = bname;
		this.price = price;
	}
	public Book() {
		super();
	}
	@Override
	public String toString() {
		return "Book [bid=" + bid + ", bname=" + bname + ", price=" + price + "]";
	}
}

然后创建一个分页的类 PageBean“

package com.wangshaoyang.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 + "]";
	}
}

BaseDao

如果要做一个通用的分页,当然要写一个所有类都可以用的一个父类来进行操作,父类里包含着分页dao方法的许多共同的特点,如果不分页,那么要先获取符合查询条件的值的数量,然后根据pageBean查询出用户所需的数据。

package com.wangshaoyang.dao;

import java.lang.reflect.Field;
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.wangshaoyang.util.DBAccess;
import com.wangshaoyang.util.PageBean;


public class BaseDao<T> {
	
	/**
	 * 
	 * @param sql  决定查询哪个表的数据
	 * @param clz  查询出来的数据封装到哪个实体类中
	 * @param p   决定是否分页
	 * @return
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public List<T> executeQuery(String sql,Class clz,PageBean p) throws SQLException, InstantiationException, IllegalAccessException{
		List<T> list = new ArrayList<>();
		Connection con = DBAccess.getConnection();
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			//判断是否进行分页
			if (p != null && p.isPagination()) {
				//分页
				//查出符合条件的数据总数
				String countsql=getCountSql(sql);
				pst = con.prepareStatement(countsql);
				rs = pst.executeQuery();
				if(rs.next()) {
					p.setTotal(rs.getLong(1)+"");
				}
				//分页查询数据
				String pageSql=getPageSql(sql,p);
				pst = con.prepareStatement(pageSql);
				rs = pst.executeQuery();
				
			} else {
				//不分页,查询所有并全部展示
				pst = con.prepareStatement(sql);
				rs = pst.executeQuery();
			}
			while (rs.next()) {
				//1.创建了一个对象
				T t = (T) clz.newInstance();
				//从ResultSet结果集中获取值放入对象属性,并获取属性对象
				Field[] fields = clz.getDeclaredFields();
				//给属性对象赋值
				for (Field f : fields) {
					f.setAccessible(true);
					f.set(t, rs.getObject(f.getName()));
				}
				//将已经有值的book对象放入list集合中
				list.add(t);
			} 
		} finally {
			//shift+alt+z  
			DBAccess.close(con, pst, rs);
		}
		return list;
	}
	/**
	 * 将原生sql拼接出符合条件的某一页的数据查询sql
	 * @param sql
	 * @param p
	 * @return
	 */
	private String getPageSql(String sql,PageBean p) {
		// TODO Auto-generated method stub
		return sql+" limit " + p.getStartIndex()+","+p.getRows();
	}
	/**
	 * 用原生sql拼接出查询符合条件的记录数
	 * @param sql
	 * @return
	 */
	private String getCountSql(String sql) {
		return "select count(1) from ("+sql+") t";
	}
}

BookDao

我们使用BookDao继承BaseDao,调用写分页方法,获得父类的返回值。

public List<Book> list(Book book, PageBean pageBean) throws SQLException, InstantiationException, IllegalAccessException, IllegalArgumentException {
		List<Book> list = new ArrayList<>();
		String sql = "select * from t_mvc_book where 1=1";
		if (StringUtils.isNotBlank(book.getBname())) {
			sql += " and bname like '%" + book.getBname() + "%'";
		}
		return super.executeQuery(sql, Book.class, pageBean);
	}

然后在进行测试

public static void main(String[] args) {
		BookDao bookDao=new BookDao();
		try {
			Book b=new Book();
			PageBean pb=new PageBean();
//			pb.setPagination(false);//不分頁
			pb.setPage(3);//第三頁
			b.setBname("不死不");
		List<Book> list = bookDao.list(b, pb);
		for(Book book:list) {
			System.out.println(book);
		}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

最后我们就可以的得到第三页的10条数据
通用分页《一》_分页

今天就更新到这里
喜欢的可以关注我
不定时更新