1. PageBean
package com.zyp.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
package com.zyp.util;
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.zyp.entity.Book;
/**
* T = Book.class
* T = Student.class
* @author zhouyanpeng
*
* @param <T>
*/
public class BaseDao<T> {
/**
*
* @param sql 决定查询哪张表的数据
* @param clz 查询出来的数据封装到哪个实体类中
* @param pagebean 决定是否分页
* @return
* @throws IllegalAccessException
* @throws InstantiationException
* @throws SQLException
*/
public List<T> executeQuery(String sql,Class clz,PageBean pagebean) throws InstantiationException, IllegalAccessException, SQLException{
List<T> list = new ArrayList<>();
Connection con = DBAccess.getConnection();
PreparedStatement pst = null;
ResultSet rs = null;
if(pagebean != null && pagebean.isPagination()) {
//该分页了
String countSql = getCountSql(sql);
pst = con.prepareStatement(countSql);
rs = pst.executeQuery();
if(rs.next()) {
pagebean.setTotal(rs.getLong(1)+"");
}
String pageSql = getpageSql(sql,pagebean);
pst = con.prepareStatement(pageSql);
rs = pst.executeQuery();
}else {
pst = con.prepareStatement(sql);
rs = pst.executeQuery();
}
try {
while (rs.next()) {
// list.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()));//第二步完成
}
list.add(t);
}
} finally {
//shift+alt+z
DBAccess.close(con, pst, rs);;
}
return list;
}
/**
* 将原生sql拼接出符合条件的某一页的数据查询sql
* @param sql
* @param pagebean
* @return
*/
private String getpageSql(String sql, PageBean pagebean) {
// TODO Auto-generated method stub
return sql+" limit" +pagebean.getStartIndex()+","+pagebean.getRows();
}
/**
* 用原生sql拼接出查询符合条件的记录数。
* @param sql
* @return
*/
private String getCountSql(String sql) {
// TODO Auto-generated method stub
return "select count(1) from ("+sql+") t";
}
}
3、案例演示【book】
实例化Book类
package com.zyp.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() {
super();
}
public Book(int bid, String bname, float price) {
super();
this.bid = bid;
this.bname = bname;
this.price = price;
}
}
BookDao
package com.zyp.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.zyp.entity.Book;
import com.zyp.util.BaseDao;
import com.zyp.util.DBAccess;
import com.zyp.util.PageBean;
import com.zyp.util.StringUtils;
/**
* 操作数据库中的t_mvc_book表
* 1、加载驱动
* 2、建立连接
* 3、获取预定义处理对象
* 4、执行sql语句
* 5、处理结果集
* 6、关闭连接
* @author zhouyanpeng
*
*/
public class BookDao extends BaseDao<Book>{
// /**
// *
// * @param book 是从jsp传递过来的参数封装成对象作为参数查询并执行的sql
// * @return pageBean 决定是否分页
// * @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 1 = 1";
// if(StringUtils.isNotBlank(book.getBname())) {
// sql += " and bname like '%"+book.getBname()+"%'";
// }
// 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;
// }
// public static void main(String[] args) throws SQLException {
// BookDao bookdao = new BookDao();
// Book b = new Book();
// List<Book> list = bookdao.list(b, null);
// for (Book book : list) {
// System.out.println(book);
// }
// }
/**
*
* @param book 是从jsp传递过来的参数封装成对象作为参数查询并执行的sql
* @return pageBean 决定是否分页
* @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 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 pageBean = new PageBean();
// pageBean.setPage(2);
pageBean.setPagination(false);
b.setBname("圣墟");
List<Book> list = bookdao.list(b, pageBean);
for (Book book : list) {
System.out.println(book);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
测试结果
public static void main(String[] args) {
BookDao bookdao = new BookDao();
try {
Book b = new Book();
PageBean pageBean = new PageBean();
pageBean.setPage(2);//第几页 2
// pageBean.setPagination(false);//不分页
b.setBname("圣墟");//模糊查询的参数
List<Book> list = bookdao.list(b, pageBean);
for (Book book : list) {//遍历输出
System.out.println(book);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
打印结果
分页三要素
1、 page: 页码 视图层传递过来
2、rows: 页大小 视图层传递过来
3、total : 总记录数 后台查出来
想要了解更多,联系博主。