JAVAEE---分页

目录

1.分页

1.2分页的步骤

2.解析BaseDao

3.案列及效果


1.分页

1、通过分页,我们不用一次性将所有的数据查出来,只需先查出来一部分,可以减少数据库的IO数据量的传输,降低数据库读写压力,从而提高数据库响应速度

2、页面也不用一次性显示所有的数据,可以减少浏览器和服务器之间大量数据的IO传输,从而提高服务器的响应速度

3、我们可能值需要很多信息中少数的几条,那么传输其他多余的数据就是无形之中对于资源的浪费,分页可以减少资源的浪费

1.2分页的步骤

首先先建一个类,写上需要的属性,再写一个起始的下标

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 + "]";
	}
/**
	 * 带分类的模糊查询
	 * @param sql
	 * @param clz
	 * @param pageBean
	 * @return
	 * @throws Exception
	 */
	public List executeQuery(String sql,Class clz,PageBean pageBean) throws Exception{
		List list = new ArrayList();
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		
		if(pageBean != null && pageBean.isPagination()) {
			String countSQL = getCountSQL(sql);
			conn = DBAccess.getConnection();
			pst = conn.prepareStatement(countSQL);
			rs = pst.executeQuery();
			if(rs.next()) {
				pageBean.setTotal(rs.getObject("n").toString());
			}
			
			String pageSQL = getPageSQL(sql,pageBean);
			conn = DBAccess.getConnection();
			pst = conn.prepareStatement(pageSQL);
			rs = pst.executeQuery();
		}else {
			conn = DBAccess.getConnection();
			pst = conn.prepareStatement(sql);
			rs = pst.executeQuery();
		}
		
		while(rs.next()) {
			T t = (T) clz.newInstance();
//			拿到clz对应的所有属性对象
			Field[] fields = clz.getDeclaredFields();
			for (Field f : fields) {
				f.setAccessible(true);
//				System.out.println(f.getName());
				f.set(t, rs.getObject(f.getName()));
			}
			list.add(t);
		}
		return list;
	}

2.解析BaseDao

2.1先定义它的属性:

 2.2再进行判断

JAVAEE---分页_第1张图片

2.3遍历数据,最后再设置保存 

JAVAEE---分页_第2张图片

JAVAEE---分页_第3张图片

3.案列及效果

package com.xuyahui.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 org.junit.Test;

import com.xuyahui.entity.Book;
import com.xuyahui.util.BaseDao;
import com.xuyahui.util.DBAccess;
import com.xuyahui.util.PageBean;
import com.xuyahui.util.StringUtils;


/**
 * BookDao:  操作的是书籍表
 * @author 86155
 *
 */
public class BookDao extends BaseDao{

	public List list1(Book book,PageBean pageBean) throws Exception{
		List list = new ArrayList();
		Connection conn = DBAccess.getConnection();
		String sql = "select * from t_mvc_book";
		String bname = book.getBname();
		if(StringUtils.isNotBlank(bname)) {
			sql += " and bname like '%"+bname+"%'";
		}
		PreparedStatement pst = conn.prepareStatement(sql);
		ResultSet rs = pst.executeQuery();
		while(rs.next()) {
			Book b = new Book();
			b.setBid(rs.getInt("bid"));
			b.setBname(rs.getString("bname"));
			b.setPrice(rs.getFloat("price"));
			list.add(b);
		}
		return list;
	}
	
	public List list2(Book book,PageBean pageBean) throws Exception{
		String sql = "select * from t_mvc_book 1=1";
		String bname = book.getBname();
		if(StringUtils.isNotBlank(bname)) {
			sql += " and bname like '%"+bname+"%'";
		}
		return super.executeQuery(sql, Book.class, pageBean);
	}

	@Test
	public void test1() throws Exception { 
		System.out.println("test1...");
		BookDao bookDao = new BookDao();
		Book book = new Book();
		book.setBname("圣墟");
		PageBean pageBean = new PageBean();
		List list = bookDao.list1(book, pageBean);
		for (Book b : list) {
			System.out.println(b);
		}
	}

效果

JAVAEE---分页_第4张图片

 

你可能感兴趣的:(数据库)