1.首先根据自己实际需求编写实体类
import java.io.Serializable; public class User implements Serializable{ //最好将该实体类序列化 private static final long serialVersionUID = 1L; private Integer id; private String name; private String birthday; private String gender; private String career; private String address; private String mobile; private String picPath; public User() { } public User(Integer id, String name, String birthday, String gender, String career, String address, String mobile, String picPath) { this.id = id; this.name = name; this.birthday = birthday; this.gender = gender; this.career = career; this.address = address; this.mobile = mobile; this.picPath = picPath; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getCareer() { return career; } public void setCareer(String career) { this.career = career; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } public String getPicPath() { return picPath; } public void setPicPath(String picPath) { this.picPath = picPath; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", birthday=" + birthday + ", gender=" + gender + ", career=" + career + ", address=" + address + ", mobile=" + mobile + ", picPath=" + picPath + " + "]"; } }
2.编写分页所需要的通用bean,这是关键的一步。如果你用的是easyUI框架写的管理系统,currPage,PageSize,total,totalPage,rows这几个参数是必须的。
import java.util.List; public class PageNationBean<T> { //请求参数 private Integer currPage=1;//当前页 private Integer PageSize=10;//页面数据条数 //响应数据 private Integer total; //数据的总记录数 private Integer totalPage;//总页数 private List<T> rows; public PageNationBean() { } public PageNationBean(Integer currPage, Integer pageSize, Integer total, Integer totalPage, List<T> rows) { this.currPage = currPage; this.PageSize = pageSize; this.total = total; this.totalPage = totalPage; this.rows = rows; } public Integer getCurrPage() { return currPage; } public void setCurrPage(Integer currPage) { this.currPage = currPage; } public Integer getPageSize() { return PageSize; } public void setPageSize(Integer pageSize) { PageSize = pageSize; } public Integer getTotal() { return total; } public void setTotal(Integer total) { this.total = total; } public Integer getTotalPage() { return totalPage; } public void setTotalPage(Integer totalPage) { this.totalPage = totalPage; } public List<T> getRows() { return rows; } public void setRows(List<T> rows) { this.rows = rows; } @Override public String toString() { return "\n\tPageNationBean [currPage=" + currPage + ", PageSize=" + PageSize + ", total=" + total + ", totalPage=" + totalPage + ", rows=" + rows + "]"; } }
3.数据库中建好需要测试的表,插入一定量的数据是肯定的
4.编写映射接口
public interface UserMapper { PageNationBean<User> getUsersByPagenation(PageNationBean<User> userBean); }
5.编写映射所需的xml配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 命名空间与映射接口的全类名一致 --> <mapper namespace="com.yc.us.mapper.UserMapper"> <resultMap type="PageNationBean" id="PageNationBeanMap"> <result column="pageSize" property="pageSize"/> <result column="currPage" property="currPage"/> <collection property="rows" column="{pageSize=pageSize,currPage=currPage}" select="getUsers"/> </resultMap> <!-- #是采用占用符 $是直接取到值 --> <select id="getUsersByPagenation" parameterType="PageNationBean" resultMap="PageNationBeanMap"> select count(1) total,ceil(count(1)/${pageSize}) totalPage,${pageSize} pageSize,${currPage} currPage from profile </select> <select id="getUsers" resultType="User"> select * from (select m.*,rownum rn from (select * from profile) m where ${currPage}*${pageSize} >=rownum) where rn > (${currPage}-1)*${pageSize} </select> </mapper>
6.根据自己实际需求写一个测试类,进行实际测试