• 通用分页(1)


    目录

    前言

    一、PageBean

    二、反射通用后台查询

    三、通用的分页后台查询

    四、junit4

    总结


    前言

    上次我们分享了自定义标签——jsp标签增强,今天分享的内容是通用分页。


    一、PageBean

    1. package com.zhw.util;
    2. public class PageBean {
    3. private int page = 1;// 页码
    4. private int rows = 10;// 页大小
    5. private int total = 0;// 总记录数
    6. private boolean pagination = true;// 是否分页
    7. public PageBean() {
    8. super();
    9. }
    10. public int getPage() {
    11. return page;
    12. }
    13. public void setPage(int page) {
    14. this.page = page;
    15. }
    16. public int getRows() {
    17. return rows;
    18. }
    19. public void setRows(int rows) {
    20. this.rows = rows;
    21. }
    22. public int getTotal() {
    23. return total;
    24. }
    25. public void setTotal(int total) {
    26. this.total = total;
    27. }
    28. public void setTotal(String total) {
    29. this.total = Integer.parseInt(total);
    30. }
    31. public boolean isPagination() {
    32. return pagination;
    33. }
    34. public void setPagination(boolean pagination) {
    35. this.pagination = pagination;
    36. }
    37. /**
    38. * 获得起始记录的下标
    39. *
    40. * @return
    41. */
    42. public int getStartIndex() {
    43. return (this.page - 1) * this.rows;
    44. }
    45. @Override
    46. public String toString() {
    47. return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]";
    48. }
    49. }

    二、反射通用后台查询 

    原生:
    1. public List<Book> list(Book book,PageBean pageBean) throws Exception{
    2. List<Book> list = new ArrayList<Book>();
    3. Connection con = DBAccess.getConnection();
    4. String sql = "select * from t_mvc_book where 1=1 ";
    5. String bname = book.getBname();
    6. if(StringUtils.isNotBlank(bname)) {
    7. sql += "and bname like '%"+bname+"%' ";
    8. }
    9. PreparedStatement pst = con.prepareStatement(sql);
    10. ResultSet rs = pst.executeQuery();
    11. while(rs.next()) {
    12. list.add(new Book(rs.getInt("bid"), rs.getString("bname"), rs.getFloat("price")));
    13. }
    14. return list;
    15. }
    反射:
    1. public List<T> list(String sql,Class<T> clz) throws Exception{
    2. List<T> list = new ArrayList<T>();
    3. Connection con = DBAccess.getConnection();
    4. String sql = "select * from t_mvc_book where 1=1 ";
    5. String bname = book.getBname();
    6. if(StringUtils.isNotBlank(bname)) {
    7. sql += "and bname like '%"+bname+"%' ";
    8. }
    9. PreparedStatement pst = con.prepareStatement(sql);
    10. ResultSet rs = pst.executeQuery();
    11. while(rs.next()) {
    12. // list.add(new Book(rs.getInt("bid"), rs.getString("bname"), rs.getFloat("price")));
    13. T t = clz.newInstance();
    14. Field[] fields = clz.getDeclaredFields();
    15. for (Field f : fields) {
    16. f.setAccessible(true);
    17. f.set(t, rs.getObject(f.getName()));
    18. }
    19. list.add(t);
    20. }
    21. return list;
    22. }

    三、通用的分页后台查询

    1. public List<T> executeQuery(String sql,PageBean pageBean,Class<T> clz) throws Exception{
    2. List<T> list = new ArrayList<T>();
    3. Connection con = DBAccess.getConnection();
    4. PreparedStatement pst = null;
    5. ResultSet rs = null;
    6. String countSQL = getCountSQL(sql);
    7. String pageSQL = getPageSQL(sql,pageBean);
    8. if(pageBean != null && pageBean.isPagination()) {
    9. pst = con.prepareStatement(countSQL);
    10. rs = pst.executeQuery();
    11. if(rs.next()) {
    12. pageBean.setTotal(String.valueOf(rs.getObject(1)));
    13. }
    14. pst = con.prepareStatement(pageSQL);
    15. rs = pst.executeQuery();
    16. }else {
    17. pst = con.prepareStatement(sql);
    18. rs = pst.executeQuery();
    19. }
    20. while(rs.next()) {
    21. // list.add(new Book(rs.getInt("bid"), rs.getString("bname"), rs.getFloat("price")));
    22. T t = clz.newInstance();
    23. Field[] fields = clz.getDeclaredFields();
    24. for (Field f : fields) {
    25. f.setAccessible(true);
    26. f.set(t, rs.getObject(f.getName()));
    27. }
    28. list.add(t);
    29. }
    30. return list;
    31. }
    32. private String getPageSQL(String sql, PageBean pageBean) {
    33. return sql + " limit "+pageBean.getStartIndex()+","+pageBean.getRows();
    34. }
    35. private String getCountSQL(String sql) {
    36. return "select count(1) FROM ("+sql+") t";
    37. }

    四、junit4

      我们测试的时候经常会在main方法中打印。测试的方法越来越多,main方法也越来越杂。

      这时候就可以用到Junit

    在测试类右键,Ctrl+N, 搜索junit,选择第一个。

     选择junit 4,并勾选setupteardown 

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

    finish。测试类就完成了。

     

    1. package com.zhw.dao;
    2. import java.util.List;
    3. import org.junit.After;
    4. import org.junit.Before;
    5. import org.junit.Test;
    6. import com.zhw.entity.Book;
    7. public class BookDaoTest {
    8. @Before
    9. public void setUp() throws Exception {
    10. System.out.println("被测试方法执行前调用");
    11. }
    12. @After
    13. public void tearDown() throws Exception {
    14. System.out.println("被测试方法执行后调用");
    15. }
    16. @Test
    17. public void testList() {
    18. List<Book> list;
    19. try {
    20. list = new BookDao().list(new Book(), null);
    21. for (Book book : list) {
    22. System.out.println(book);
    23. }
    24. } catch (Exception e) {
    25. // TODO Auto-generated catch block
    26. e.printStackTrace();
    27. }
    28. }
    29. }

    运行结果:

     

    如果我们要测试其他方法

     新写一个:

     

    1. package com.zhw.dao;
    2. import java.util.List;
    3. import org.junit.After;
    4. import org.junit.Before;
    5. import org.junit.Test;
    6. import com.zhw.entity.Book;
    7. public class BookDaoTest {
    8. @Before
    9. public void setUp() throws Exception {
    10. System.out.println("被测试方法执行前调用");
    11. }
    12. @After
    13. public void tearDown() throws Exception {
    14. System.out.println("被测试方法执行后调用");
    15. }
    16. @Test
    17. public void testList() {
    18. List<Book> list;
    19. try {
    20. list = new BookDao().list(new Book(), null);
    21. for (Book book : list) {
    22. System.out.println(book);
    23. }
    24. } catch (Exception e) {
    25. // TODO Auto-generated catch block
    26. e.printStackTrace();
    27. }
    28. }
    29. @Test
    30. public void testList2() {
    31. System.out.println("测试代码2");
    32. }
    33. }

    结果如下:


    总结

    这次分享的内容是通用标签分页,便于使用,如果有多个类型需要分页则不需要写重复代码,只需调用写好的通用分页代码,缩短了开发时间,减少代码量。

    希望对你能有帮助,下期预告通用分页(2)。

    我是九歌,一个喜欢编程的程序员。

    如有错误还望指正,谢谢啦。

  • 相关阅读:
    八、SpringMVC(2)
    MPJ: MyBatis-Plus-Join连表查询
    【数据结构与算法】无重复字符的最长子串
    Spring知识点讲解 【笔记】
    LoRA微调语言大模型的实用技巧
    yolov5 奇奇怪怪的错误汇总
    MySQL存储引擎
    【html5期末大作业】基于HTML+CSS+JavaScript管理系统页面模板
    Java面向对象---尚硅谷Java入门视频学习
    【电梯控制系统】基于VHDL语言和状态机实现的电梯控制系统的设计,使用了状态机
  • 原文地址:https://blog.csdn.net/qq_62331938/article/details/125402650