java分页查询接口的实现

分页要传入当前所在页数和每页显示记录数,再分页查询数据库,部分代码如下所示。

传入参数实体类:

public class MessageReq {

    private String memberId;//会员id
    private int currentPage;//当前页
    private int pageSize;//一页多少条记录
    private int startIndex;//从哪一行开始
    private int endIndex;//从哪一行结束


    public String getMemberId() {
        return memberId;
    }
    public void setMemberId(String memberId) {
        this.memberId = memberId;
    }
    public int getCurrentPage() {
        return currentPage;
    }   
    public int getStartIndex() {
        return startIndex;
    }
    public int getEndIndex() {
        return endIndex;
    }
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }
    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }
    public void setEndIndex(int endIndex) {
        this.endIndex = endIndex;
    }
    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    //根据当前所在页数和每页显示记录数计算出startIndex和endIndex
    public void setStartIndexEndIndex(){
         this.startIndex=(this.getCurrentPage()-1)*this.getPageSize();
         this.endIndex= (this.getCurrentPage()-1)*this.getPageSize()+this.getPageSize();
    }

}

分页工具类:

public class Page<T>{
    private int currentPage = 1; // 当前页
    private int pageSize = 20; //每页显示记录数
    private int startRecord = 1; //起始查询记录
    private int totalPage = 0; //总页数
    private int totalRecord = 0; //总记录数

    private List<T> datas;

    public Page(){}

    public Page(int currentPage, int pageSize) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        if(this.currentPage <= 0) {
            this.currentPage = 1;
        }
        if(this.pageSize <=0) {
            this.pageSize = 1;
        }
    }

    public Page(int currentPage, int pageSize, int totalRecord) {
        this(currentPage, pageSize);
        this.totalRecord = totalRecord;
        if(this.totalRecord <=0) {
            this.totalRecord = 1;
        }
    }

    public int getCurrentPage() {
        if(currentPage <= 0) {
            return 1;
        }
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public int getTotalRecord() {
        if(totalRecord < 0) {
            return 0;
        }
        return totalRecord;
    }
    public void setTotalRecord(int totalRecord) {
        this.totalRecord = totalRecord;
    }
    public List<T> getDatas() {
        return datas;
    }
    public void setDatas(List<T> datas) {
        this.datas = datas;
    }

    public int getTotalPage() {
        if(totalRecord <= 0) {
            return 0;
        }
        int size = totalRecord / pageSize;//总条数/每页显示的条数=总页数
        int mod = totalRecord % pageSize;//最后一页的条数
        if(mod != 0) {
            size++;
        }
        totalPage = size;
        return totalPage;
    }

    public int getStartRecord() {
        startRecord = (getCurrentPage() - 1) * pageSize;
        return startRecord;
    }

}

Manager层

public interface MessageManager {

    //分页查询消息
    public Page<Message> queryMessage(MessageReq req);
}
@Component
public class MessageManagerImpl implements MessageManager{ 
   

    @Autowired
    private MessageMapper messageMapper;

    @Override
    public Page<Message> queryMessage(MessageReq req) {
        Page<Message> page = new Page<Message>();
        int pageCount = messageMapper.getMessageNum(req.getMemberId());//得到总条数
        page = initPage(page, pageCount, req);
        List<Message> message= messageMapper.queryMessage(req);
        if (!message.isEmpty()) {
            page.setDatas(message);
        }
        return page;
    }

    private Page<Message> initPage(Page<Message> page, int pageCount,
            MessageReq messageReq) {
        page.setTotalRecord(pageCount);
        page.setCurrentPage(messageReq.getCurrentPage());
        page.setPageSize(messageReq.getPageSize());
        messageReq.setStartIndexEndIndex();
        return page;    
    }   

}

Dao层

public interface MessageMapper {

    //分页查询
    public List<Message> queryMessage(Messagereq);

    //查询总条数
    public int getMessageNum(String memberId);
}

mybatis的.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.sf.ccsp.member.dao.mapper.MessageMapper">
   <resultMap id="MessageResultMap" type="com.sf.ccsp.member.dao.domain.message.Message" >
      <result column="ID" property="id" jdbcType="VARCHAR" />
      <result column="MEMBERID" property="memberId" jdbcType="VARCHAR" />
      <result column="MESSAGE_CLASSIFY" property="messageClassify" jdbcType="VARCHAR" />
      <result column="MESSAGE_CODE" property="messageCode" jdbcType="VARCHAR" />
      <result column="MESSAGE_CONTENT" property="messageContent" jdbcType="VARCHAR" />
      <result column="MESSAGE_STATUS" property="messageStatus" jdbcType="VARCHAR" />
   </resultMap>

   <select id="queryMessage" resultMap="MessageResultMap" parameterType="com.sf.ccsp.member.client.request.MessageReq">
     select *
     from cx_customer_message
     where MEMBERID = #{memberId, jdbcType=VARCHAR}
     and ISVALID = '1'
     LIMIT #{startIndex,jdbcType=INTEGER},#{pageSize,jdbcType=INTEGER}  
   </select>

    <select id="getMessageNum" resultType="INTEGER" parameterType="String">
      select count(*)
      from cx_customer_message
      where MEMBERID = #{memberId, jdbcType=VARCHAR}
    </select>

</mapper>