首页 > 代码库 > mybatis如何做分页处理

mybatis如何做分页处理

1.首先根据自己实际需求编写实体类

 1 import java.io.Serializable;
 2 
 3 public class User implements Serializable{  //最好将该实体类序列化
 4 
 5     private static final long serialVersionUID = 1L;
 6     
 7     private Integer id;
 8     private String name;
 9     private String birthday;
10     private String gender;
11     private String career;
12     private String address;
13     private String mobile;
14     private String picPath;
15     
16     public User() {
17     }
18 
19     public User(Integer id, String name, String birthday, String gender, String career, String address, String mobile,
20             String picPath) {
21         this.id = id;
22         this.name = name;
23         this.birthday = birthday;
24         this.gender = gender;
25         this.career = career;
26         this.address = address;
27         this.mobile = mobile;
28         this.picPath = picPath;
29     }
30 
31 
32     public Integer getId() {
33         return id;
34     }
35     public void setId(Integer id) {
36         this.id = id;
37     }
38     public String getName() {
39         return name;
40     }
41     public void setName(String name) {
42         this.name = name;
43     }
44     public String getBirthday() {
45         return birthday;
46     }
47     public void setBirthday(String birthday) {
48         this.birthday = birthday;
49     }
50     public String getGender() {
51         return gender;
52     }
53     public void setGender(String gender) {
54         this.gender = gender;
55     }
56     public String getCareer() {
57         return career;
58     }
59     public void setCareer(String career) {
60         this.career = career;
61     }
62     public String getAddress() {
63         return address;
64     }
65     public void setAddress(String address) {
66         this.address = address;
67     }
68     public String getMobile() {
69         return mobile;
70     }
71     public void setMobile(String mobile) {
72         this.mobile = mobile;
73     }
74     public String getPicPath() {
75         return picPath;
76     }
77     public void setPicPath(String picPath) {
78         this.picPath = picPath;
79     }
80 
81 
82     @Override
83     public String toString() {
84         return "User [id=" + id + ", name=" + name + ", birthday=" + birthday + ", gender=" + gender + ", career="
85                 + career + ", address=" + address + ", mobile=" + mobile + ", picPath=" + picPath + "
86                 + "]";
87     }
88 }

2.编写分页所需要的通用bean,这是关键的一步。如果你用的是easyUI框架写的管理系统,currPage,PageSize,total,totalPage,rows这几个参数是必须的。

 1 import java.util.List;
 2 
 3 public class PageNationBean<T> {
 4     //请求参数
 5     private Integer currPage=1;//当前页
 6     private Integer PageSize=10;//页面数据条数
 7     
 8     //响应数据
 9     private Integer total;    //数据的总记录数
10     private Integer totalPage;//总页数
11     private List<T> rows;
12     
13     public PageNationBean() {
14     }
15     
16     public PageNationBean(Integer currPage, Integer pageSize, Integer total, Integer totalPage, List<T> rows) {
17         this.currPage = currPage;
18         this.PageSize = pageSize;
19         this.total = total;
20         this.totalPage = totalPage;
21         this.rows = rows;
22     }
23 
24 
25     public Integer getCurrPage() {
26         return currPage;
27     }
28     public void setCurrPage(Integer currPage) {
29         this.currPage = currPage;
30     }
31     public Integer getPageSize() {
32         return PageSize;
33     }
34     public void setPageSize(Integer pageSize) {
35         PageSize = pageSize;
36     }
37     public Integer getTotal() {
38         return total;
39     }
40     public void setTotal(Integer total) {
41         this.total = total;
42     }
43     public Integer getTotalPage() {
44         return totalPage;
45     }
46     public void setTotalPage(Integer totalPage) {
47         this.totalPage = totalPage;
48     }
49     public List<T> getRows() {
50         return rows;
51     }
52     public void setRows(List<T> rows) {
53         this.rows = rows;
54     }
55     @Override
56     public String toString() {
57         return "\n\tPageNationBean [currPage=" + currPage + ", PageSize=" + PageSize + ", total=" + total + ", totalPage="
58                 + totalPage + ", rows=" + rows + "]";
59     }
60     
61 }

3.数据库中建好需要测试的表,插入一定量的数据是肯定的

4.编写映射接口

public interface UserMapper {

    PageNationBean<User> getUsersByPagenation(PageNationBean<User> userBean);

}

5.编写映射所需的xml配置文件

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <!-- 命名空间与映射接口的全类名一致 -->
 6 <mapper namespace="com.yc.us.mapper.UserMapper">
 7     
 8     <resultMap type="PageNationBean" id="PageNationBeanMap">
 9         <result column="pageSize" property="pageSize"/>
10         <result column="currPage" property="currPage"/>
11         <collection property="rows" column="{pageSize=pageSize,currPage=currPage}" select="getUsers"/>
12     </resultMap>
13     
14     <!-- #是采用占用符    $是直接取到值 -->
15     <select id="getUsersByPagenation" parameterType="PageNationBean" resultMap="PageNationBeanMap">
16         select count(1) total,ceil(count(1)/${pageSize}) totalPage,${pageSize} pageSize,${currPage} currPage from profile
17     </select>
18     <select id="getUsers" resultType="User">
19         select * from
20         (select m.*,rownum rn from
21         (select * from profile) m where ${currPage}*${pageSize} >=rownum)
22         where rn > (${currPage}-1)*${pageSize}
23     </select>
24     
25 </mapper>

6.根据自己实际需求写一个测试类,进行实际测试

 

mybatis如何做分页处理