通用分页-01

一、分页的作用

1、分页可以提高客户体验度,适当地选择合适的数据条数,让页面显得更有条理,使得用户体验感良好,避免过多数据的冗余。(冗余:多余的、重复的,或者啰嗦内容)

2、提高性能的需要。分页技术,有选择的加载某部分数据,在数据量较大的时候,分部分加载数据、显示数据,可以有效提高程序的性能,当然,单纯的js的分页技术并没有这种效果。

二、分页的三要素

page 页码 视图层传递过来
rows 页大小 视图层传递过来
total 总记录数 后台查出来

注:pagination 是否分页 视图层传递过来

三、分页的案例

1、我们先建立实体类BookPageBean
Book

	package com.zking.entity;

	public class Book {
	private int bid;
	private String bname;
	private float price;

	@Override
	public String toString() {
		return "Book [bid=" + bid + ", bname=" + bname + ", price=" + 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) {
		this.bid = bid;
		this.bname = bname;
		this.price = price;
	}

	public Book() {}
	}

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

	}

2、接下来建立一个通用的方法
BaseDao (注:DBAccess是跟Mysql数据库获得连接的一个类)

	package com.zking.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.zking.entity.Book;
	import com.zking.util.DBAccess;
	import com.zking.util.PageBean;
	import com.zking.util.StringUtils;
	
	/**
	 * T = Book.class
	 * T = Student.class
	 * @author Administrator
	 *
	 * @param <T>
	 */
	public class BaseDao<T> {
		
		/**
		 * 
		 * @param sql 决定查询那张表的数据
		 * @param clz 查询出来的数据封装到那个实体类中
		 * @param pb   决定是否分页
		 * @return
		 * @throws SQLException 
		 * @throws IllegalAccessException 
		 * @throws InstantiationException 
		 */
		public List<T> executeQuery(String sql,Class clz,PageBean pb) throws SQLException, InstantiationException, IllegalAccessException{
			List<T> ls = new ArrayList<>();
			Connection con = DBAccess.getConnection();
			PreparedStatement ps = null;
			ResultSet rs = null;
			try {
				if(pb!=null&&pb.isPagination()) {
					//该分页了
					String countSql = getCountSql(sql);
					ps = con.prepareStatement(countSql);
					rs = ps.executeQuery();
					if(rs.next()) {
						pb.setTotal(rs.getLong(1)+"");
					}
					String pageSql = getPageSql(sql,pb);
					ps = con.prepareStatement(pageSql);
					rs = ps.executeQuery();
				}
				else {
					ps = con.prepareStatement(sql);
					rs = ps.executeQuery();
				}
		
				while(rs.next()) {
		//			ls.add(new Book(rs.getInt("bid"),
		//					rs.getString("bname"),
		//					rs.getFloat("price")));
					/*
					 * 1、创建了一个Book对象
					 * 2、从ResultSet结果集中获取值放入Book对象属性中
					 * 	2.1  获取到Book的属性对象
					 * 	2.2  给属性对象赋值
					 * 3、将已经有值的book对象放入list中
					 */
					T t = (T) clz.newInstance();
					Field[] fields = clz.getDeclaredFields();
					for (Field field : fields) {
						field.setAccessible(true);
						field.set(t, rs.getObject(field.getName()));
					}
					
					ls.add(t);
				} 
			} finally {
				DBAccess.close(con, ps, rs);
			}
			
			
			return ls;
		}
		
		/**
		 * 用原生sql拼接查询符合条件的某一页的数据查询sql
		 * @param sql
		 * @param pb
		 * @return
		 */
		private String getPageSql(String sql,PageBean pb) {
			return sql+" limit "+pb.getStartIndex()+","+pb.getRows();
		}
	
		/**
		 * 用原生sql拼接查询符合条件的记录
		 * @param sql
		 * @return
		 */
		private String getCountSql(String sql) {
			return "select count(1) from ("+sql+") t";
		}
	}

3、接下来我们建立一个BookDao来调用BaseDao
BookDao (注:StringUtils是一个类,isNotBlank(String sql)是个方法,如果字符串不等于null或去空格后不等于"",则返回true,否则返回false)

	package com.zking.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.entity.Book;
	import com.zking.util.DBAccess;
	import com.zking.util.PageBean;
	import com.zking.util.StringUtils;
	
	/**
	 * 操作数据库中t_mvc_book表
	 * 1、获得连接
	 * 2、加载驱动
	 * 3、获取预定义处理对象
	 * 4、执行sql语句
	 * 5、处理结果集
	 * 6、关闭连接
	 * 
	 * @author Administrator
	 *
	 */
	public class BookDao extends BaseDao<Book>{
		
		
		/**
		 * 
		 * @param b  是从jsp传递过来的参数封装成对象的作为参数查询并执行sql
		 * @param pb 决定是否分页
		 * @return
		 * @throws SQLException 
		 * @throws IllegalAccessException 
		 * @throws InstantiationException 
		 */
		public List<Book> list(Book b,PageBean pb) throws SQLException, InstantiationException, IllegalAccessException{
			List<Book> ls = new ArrayList<>();
			String sql = "select * from t_mvc_book where 1 = 1";
			if(StringUtils.isNotBlank(b.getBname())) {
				sql += " and bname like '%"+b.getBname()+"%'";
			}
			return super.executeQuery(sql, Book.class, pb);
		}
	}

4、最后我们就可以测试结果了

public static void main(String[] args) {
		BookDao bd = new BookDao();
		Book b = new Book();
		PageBean pb = new PageBean();
		try {
			b.setBname("圣墟");
			List<Book> ls = bd.list(b,pb);
			for (Book book : ls) {
				System.out.println(book);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}

输出
在这里插入图片描述
上面显示的数据是名称为“圣墟”的第一页的10条数据。

b.setBname(); 括号内放想查询的名称
pb.setPage() 括号内放页数;
pb.setPagination(); 括号内放true(可以分页)或false(不分页)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值