mybatis分页实现

一:数组分页

        查询出全部数据,然后再list中截取需要的部分。

        mybatis接口:

package com.chen.boot01helloworld.dao;

import com.chen.boot01helloworld.entities.Payment;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * Created by 莫荒 on 2022/9/19 15:25
 */
@Mapper
public interface PaymentDao {
    List<Payment> queryPaymentsByArray();
}

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.chen.boot01helloworld.dao.PaymentDao">

    <select id="queryPaymentsByArray" resultMap="paymentMapper">
        select * from payment
 </select>

    <resultMap id="paymentMapper" type="payment">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <id column="serial" property="serial" jdbcType="VARCHAR"/>
    </resultMap>
</mapper>

service接口:

package com.chen.boot01helloworld.service;

import com.chen.boot01helloworld.entities.Payment;

import java.util.List;

/**
 * Created by 莫荒 on 2022/9/19 15:31
 */
public interface PaymentService {
    List<Payment> queryStudentsByArray(int currPage, int pageSize);//实现分页查询
}

service实现类:

package com.chen.boot01helloworld.service.impl;

import com.chen.boot01helloworld.dao.PaymentDao;
import com.chen.boot01helloworld.entities.Payment;
import com.chen.boot01helloworld.service.PaymentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 莫荒 on 2022/9/19 15:32
 */
@Service
public class PaymentImpl implements PaymentService {

    @Resource
    private PaymentDao paymentDao;

    @Override
    public List<Payment> queryStudentsByArray(int currPage, int pageSize) {

        //查询全部数据
        List<Payment> payments = paymentDao.queryPaymentsByArray();

        //从第几条数据开始
        int firstIndex = (currPage - 1) * pageSize;
        //到第几条数据结束
        int lastIndex = currPage * pageSize;
        return payments.subList(firstIndex, lastIndex); //直接在list中截取
    }
}

controller调用:

@ResponseBody
    @GetMapping("/getList/{currPage}/{pageSize}")
    public List getPaymentsList(@PathVariable("currPage") int currPage,@PathVariable("pageSize") int pageSize){
        List<Payment> payments = paymentService.queryStudentsByArray(currPage, pageSize);
        return payments;
    }

二:SQL分页

          mybatis接口:

    //    SQL分页
    List<Payment> queryPaymentBySql(Map<String, Object> data);

        xml实现:

<select id="queryPaymentBySql" parameterType="map" resultMap="paymentMapper">
        select * from payment limit #{currIndex} , #{pageSize}
</select>

        service接口:

List<Payment> queryPaymentBySql(int currPage, int pageSize);

        service实现:

@Override
    public List<Payment> queryPaymentBySql(int currPage, int pageSize) {
        HashMap<String, Object> data = new HashMap<>();
        data.put("currIndex",(currPage-1)*pageSize);
        data.put("pageSize",pageSize);
        return paymentDao.queryPaymentBySql(data);
    }

        controller调用:

@ResponseBody
    @GetMapping("/getList/{currPage}/{pageSize}")
    public List getPaymentsList(@PathVariable("currPage") int currPage,@PathVariable("pageSize") int pageSize){
        List<Payment> payments = paymentService.queryPaymentBySql(currPage, pageSize);
        return payments;
    }

补充

实体类:Page

package com.chen.boot01helloworld.pojo;

import java.util.List;

/**
 * Created by 莫荒 on 2022/9/20 10:32
 */
public class Page {
    private String pageNo = null;
    private String pageSize = null;
    private String code=null;
    private String massage =null;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMassage() {
        return massage;
    }

    public void setMassage(String massage) {
        this.massage = massage;
    }

    //    private String total = null;
    private List rows = null;

    /*public String getTotal() {
        return total;
    }*/

    /*public void setTotal(String total) {
        this.total = total;
    }*/

    public List getRows() {
        return rows;
    }

    public void setRows(List rows) {
        this.rows = rows;
    }

    public String getPageNo() {
        return pageNo;
    }

    public void setPageNo(String pageNo) {
        this.pageNo = pageNo;
    }

    public String getPageSize() {
        return pageSize;
    }

    public void setPageSize(String pageSize) {
        this.pageSize = pageSize;
    }

}

controller:

 @ResponseBody
    @RequestMapping("/getList")
    public Page getPaymentsList(int currPage,int pageSize){
        //当前页码数,默认为1
        currPage=currPage>0?currPage:1;

        //页面大小,默认为5
        pageSize=pageSize>0?pageSize:5;

        //获取当前页数据
        List<Payment> payments = paymentService.queryPaymentBySql(currPage, pageSize);

        //封装数据
        Page page = new Page();
        page.setPageNo(currPage+"");
        page.setPageSize(pageSize+"");
        page.setRows(payments);
        page.setMassage("成功返回分页数据");
        page.setCode("200");

        return page;
    }

测试:

 over

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

莫荒莫慌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值