java中分页查询的实现_java中分页实现步骤图解

java中分页查询的实现_java中分页实现步骤图解java分页查询的实现分页要传入当前所在页数和每页显示记录数,再分页查询数据库,部分代码如下所示。传入参数实体类:publicclassMessageReq{privateStringmemberId;//会员idprivateintcurrentPage;//当前页privateintpageSize;//一页多少条记录privateint

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

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>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/186935.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ssl证书怎么用_为什么会ssl证书无效

    ssl证书怎么用_为什么会ssl证书无效1.打开网站:https://freessl.cn/按提示操作,验证类型:离线验证;2.会给出一个域名的访问路径和一个文件内容,按照域名解析的主机配置nginx或其它的web服务,返回文件给出的内容;3.确认文件url和内容无误后点验证;4.通过后可以在KeyManager里的证书管理里看到颁发的证书;5.点更多然后选择导出Nginx证书,crt为证书,key为密钥;6.将文件分发到nginx等其它需要证书的服务上去使用;注意:这里最关键的一步就是,你的域…

    2022年10月23日
    0
  • IOS-导航路线_iphone导航

    IOS-导航路线_iphone导航1.可以将需要导航的位置丢给系统自带的APP进行导航2.发送网络请求到公司服务器获取导航数据,然后自己手动绘制导航3.利用三方SDK实现导航(百度)>当点击开始导航时获取用户输入的起点和

    2022年8月4日
    5
  • PowerDesign的使用[通俗易懂]

    PowerDesign的使用[通俗易懂]使用powerdesign

    2022年7月3日
    36
  • 优先队列(堆)priority queue

    优先队列(堆)priority queue优先队列(堆)priorityqueue完全二叉树:除了最底层都被元素填满堆序性:除根节点,最小堆每个节点父亲的Key小于等于该节点的Key,最大堆反之优先队列的申明structHeapStruct;typedefstructHeapStruct*PriorityQueue;PriorityQueueInitialize(intMaxElements);void…

    2022年7月16日
    13
  • 微信授权网页扫码登录php,PHP实现微信开放平台扫码登录源码

    微信授权网页扫码登录php,PHP实现微信开放平台扫码登录源码1、首先到微信开放平台申请https://open.weixin.qq.com/获取到appid和APPSECRET,前台显示页面如下html>varobj=newWxLogin({id:”login_container”,appid:”wxed782be999f86e0e”,scope:”snsapi_login”,redirect_uri:encodeURICompon…

    2022年6月3日
    29
  • 光流法原理与实现「建议收藏」

    光流法原理与实现「建议收藏」以下内容摘自一篇硕士论文《视频序列中运动目标检测与跟踪算法的研究》:1950年Gibson首先提出了光流的概念,光流(opticalflow)法是空间运动物体在观测成像面上的像素运动的瞬时速度。物体在运动的时候,它在图像上对应点的亮度模式也在做相应的运动,这种图像亮度模式的表观运动就是光流。光流的研究就是利用图像序列中像素的强度数据的时域变化和相关性来确定各自像素位置的“运动”。光流表达

    2022年7月23日
    8

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号