目录
上次我们分享了自定义标签——jsp标签增强,今天分享的内容是通用分页。
- package com.zhw.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 + "]";
- }
- }
原生:
- public List<Book> list(Book book,PageBean pageBean) throws Exception{
- List<Book> list = new ArrayList<Book>();
- Connection con = DBAccess.getConnection();
- String sql = "select * from t_mvc_book where 1=1 ";
- String bname = book.getBname();
- if(StringUtils.isNotBlank(bname)) {
- sql += "and bname like '%"+bname+"%' ";
- }
- 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 List<T> list(String sql,Class<T> clz) throws Exception{ List<T> list = new ArrayList<T>(); Connection con = DBAccess.getConnection(); String sql = "select * from t_mvc_book where 1=1 "; String bname = book.getBname(); if(StringUtils.isNotBlank(bname)) { sql += "and bname like '%"+bname+"%' "; } 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"))); T t = clz.newInstance(); Field[] fields = clz.getDeclaredFields(); for (Field f : fields) { f.setAccessible(true); f.set(t, rs.getObject(f.getName())); } list.add(t); } return list; }
- public List<T> executeQuery(String sql,PageBean pageBean,Class<T> clz) throws Exception{
- List<T> list = new ArrayList<T>();
- Connection con = DBAccess.getConnection();
- PreparedStatement pst = null;
- ResultSet rs = null;
-
- String countSQL = getCountSQL(sql);
- String pageSQL = getPageSQL(sql,pageBean);
-
- if(pageBean != null && pageBean.isPagination()) {
- pst = con.prepareStatement(countSQL);
- rs = pst.executeQuery();
-
- if(rs.next()) {
- pageBean.setTotal(String.valueOf(rs.getObject(1)));
- }
-
- pst = con.prepareStatement(pageSQL);
- rs = pst.executeQuery();
- }else {
- pst = con.prepareStatement(sql);
- rs = pst.executeQuery();
- }
-
-
- while(rs.next()) {
- // list.add(new Book(rs.getInt("bid"), rs.getString("bname"), rs.getFloat("price")));
- T t = clz.newInstance();
- Field[] fields = clz.getDeclaredFields();
- for (Field f : fields) {
- f.setAccessible(true);
- f.set(t, rs.getObject(f.getName()));
- }
- list.add(t);
- }
- return list;
- }
-
- private String getPageSQL(String sql, PageBean pageBean) {
- return sql + " limit "+pageBean.getStartIndex()+","+pageBean.getRows();
- }
-
- private String getCountSQL(String sql) {
- return "select count(1) FROM ("+sql+") t";
- }
我们测试的时候经常会在main方法中打印。测试的方法越来越多,main方法也越来越杂。
这时候就可以用到Junit。
在测试类右键,Ctrl+N, 搜索junit,选择第一个。
选择junit 4,并勾选setup和teardown

next,选中 当前类的测试方法

finish。测试类就完成了。

- package com.zhw.dao;
-
- import java.util.List;
-
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
-
- import com.zhw.entity.Book;
-
- public class BookDaoTest {
- @Before
- public void setUp() throws Exception {
- System.out.println("被测试方法执行前调用");
- }
-
- @After
- public void tearDown() throws Exception {
- System.out.println("被测试方法执行后调用");
- }
-
- @Test
- public void testList() {
- List<Book> list;
- try {
- list = new BookDao().list(new Book(), null);
- for (Book book : list) {
- System.out.println(book);
- }
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
-
- }
-
-
-
-
-
-
-
运行结果:

如果我们要测试其他方法
新写一个:


- package com.zhw.dao;
-
- import java.util.List;
-
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
-
- import com.zhw.entity.Book;
-
- public class BookDaoTest {
- @Before
- public void setUp() throws Exception {
- System.out.println("被测试方法执行前调用");
- }
-
- @After
- public void tearDown() throws Exception {
- System.out.println("被测试方法执行后调用");
- }
-
- @Test
- public void testList() {
- List<Book> list;
- try {
- list = new BookDao().list(new Book(), null);
- for (Book book : list) {
- System.out.println(book);
- }
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- @Test
- public void testList2() {
- System.out.println("测试代码2");
- }
- }
-
-
-
-
-
-
-
结果如下:

这次分享的内容是通用标签分页,便于使用,如果有多个类型需要分页则不需要写重复代码,只需调用写好的通用分页代码,缩短了开发时间,减少代码量。
希望对你能有帮助,下期预告通用分页(2)。
我是九歌,一个喜欢编程的程序员。
如有错误还望指正,谢谢啦。