查询出全部数据,然后再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
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
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;
- }
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 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