IT序号网

使用Java封装的分页工具类

shasha 2021年06月03日 编程语言 308 0

1、分页的插件也有很多,比如PageHelper,这种后端分页框架,将数据都查询出来,设置一下起始页,每页显示的数据就行了,操作起来十分方便。还有前端分页插件,DisplayTag,将数据查询出来以后,用前端分页插件展示数据,设置一下起始页,每页显示的数据就行了,做课设、毕设再爽不过了。还有很多其他分页插件,都是大神封装好的,既然不会造轮子,就先学会熟练使用轮子吧。

开源届有一句经典的话,不要重复发明轮子。意思就是有现成的成熟实现就用它,不要自己从头实现一遍。
所以,相反的重新实现已有的模块(为了锻炼技术,或者得到更好的性能)就叫重复发明轮子,造轮子。

2、分页插件,很多很多,但是有的时候,项目比较急,或者这些分页插件不是很适合你的项目,这个适合最好使用一个Java封装的分页工具类,快速实现你分页的效果。

 1 package com.bie.utils; 
 2  
 3 import java.util.List; 
 4  
 5 /** 
 6  * 使用泛型可以传入任何类型的实体类 
 7  * 
 8  * @ProjectName: nationalpolicy 
 9  * @Package: com.bie.utils 
10  * @ClassName: PageBean 
11  * @Author: biehl 
12  * @Description: 使用泛型可以传入任何类型的实体类 
13  * @Date: 2020/2/28 18:57 
14  * @Version: 1.0 
15  */ 
16 public class PageBean<T> { 
17  
18  
19     private List<T> lists;// 存放需要显示的实体类数据 
20     private Integer pageNo = 1;// 当前页码数(默认给1),需要传参 
21     private Integer pageSize; // 每页显示的行数,需要传参 
22     // this.totalPage = rows % pageSize == 0 ? rows / pageSize : (rows / pageSize + 1); 
23     private Integer totalPage;// 总页数,是根据总行数和每页显示的行数计算出来的结果 
24     private Integer rows;// 总行数,总行数是查询出来的数据表总记录数 
25  
26     // 对私有属性的封装 
27     // 不需要对外提供totalPage总页数的set设值方法,因为totalPage是根据总行数和每页显示的行数求出来的 
28     public List<T> getLists() { 
29         return lists; 
30     } 
31  
32     public void setLists(List<T> lists) { 
33         this.lists = lists; 
34     } 
35  
36     public Integer getPageNo() { 
37         return pageNo; 
38     } 
39  
40     public Integer getPageSize() { 
41         return pageSize; 
42     } 
43  
44     public void setPageSize(Integer pageSize) { 
45         this.pageSize = pageSize; 
46     } 
47  
48     public Integer getTotalPage() { 
49         return totalPage; 
50     } 
51  
52     public Integer getRows() { 
53         return rows; 
54     } 
55  
56     /** 
57      * 设置总行数据并求出总页数 
58      * 
59      * @param rows 此参数是总行数 
60      */ 
61     public void setRows(Integer rows) { 
62         this.rows = rows; 
63         //页数根据传入的总行数以及每页显示的行数,求出总页数 
64         this.totalPage = rows % pageSize == 0 ? rows / pageSize : (rows / pageSize + 1); 
65     } 
66  
67     /** 
68      * 设置页码 
69      * 
70      * @param pageNo 当前页数 
71      */ 
72     public void setPageNo(Integer pageNo) { 
73         //如果传入的页码为空或者小于0  就默认给1 
74         if (null == pageNo || pageNo < 0) { 
75             this.pageNo = 1; 
76             //如果当前页码数大于总页码数,就让当前页码数等于最大页码数 
77         } else if (pageNo > this.totalPage && this.totalPage > 0) { 
78             this.pageNo = this.totalPage; 
79             //都符合条件就让当前页码数等于传入的页码数 
80         } else { 
81             this.pageNo = pageNo; 
82         } 
83     } 
84  
85 }

3、既然分页工具类已经封装好了,那么如何调用该工具类呢,如下所示:

