• mybatis分页实现


    一:数组分页

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

            mybatis接口:

    1. package com.chen.boot01helloworld.dao;
    2. import com.chen.boot01helloworld.entities.Payment;
    3. import org.apache.ibatis.annotations.Mapper;
    4. import java.util.List;
    5. /**
    6. * Created by 莫荒 on 2022/9/19 15:25
    7. */
    8. @Mapper
    9. public interface PaymentDao {
    10. List queryPaymentsByArray();
    11. }

    xml实现:

    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    3. <mapper namespace="com.chen.boot01helloworld.dao.PaymentDao">
    4. <select id="queryPaymentsByArray" resultMap="paymentMapper">
    5. select * from payment
    6. </select>
    7. <resultMap id="paymentMapper" type="payment">
    8. <id column="id" property="id" jdbcType="BIGINT"/>
    9. <id column="serial" property="serial" jdbcType="VARCHAR"/>
    10. </resultMap>
    11. </mapper>

    service接口:

    1. package com.chen.boot01helloworld.service;
    2. import com.chen.boot01helloworld.entities.Payment;
    3. import java.util.List;
    4. /**
    5. * Created by 莫荒 on 2022/9/19 15:31
    6. */
    7. public interface PaymentService {
    8. List queryStudentsByArray(int currPage, int pageSize);//实现分页查询
    9. }

    service实现类:

    1. package com.chen.boot01helloworld.service.impl;
    2. import com.chen.boot01helloworld.dao.PaymentDao;
    3. import com.chen.boot01helloworld.entities.Payment;
    4. import com.chen.boot01helloworld.service.PaymentService;
    5. import org.springframework.stereotype.Service;
    6. import javax.annotation.Resource;
    7. import java.util.List;
    8. /**
    9. * Created by 莫荒 on 2022/9/19 15:32
    10. */
    11. @Service
    12. public class PaymentImpl implements PaymentService {
    13. @Resource
    14. private PaymentDao paymentDao;
    15. @Override
    16. public List<Payment> queryStudentsByArray(int currPage, int pageSize) {
    17. //查询全部数据
    18. List<Payment> payments = paymentDao.queryPaymentsByArray();
    19. //从第几条数据开始
    20. int firstIndex = (currPage - 1) * pageSize;
    21. //到第几条数据结束
    22. int lastIndex = currPage * pageSize;
    23. return payments.subList(firstIndex, lastIndex); //直接在list中截取
    24. }
    25. }

    controller调用:

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

    二:SQL分页

              mybatis接口:

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

            xml实现:

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

            service接口:

    List queryPaymentBySql(int currPage, int pageSize);

            service实现:

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

            controller调用:

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

    补充

    实体类:Page

    1. package com.chen.boot01helloworld.pojo;
    2. import java.util.List;
    3. /**
    4. * Created by 莫荒 on 2022/9/20 10:32
    5. */
    6. public class Page {
    7. private String pageNo = null;
    8. private String pageSize = null;
    9. private String code=null;
    10. private String massage =null;
    11. public String getCode() {
    12. return code;
    13. }
    14. public void setCode(String code) {
    15. this.code = code;
    16. }
    17. public String getMassage() {
    18. return massage;
    19. }
    20. public void setMassage(String massage) {
    21. this.massage = massage;
    22. }
    23. // private String total = null;
    24. private List rows = null;
    25. /*public String getTotal() {
    26. return total;
    27. }*/
    28. /*public void setTotal(String total) {
    29. this.total = total;
    30. }*/
    31. public List getRows() {
    32. return rows;
    33. }
    34. public void setRows(List rows) {
    35. this.rows = rows;
    36. }
    37. public String getPageNo() {
    38. return pageNo;
    39. }
    40. public void setPageNo(String pageNo) {
    41. this.pageNo = pageNo;
    42. }
    43. public String getPageSize() {
    44. return pageSize;
    45. }
    46. public void setPageSize(String pageSize) {
    47. this.pageSize = pageSize;
    48. }
    49. }

    controller:

    1. @ResponseBody
    2. @RequestMapping("/getList")
    3. public Page getPaymentsList(int currPage,int pageSize){
    4. //当前页码数,默认为1
    5. currPage=currPage>0?currPage:1;
    6. //页面大小,默认为5
    7. pageSize=pageSize>0?pageSize:5;
    8. //获取当前页数据
    9. List<Payment> payments = paymentService.queryPaymentBySql(currPage, pageSize);
    10. //封装数据
    11. Page page = new Page();
    12. page.setPageNo(currPage+"");
    13. page.setPageSize(pageSize+"");
    14. page.setRows(payments);
    15. page.setMassage("成功返回分页数据");
    16. page.setCode("200");
    17. return page;
    18. }

    测试:

     over

  • 相关阅读:
    怎么设置 idea terminal 窗口的编码格式
    nginx基础架构
    ArcGIS笔记8_测量得到的距离单位不是米?一经度一纬度换算为多少米?
    黑帽python第二版(Black Hat Python 2nd Edition)读书笔记 之 第八章 Windows常见特洛伊木马任务(4)沙箱检测
    requestanimationframe解决js动画过渡失效
    Python零基础-下【详细】
    考研数据结构大题整合_组三(LZH组)
    服务监控(五)之Grafana 接入Prometheus 的数据实现服务监控
    叶子数和深度
    2243. 计算字符串的数字和
  • 原文地址:https://blog.csdn.net/Ms_future/article/details/126946958