通用分页1
通用分页
首先你得先把数据库连上(由于数据库不同,就不做演示了)
然后就是准备一个判断字符串得类
StringUtils类
public class StringUtils { // 私有的构造方法,保护此类不能在外部实例化 private StringUtils() { } /** * 如果字符串等于null或去空格后等于"",则返回true,否则返回false * * @param s * @return */ public static boolean isBlank(String s) { boolean b = false; if (null == s || s.trim().equals("")) { b = true; } return b; } /** * 如果字符串不等于null或去空格后不等于"",则返回true,否则返回false * * @param s * @return */ public static boolean isNotBlank(String s) { return !isBlank(s); } }
其次实体类是必不可少的
book
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) { super(); this.bid = bid; this.bname = bname; this.price = price; } public Book() { super(); // TODO Auto-generated constructor stub } }
通用分页还需要一个工具类pagebean
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使用泛型
basedao
public class BaseDao<T> { public List<T> executeQuery(String sql,Class clz,PageBean pageBean) throws SQLException, InstantiationException, IllegalAccessException{ 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(); } List<T> list=new ArrayList<>(); T t; while(rs.next()) { /* * 1,实例化一个book对象(该对象是空得) * 2,取book得所有属性,然后给其赋值 * 2.1获取所有属性对象 * 2.2给属性对象赋值 * 3,赋完值得book对象装进list容器中 */ // list.add(new Book(rs.getInt("bid"),rs.getString("bname"),rs.getFloat("price"))); 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); } 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()+",5"; } /** * 获取符合条件的总记录 * @param sql * @return */ private String getcountsql(String sql) { // TODO Auto-generated method stub return "SELECT COUNT(*)FROM("+sql+")t;"; } }
接下来就是我们的dao方法了,写完dao方法就可以直接运行看看效果了
public class BookDao extends BaseDao<Book> { public List<Book> list(Book book, PageBean pageBean) throws SQLException, InstantiationException, IllegalAccessException { String sql = "select * from t_mvc_book where true"; String bname = book.getBname(); if (StringUtils.isNotBlank(bname)) { sql += " and bname like '%" + bname + "%'"; } return super.executeQuery(sql, Book.class, pageBean); } public static void main(String[] args) throws InstantiationException, IllegalAccessException, SQLException { BookDao bookDao = new BookDao(); Book book = new Book(); PageBean pageBean = new PageBean(); book.setBname("圣墟"); pageBean.setPage(2);//这是是设置看第几页的 List<Book> list = bookDao.list(book, pageBean); for (Book b : list) { System.out.println(b); } } }
结果(这是我设置pageBean.setPage(2);)只要要改括号,改变查询的页数
下面还做了一个小测试,看查其他的表需要写多少代码可以实现这个分页
建一个实体类blog类
public class Blog { private int id; private String bookname; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getBookname() { return bookname; } public void setBookname(String bookname) { this.bookname = bookname; } @Override public String toString() { return "Blog [id=" + id + ", bookname=" + bookname + "]"; } }
接下来直接写dao方法继承下basedao就可以了
public class BlogDao extends BaseDao<Blog> { public List<Blog> list(Blog blog, PageBean pageBean)throws SQLException, InstantiationException, IllegalAccessException { String sql = "select * from t_book where true"; String bookname = blog.getBookname(); if (StringUtils.isNotBlank(bookname)) { sql += " and bookname LIKE '%" + bookname + "%'"; } System.out.println(sql); return super.executeQuery(sql, Blog.class, pageBean); } public static void main(String[] args) throws InstantiationException, IllegalAccessException, SQLException { BlogDao blogDao = new BlogDao(); PageBean pageBean = new PageBean(); Blog blog = new Blog(); blog.setBookname("哈林"); pageBean.setPage(3); List<Blog> list = blogDao.list(blog, pageBean); for (Blog b : list) { System.out.println(b); } } }
让我们来看下结果,这就完成了
这就实现了分页效果,就是这么简单了,直接一套用就可以了,简化代码真的实用。