由于设计到业务,所以用xxx代替了,哈哈哈,凑活看吧。

  1 package com.bie.controller; 
  2  
  3 import com.bie.po.RxxxPxxxxx; 
  4 import com.bie.service.RxxxPxxxxxService; 
  5 import com.bie.service.RxxxPxxxxxThemeService; 
  6 import com.bie.utils.PageBean; 
  7 import org.springframework.beans.factory.annotation.Autowired; 
  8 import org.springframework.stereotype.Controller; 
  9 import org.springframework.web.bind.annotation.RequestMapping; 
 10 import org.springframework.web.bind.annotation.RequestMethod; 
 11 import org.springframework.web.bind.annotation.RequestParam; 
 12 import org.springframework.web.bind.annotation.ResponseBody; 
 13  
 14 import java.util.List; 
 15  
 16 /** 
 17  * @ProjectName: nxxxxxpxxxxxx 
 18  * @Package: com.bie.controller 
 19  * @ClassName: RxxxPxxxxxController 
 20  * @Author: biehl 
 21  * @Description: ${description} 
 22  * @Date: 2020/2/28 19:42 
 23  * @Version: 1.0 
 24  */ 
 25 @Controller 
 26 @RequestMapping(value = "/xxxxxx") 
 27 public class RxxxPxxxxxController { 
 28  
 29     @Autowired 
 30     private RxxxPxxxxxService rxxxPxxxxxService; 
 31  
 32     @Autowired 
 33     private RxxxPxxxxxThemeService rxxxPxxxxxThemeService; 
 34  
 35  
 36     /** 
 37      * @param aaa 
 38      * @param bbb 
 39      * @param ccc 
 40      * @param ddd 
 41      * @param current      当前页,默认是第一页,从1开始 
 42      * @param size         每页多少,默认是每页20条数据 
 43      * @return 
 44      */ 
 45     @RequestMapping(value = "/xxxxxxxxx/xxxxx", method = RequestMethod.GET) 
 46     @ResponseBody 
 47     public List<RxxxPxxxxx> selectRxxxPxxxxxPageBean(@RequestParam(value = "aaa", required = false) String aaa, @RequestParam(value = "bbb", required = false) String bbb, @RequestParam(value = "ccc", required = false) String ccc, @RequestParam(value = "ddd", required = false) String ddd, @RequestParam(value = "current", defaultValue = "1") int current, @RequestParam(value = "size", defaultValue = "20") int size) { 
 48         // 得到数据表中的行数 
 49         int count = rxxxPxxxxxService.selectRxxxPxxxxxPageBeanCount(); 
 50         // 创建工具类对象 
 51         PageBean<RxxxPxxxxx> pager = new PageBean<>(); 
 52         // 每页显示的行数 
 53         pager.setPageSize(size); 
 54         // 设置总行数 
 55         pager.setRows(count); 
 56         // 设置当前页数 
 57         pager.setPageNo(current); 
 58         // 计算出总页数 
 59         int totalPage = count % size == 0 ? count / size : (count / size + 1); 
 60         // mysql获取分页第一个参数 (pager.getPageNo() - 1) * pager.getPageSize(); 
 61         // 获取分页第一个参数 
 62         // Limit接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数, 
 63         // 第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。 
 64         // 初始记录行的偏移量是 0(而不是 1)。 
 65         int first = (pager.getPageNo() - 1) * pager.getPageSize(); 
 66         //调用service层将分页的两个参数传递过去 
 67         List<RxxxPxxxxx> rxxxPxxxxxs = rxxxPxxxxxService.selectRxxxPxxxxxPageBean(aaa, bbb, ccc, ddd, first, pager.getPageSize()); 
 68  
 69  
 70         // 将得到的集合对象放到PagerBean类里,这里面主要看你需要返回什么格式的数据,可以根据需要返回的格式进行封装即可 
 71         pager.setLists(rxxxPxxxxxs); 
 72         List<RxxxPxxxxx> lists = pager.getLists(); 
 73         return lists; 
 74  
 75         // 如果是需要其他格式的返回类型,在下面可以封装返回的逻辑或者在业务层封装返回的逻辑 
 76         // 封装返回结果,如果返回的是下面格式的数据,返回类型换成NxxxxxxxPxxxxxxxResult即可。 
 77 //        List<RxxxPxxxxx> resultLists = new ArrayList<>(); 
 78 //        if (rxxxPxxxxxs != null && rxxxPxxxxxs.size() > 0 && !rxxxPxxxxxs.isEmpty()) { 
 79 //            for (RxxxPxxxxx rxxxPxxxxx : rxxxPxxxxxs) { 
 80 //                RxxxPxxxxx rxxxPxxxxx1 = new RxxxPxxxxx(); 
 81 //                // 获取到policyId 
 82 //                int policyId = rxxxPxxxxx.getId(); 
 83 //                // 封装主题信息 
 84 //                List<RxxxPxxxxxTheme> rxxxPxxxxxThemes = rxxxPxxxxxThemeService.selectRxxxPxxxxxThemeByPolicyId(policyId); 
 85 //                if (rxxxPxxxxxThemes != null && rxxxPxxxxxThemes.size() > 0 && !rxxxPxxxxxThemes.isEmpty()) { 
 86 //                    int[] themeIds = new int[rxxxPxxxxxThemes.size()]; 
 87 //                    for (int i = 0; i < rxxxPxxxxxThemes.size(); i++) { 
 88 //                        int themeId = rxxxPxxxxxThemes.get(i).getThemeId(); 
 89 //                        themeIds[i] = themeId; 
 90 //                    } 
 91 //                    rxxxPxxxxx1.setThemeIds(themeIds); 
 92 //                } 
 93 // 
 94 //                // 封装themeIds 
 95 //                rxxxPxxxxx1.setId(rxxxPxxxxx.getId()); 
 96 //                rxxxPxxxxx1.setAaa(rxxxPxxxxx.getAaa()); 
 97 //                rxxxPxxxxx1.setBbb(rxxxPxxxxx.getBbb()); 
 98 //                rxxxPxxxxx1.setCcc(rxxxPxxxxx.getCcc()); 
 99 //                rxxxPxxxxx1.setDdd(rxxxPxxxxx.getDdd()); 
100 //                rxxxPxxxxx1.setEee(rxxxPxxxxx.getEee()); 
101 //                rxxxPxxxxx1.setFff(rxxxPxxxxx.getFff()); 
102 //                rxxxPxxxxx1.setGgg(rxxxPxxxxx.getGgg()); 
103 //                rxxxPxxxxx1.setIii(rxxxPxxxxx.getIii()); 
104 // 
105 //                resultLists.add(rxxxPxxxxx1); 
106 //            } 
107 //        } 
108 // 
109 //        RxxxPxxxxxResult rxxxPxxxxxResult = new RxxxPxxxxxResult(); 
110 //        rxxxPxxxxxResult.setRecords(resultLists); 
111 //        rxxxPxxxxxResult.setTotal(count); 
112 //        rxxxPxxxxxResult.setSize(size); 
113 //        rxxxPxxxxxResult.setCurrent(current); 
114 //        rxxxPxxxxxResult.setOrders(new int[0]); 
115 //        rxxxPxxxxxResult.setSearchCount(true); 
116 //        rxxxPxxxxxResult.setPages(totalPage); 
117 // 
118 //        NxxxxxxxPxxxxxxxResult nationalPolicyResult = new NxxxxxxxPxxxxxxxResult(); 
119 //        NxxxxxxxPxxxxxxxResult result = new NxxxxxxxPxxxxxxxResult(); 
120 //        if (resultLists != null && resultLists.size() > 0 && !resultLists.isEmpty()) { 
121 //            result = nationalPolicyResult.success(rxxxPxxxxxResult); 
122 //        } else { 
123 //            result = nationalPolicyResult.build(1, "fail"); 
124 //        } 
125 //        return result; 
126     } 
127 }

