一、通用分页查询概述
当需要对多种类型的数据进行分页功能时,如果每种类型都要写相对应的分页功能的话,那代码量太大了,其实这些分页功能存在大量重复性的代码,那我们可以将其中重复的代码提炼起来做个通用的封装分页方法,每次需要写分页查询功能时,不管是要对张三的内裤进行分页、还是要对李四的丝袜进行分页,只要继承通用分页查询方法即可减少大量代码。
二、通用分页查询讲解
准备:连接mysql、一个书籍实体类和非空判断方法类以及分页工具类
2.1 未使用通用查询示例
非空判断方法类:
package com.xqx.util; /** * 封装非空判断方法 * * @author W许潜行 * */ 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); } }
Dao方法类:
/**原始查询方法 * @param book 书籍实体类 * @return * @throws Exception */ public List<Book> queryBook(Book book) throws Exception { List<Book> list = new ArrayList<Book>(); // 建立连接 Connection conn = DBHepler.getConnection(); String sql = "select *from t_mvc_book"; String bname = book.getBname(); if (!StringUtils.isBlank(bname)) {//如果非空 sql += " where bname like '%" + bname + "%' "; } // 执行sql语句 PreparedStatement ps = conn.prepareStatement(sql); // 结果集 ResultSet rs = ps.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 Exception { System.out.println("==================原始查询======================"); Book book = new Book(); //查询名称 book.setBname("圣墟"); List<Book> listBook = new BookDao().queryBook(book,); for (Book bookList : listBook) { System.out.println(bookList); } }
打印结果:
2.2 通用查询实例
首先封装通用查询方法
/** * @param sql 语句 * @param classes 类 * @param pageBean * @return * @throws Exception */ public List<T> query(String sql,Class<T> classes ) throws Exception { List<T> list = new ArrayList<T>(); // 建立连接 Connection conn = DBHepler.getConnection(); // 执行sql语句 PreparedStatement ps = conn.prepareStatement(sql); // 结果集 ResultSet rs = ps.executeQuery(); while (rs.next()) { //类实例 T t = classes.newInstance(); //拿到所有属性 Field[] fields = classes.getDeclaredFields(); for (Field fieldName : fields) { //打开访问权限 fieldName.setAccessible(true); //为t赋值 fieldName.set(t, rs.getObject(fieldName.getName())); } //将t加到集合 list.add(t); } return list; }
实现思路:
- 获取数据库连接。
- 创建PreparedStatement对象,执行SQL语句,获得结果集。
- 循环结果集,创建查询结果对象t,并将结果集中的数据赋值给t的属性。
- 将数据对象t添加到查询结果的List中。
- 返回List对象。
然后在dao方法内继承调用通用方法
/**使用通用模糊查询方法 * @param book * @return * @throws Exception */ public List<Book> queryBook2(Book book) throws Exception { String sql = "select *from t_mvc_book"; String bname = book.getBname(); if (!StringUtils.isBlank(bname)) { sql += " where bname like '%" + bname + "%' "; } return super.query(sql, Book.class); }
测试
public static void main(String[] args) throws Exception { System.out.println("==================通用查询======================"); Book book = new Book(); //查询名称 book.setBname("圣墟"); List<Book> listBook = new BookDao().queryBook2(book); for (Book bookList : listBook) { System.out.println(bookList); } }
打印结果:
可以看到继承通用查询方法后,dao方法类里面的查询方法减少7行了,如果我需要写100个查询方法,那么就减少了700行代码,这是不是很直观地感受到了通用查询的高效了呢。
2.3 通用分页查询实例
准备好的分页工具
package com.xqx.util; /** * 分页工具类 * @author W许潜行 * */ 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 + "]"; } }
封装通用分页查询方法
public List<T> query2(String sql,Class<T> classes ,PageBean pageBean) throws Exception { List<T> list = new ArrayList<T>(); // 建立连接 Connection conn = DBHepler.getConnection(); // 执行sql语句 PreparedStatement ps =null; // 结果集 ResultSet rs = null; if (pageBean!=null&&pageBean.isPagination()) {//如果pageBean不等于空并且分页 String countSql=getCountSql(sql); ps = conn.prepareStatement(countSql); rs = ps.executeQuery(); if (rs.next()) { //得到总数 pageBean.setTotal(rs.getObject("n").toString()); } String pageSql=getPageSql(sql,pageBean); ps = conn.prepareStatement(pageSql); rs = ps.executeQuery(); }else {//不分页 conn = DBHepler.getConnection(); ps = conn.prepareStatement(sql); } while (rs.next()) { //类实例 T t = classes.newInstance(); //拿到所有属性 Field[] fields = classes.getDeclaredFields(); for (Field fieldName : fields) { //打开访问权限 fieldName.setAccessible(true); //为t赋值 fieldName.set(t, rs.getObject(fieldName.getName())); } list.add(t); } return list; } /**得到符合条件的sql * @param sql * @param pageBean * @return */ private String getPageSql(String sql, PageBean pageBean) { return sql + " limit "+pageBean.getStartIndex()+","+pageBean.getRows(); } /**得到总数的sql * @param sql * @return */ private String getCountSql(String sql) { return "select count(1) as n FROM ("+sql+") t"; }
实现思路:
- 获取数据库连接。
- 判断是否需要分页查询,如果需要,则根据传入的SQL语句生成查询该结果的总条数的SQL语句,并执行该SQL语句,得到总条数并保存到pageBean对象中。再根据传入的SQL和分页信息生成分页查询的SQL语句,并执行该SQL语句。
- 如果不需要分页查询,则直接执行传入的SQL语句。
- 遍历查询结果集,将每条记录转化为一个JavaBean对象,并将其添加到查询结果的List中。
- 返回查询结果的List。
然后在dao方法内继承调用通用方法
/**通用分页模糊查询 * @param book * @param pageBean * @return * @throws Exception */ public List<Book> queryBook3(Book book, PageBean pageBean) throws Exception { String sql = "select *from t_mvc_book"; String bname = book.getBname(); if (!StringUtils.isBlank(bname)) { sql += " where bname like '%" + bname + "%' "; } return super.query2(sql, Book.class, pageBean); }
测试
public static void main(String[] args) throws Exception { System.out.println("==================通用分页查询======================"); Book book = new Book(); //查询名称 book.setBname("圣墟"); PageBean pageBean = new PageBean(); //页码 pageBean.setPage(1); //条数 pageBean.setRows(10); List<Book> listBook = new BookDao().queryBook3(book, pageBean); for (Book bookList : listBook) { System.out.println(bookList); } }
打印结果:
跟数据库查询的数据一致
三、Junit
3.1概述
Junit是一个Java语言的单元测试框架,它的主要特点是简单、易学、广泛使用和免费开源。Junit可以自动化测试Java程序中的方法,并对测试结果进行断言(assertion),从而保证程序的正确性。
- 断言(assertion)是一种编程技术,用于在程序代码中对预期结果进行验证。
在java中测试一般在main方法内,而一个class只能写一个main方法,要是需要测试多个方法,只能都写在mian方法里,如果这其中一个方法出错了,那么还需要注释其他方法,单独测试出错的方法,比较麻烦,而Junit就可以很好地解决这个问题。
3.2用法
3.2.1配置环境
右击项目-——Properties——Java Build Path-——Libraries——Add Library——JUNIT——JUNIT4——Finish
3.2.2具体用法
测试方法需要满足以下规范:
- 方法的返回类型必须为void。
- 方法不能有参数。
- 方法必须使用 @Test 注解进行标注。
@Test public void test() throws Exception { System.out.println("==================原始查询======================"); Book book = new Book(); //查询名称 book.setBname("圣墟"); PageBean pageBean = new PageBean(); List<Book> listBook = new BookDao().queryBook2(book, pageBean); for (Book bookList : listBook) { System.out.println(bookList); } } @Test public void test1() throws Exception { System.out.println("==================通用查询======================"); Book book = new Book(); //查询名称 book.setBname("圣墟"); PageBean pageBean = new PageBean(); List<Book> listBook = new BookDao().queryBook2(book, pageBean); for (Book bookList : listBook) { System.out.println(bookList); } } @Test public void test2() throws Exception { System.out.println("==================通用分页查询======================"); Book book = new Book(); //查询名称 book.setBname("圣墟"); PageBean pageBean = new PageBean(); //页码 pageBean.setPage(1); //条数 pageBean.setRows(10); List<Book> listBook = new BookDao().queryBook3(book, pageBean); for (Book bookList : listBook) { System.out.println(bookList); } }
需要测试哪个方法就选中测试即可,测试test()方法
打印结果:
如果需要测试所有数据,右键Junit Test即可
打印结果:
==================原始查询====================== Book [bid=16, bname=圣墟第16章, price=16.0] Book [bid=22, bname=圣墟第22章, price=22.0] Book [bid=24, bname=圣墟第24章, price=24.0] Book [bid=28, bname=圣墟第28章, price=28.0] Book [bid=29, bname=圣墟第29章, price=29.0] Book [bid=30, bname=圣墟第30章, price=30.0] Book [bid=31, bname=圣墟第31章, price=31.0] Book [bid=32, bname=圣墟第32章, price=32.0] Book [bid=33, bname=圣墟第33章, price=33.0] Book [bid=34, bname=圣墟第34章, price=34.0] Book [bid=35, bname=圣墟第35章, price=35.0] Book [bid=36, bname=圣墟第36章, price=36.0] Book [bid=37, bname=圣墟第37章, price=37.0] Book [bid=38, bname=圣墟第38章, price=38.0] Book [bid=39, bname=圣墟第39章, price=39.0] Book [bid=40, bname=圣墟第40章, price=40.0] Book [bid=41, bname=圣墟第41章, price=41.0] Book [bid=42, bname=圣墟第42章, price=42.0] Book [bid=43, bname=圣墟第43章, price=43.0] Book [bid=44, bname=圣墟第44章, price=44.0] Book [bid=45, bname=圣墟第45章, price=45.0] Book [bid=46, bname=圣墟第46章, price=46.0] Book [bid=47, bname=圣墟第47章, price=47.0] Book [bid=49, bname=圣墟第49章, price=49.0] Book [bid=50, bname=圣墟第50章, price=50.0] Book [bid=51, bname=圣墟第51章, price=51.0] Book [bid=52, bname=圣墟第52章, price=52.0] Book [bid=53, bname=圣墟第53章, price=53.0] Book [bid=54, bname=圣墟第54章, price=54.0] Book [bid=55, bname=圣墟第55章, price=55.0] Book [bid=56, bname=圣墟第56章, price=56.0] Book [bid=57, bname=圣墟第57章, price=57.0] Book [bid=58, bname=圣墟第58章, price=58.0] Book [bid=59, bname=圣墟第59章, price=59.0] Book [bid=60, bname=圣墟第60章, price=60.0] Book [bid=61, bname=圣墟第61章, price=61.0] Book [bid=62, bname=圣墟第62章, price=62.0] Book [bid=63, bname=圣墟第63章, price=63.0] Book [bid=64, bname=圣墟第64章, price=64.0] Book [bid=65, bname=圣墟第65章, price=65.0] Book [bid=66, bname=圣墟第66章, price=66.0] Book [bid=67, bname=圣墟第67章, price=67.0] Book [bid=68, bname=圣墟第68章, price=68.0] Book [bid=69, bname=圣墟第69章, price=69.0] Book [bid=70, bname=圣墟第70章, price=70.0] Book [bid=71, bname=圣墟第71章, price=71.0] Book [bid=72, bname=圣墟第72章, price=72.0] Book [bid=73, bname=圣墟第73章, price=73.0] Book [bid=74, bname=圣墟第74章, price=74.0] Book [bid=75, bname=圣墟第75章, price=75.0] Book [bid=76, bname=圣墟第76章, price=76.0] Book [bid=77, bname=圣墟第77章, price=77.0] Book [bid=78, bname=圣墟第78章, price=78.0] Book [bid=79, bname=圣墟第79章, price=79.0] Book [bid=80, bname=圣墟第80章, price=80.0] Book [bid=81, bname=圣墟第81章, price=81.0] Book [bid=82, bname=圣墟第82章, price=82.0] Book [bid=83, bname=圣墟第83章, price=83.0] Book [bid=84, bname=圣墟第84章, price=84.0] Book [bid=85, bname=圣墟第85章, price=85.0] Book [bid=86, bname=圣墟第86章, price=86.0] Book [bid=87, bname=圣墟第87章, price=87.0] Book [bid=88, bname=圣墟第88章, price=88.0] Book [bid=89, bname=圣墟第89章, price=89.0] Book [bid=90, bname=圣墟第90章, price=90.0] Book [bid=91, bname=圣墟第91章, price=91.0] Book [bid=92, bname=圣墟第92章, price=92.0] Book [bid=93, bname=圣墟第93章, price=93.0] Book [bid=94, bname=圣墟第94章, price=94.0] Book [bid=95, bname=圣墟第95章, price=95.0] Book [bid=96, bname=圣墟第96章, price=96.0] Book [bid=97, bname=圣墟第97章, price=97.0] Book [bid=98, bname=圣墟第98章, price=98.0] Book [bid=99, bname=圣墟第99章, price=99.0] Book [bid=100, bname=圣墟第100章, price=100.0] Book [bid=101, bname=圣墟第101章, price=101.0] Book [bid=102, bname=圣墟第102章, price=102.0] Book [bid=103, bname=圣墟第103章, price=103.0] Book [bid=104, bname=圣墟第104章, price=104.0] Book [bid=105, bname=圣墟第105章, price=105.0] ==================通用查询====================== Book [bid=16, bname=圣墟第16章, price=16.0] Book [bid=22, bname=圣墟第22章, price=22.0] Book [bid=24, bname=圣墟第24章, price=24.0] Book [bid=28, bname=圣墟第28章, price=28.0] Book [bid=29, bname=圣墟第29章, price=29.0] Book [bid=30, bname=圣墟第30章, price=30.0] Book [bid=31, bname=圣墟第31章, price=31.0] Book [bid=32, bname=圣墟第32章, price=32.0] Book [bid=33, bname=圣墟第33章, price=33.0] Book [bid=34, bname=圣墟第34章, price=34.0] Book [bid=35, bname=圣墟第35章, price=35.0] Book [bid=36, bname=圣墟第36章, price=36.0] Book [bid=37, bname=圣墟第37章, price=37.0] Book [bid=38, bname=圣墟第38章, price=38.0] Book [bid=39, bname=圣墟第39章, price=39.0] Book [bid=40, bname=圣墟第40章, price=40.0] Book [bid=41, bname=圣墟第41章, price=41.0] Book [bid=42, bname=圣墟第42章, price=42.0] Book [bid=43, bname=圣墟第43章, price=43.0] Book [bid=44, bname=圣墟第44章, price=44.0] Book [bid=45, bname=圣墟第45章, price=45.0] Book [bid=46, bname=圣墟第46章, price=46.0] Book [bid=47, bname=圣墟第47章, price=47.0] Book [bid=49, bname=圣墟第49章, price=49.0] Book [bid=50, bname=圣墟第50章, price=50.0] Book [bid=51, bname=圣墟第51章, price=51.0] Book [bid=52, bname=圣墟第52章, price=52.0] Book [bid=53, bname=圣墟第53章, price=53.0] Book [bid=54, bname=圣墟第54章, price=54.0] Book [bid=55, bname=圣墟第55章, price=55.0] Book [bid=56, bname=圣墟第56章, price=56.0] Book [bid=57, bname=圣墟第57章, price=57.0] Book [bid=58, bname=圣墟第58章, price=58.0] Book [bid=59, bname=圣墟第59章, price=59.0] Book [bid=60, bname=圣墟第60章, price=60.0] Book [bid=61, bname=圣墟第61章, price=61.0] Book [bid=62, bname=圣墟第62章, price=62.0] Book [bid=63, bname=圣墟第63章, price=63.0] Book [bid=64, bname=圣墟第64章, price=64.0] Book [bid=65, bname=圣墟第65章, price=65.0] Book [bid=66, bname=圣墟第66章, price=66.0] Book [bid=67, bname=圣墟第67章, price=67.0] Book [bid=68, bname=圣墟第68章, price=68.0] Book [bid=69, bname=圣墟第69章, price=69.0] Book [bid=70, bname=圣墟第70章, price=70.0] Book [bid=71, bname=圣墟第71章, price=71.0] Book [bid=72, bname=圣墟第72章, price=72.0] Book [bid=73, bname=圣墟第73章, price=73.0] Book [bid=74, bname=圣墟第74章, price=74.0] Book [bid=75, bname=圣墟第75章, price=75.0] Book [bid=76, bname=圣墟第76章, price=76.0] Book [bid=77, bname=圣墟第77章, price=77.0] Book [bid=78, bname=圣墟第78章, price=78.0] Book [bid=79, bname=圣墟第79章, price=79.0] Book [bid=80, bname=圣墟第80章, price=80.0] Book [bid=81, bname=圣墟第81章, price=81.0] Book [bid=82, bname=圣墟第82章, price=82.0] Book [bid=83, bname=圣墟第83章, price=83.0] Book [bid=84, bname=圣墟第84章, price=84.0] Book [bid=85, bname=圣墟第85章, price=85.0] Book [bid=86, bname=圣墟第86章, price=86.0] Book [bid=87, bname=圣墟第87章, price=87.0] Book [bid=88, bname=圣墟第88章, price=88.0] Book [bid=89, bname=圣墟第89章, price=89.0] Book [bid=90, bname=圣墟第90章, price=90.0] Book [bid=91, bname=圣墟第91章, price=91.0] Book [bid=92, bname=圣墟第92章, price=92.0] Book [bid=93, bname=圣墟第93章, price=93.0] Book [bid=94, bname=圣墟第94章, price=94.0] Book [bid=95, bname=圣墟第95章, price=95.0] Book [bid=96, bname=圣墟第96章, price=96.0] Book [bid=97, bname=圣墟第97章, price=97.0] Book [bid=98, bname=圣墟第98章, price=98.0] Book [bid=99, bname=圣墟第99章, price=99.0] Book [bid=100, bname=圣墟第100章, price=100.0] Book [bid=101, bname=圣墟第101章, price=101.0] Book [bid=102, bname=圣墟第102章, price=102.0] Book [bid=103, bname=圣墟第103章, price=103.0] Book [bid=104, bname=圣墟第104章, price=104.0] Book [bid=105, bname=圣墟第105章, price=105.0] Book [bid=130, bname=圣墟130, price=17.8] Book [bid=131, bname=圣墟132章, price=17.99] Book [bid=110000, bname=圣墟和, price=66.0] Book [bid=110001, bname=圣墟好, price=88.0] Book [bid=110002, bname=圣墟好2, price=886.0] ==================通用分页查询====================== Book [bid=16, bname=圣墟第16章, price=16.0] Book [bid=22, bname=圣墟第22章, price=22.0] Book [bid=24, bname=圣墟第24章, price=24.0] Book [bid=28, bname=圣墟第28章, price=28.0] Book [bid=29, bname=圣墟第29章, price=29.0] Book [bid=30, bname=圣墟第30章, price=30.0] Book [bid=31, bname=圣墟第31章, price=31.0] Book [bid=32, bname=圣墟第32章, price=32.0] Book [bid=33, bname=圣墟第33章, price=33.0] Book [bid=34, bname=圣墟第34章, price=34.0]
好啦,今天的分享就到此为止!希望你看完本篇文章有所收获,祝你变得更强!!!