4、既然分页工具类也封装好了,怎么调用也写好了,那么mysql怎么写的呢,这里使用的mybatis。

由于设计到业务,这里面只留了大概的架子,基本可以看得懂,就不再叙述了。

 1 <?xml version="1.0" encoding="UTF-8"?> 
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
 3         "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 
 4 <mapper namespace="com.bie.mapper.RxxxPxxxxxMapper"> 
 5  
 6     <resultMap id="BaseResultMap" type="com.bie.po.RxxxPxxxxx"> 
 7         <result column="id" jdbcType="INTEGER" property="id"/> 
 8         <result column="aaa" jdbcType="VARCHAR" property="aaa"/> 
 9         <result column="bbb" jdbcType="VARCHAR" property="bbb"/> 
10         <result column="ccc" jdbcType="VARCHAR" property="ccc"/> 
11         <result column="ddd" jdbcType="VARCHAR" property="ddd"/> 
12     </resultMap> 
13  
14     <sql id="sql_where"> 
15         <where> 
16             <if test="aaa != null and aaa != '' "> 
17                 a.aaa = #{aaa} 
18             </if> 
19             <if test="bbb != null and bbb !='' "> 
20                 and 
21                     a.id = b.policy_id 
22                 and 
23                     b.bbb = #{bbb} 
24             </if> 
25             <if test="ccc != null and ccc !='' "> 
26                 and a.ccc = #{ccc} 
27             </if> 
28             <if test="ddd != null and ddd !='' "> 
29                 and a.ddd LIKE CONCAT('%',#{ddd},'%') 
30             </if> 
31         </where> 
32  
33     </sql> 
34  
35     <sql id="sql_from"> 
36         FROM rxxxpxxx a,rxxxpxxxxxtxxxxx b 
37         <include refid="sql_where"></include> 
38     </sql> 
39  
40  
41     <select id="selectRxxxPxxxxxPageBean" resultMap="BaseResultMap"> 
42         <![CDATA[ 
43           SELECT * 
44         ]]> 
45         <include refid="sql_from"></include> 
46         <![CDATA[ 
47           ORDER BY a.pxxxx_time DESC 
48           limit #{current}, #{size} 
49         ]]> 
50     </select> 
51  
52     <!-- 分页查询的count --> 
53     <select id="selectRxxxPxxxxxPageBeanCount" resultType="int"> 
54         <![CDATA[ 
55                 SELECT COUNT(*) from rxxxpxxx 
56         ]]> 
57     </select> 
58  
59 </mapper>

作者:别先生

博客园:IT虾米网

如果您想及时得到个人撰写文章以及著作的消息推送,可以扫描上方二维码,关注个人公众号哦。


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!