• jquey+mybatis-plus实现简单分页功能


    这篇文章介绍一下怎么通过JQuery结合mybatis-plus的分页插件实现原生HTML页面的分页效果,没有使用任何前端框架,主要是对前端知识的应用。

    创建Springboot项目

    Intellij IDEA中创建一个Springboot项目,项目名为pager。

    添加必须的依赖包

    修改项目的pom.xml,添加一些基本的依赖:jdbc、mysql、mybatis、mybatis-plus、lombok、druid数据库连接池。

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <parent>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-parentartifactId>
    8. <version>2.5.9version>
    9. <relativePath />
    10. parent>
    11. <groupId>cn.edu.sgu.wwwgroupId>
    12. <artifactId>pagerartifactId>
    13. <version>0.0.1-SNAPSHOTversion>
    14. <name>pagername>
    15. <description>jquey+mybatis-plus实现简单分页功能description>
    16. <properties>
    17. <java.version>1.8java.version>
    18. <mysql.version>8.0.28mysql.version>
    19. <druid.version>1.1.21druid.version>
    20. <lombok.version>1.18.22lombok.version>
    21. <mybatis.version>2.2.2mybatis.version>
    22. <mybatis-plus.version>3.5.1mybatis-plus.version>
    23. properties>
    24. <dependencies>
    25. <dependency>
    26. <groupId>org.springframework.bootgroupId>
    27. <artifactId>spring-boot-starter-webartifactId>
    28. dependency>
    29. <dependency>
    30. <groupId>org.springframework.bootgroupId>
    31. <artifactId>spring-boot-starter-testartifactId>
    32. <scope>testscope>
    33. dependency>
    34. <dependency>
    35. <groupId>org.projectlombokgroupId>
    36. <artifactId>lombokartifactId>
    37. <version>${lombok.version}version>
    38. dependency>
    39. <dependency>
    40. <groupId>mysqlgroupId>
    41. <artifactId>mysql-connector-javaartifactId>
    42. <version>${mysql.version}version>
    43. dependency>
    44. <dependency>
    45. <groupId>com.alibabagroupId>
    46. <artifactId>druidartifactId>
    47. <version>${druid.version}version>
    48. dependency>
    49. <dependency>
    50. <groupId>org.mybatis.spring.bootgroupId>
    51. <artifactId>mybatis-spring-boot-starterartifactId>
    52. <version>${mybatis.version}version>
    53. dependency>
    54. <dependency>
    55. <groupId>com.baomidougroupId>
    56. <artifactId>mybatis-plus-boot-starterartifactId>
    57. <version>${mybatis-plus.version}version>
    58. dependency>
    59. dependencies>
    60. <build>
    61. <plugins>
    62. <plugin>
    63. <groupId>org.springframework.bootgroupId>
    64. <artifactId>spring-boot-maven-pluginartifactId>
    65. plugin>
    66. plugins>
    67. build>
    68. project>

    添加数据源配置

    将项目默认的配置文件application.properties重命名为application.yml。

    添加启动端口、数据库、日志隔离级别的配置~

    1. server:
    2. port: 8090
    3. logging:
    4. level:
    5. cn.edu.sgu.www.pager: debug
    6. spring:
    7. application:
    8. name: pager
    9. datasource:
    10. username: root
    11. password: root
    12. url: jdbc:mysql://localhost:3306/pager
    13. driver-class-name: com.mysql.cj.jdbc.Driver
    14. type: com.alibaba.druid.pool.DruidDataSource

    准备数据库表

    创建数据库pager,然后执行项目src/main/resources目录下的pager.sql脚本文件。

    添加mybatis-plus分页插件

    1. package cn.edu.sgu.www.pager.config;
    2. import com.baomidou.mybatisplus.annotation.DbType;
    3. import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
    4. import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
    5. import org.springframework.context.annotation.Bean;
    6. import org.springframework.context.annotation.Configuration;
    7. /**
    8. * Mybatis-Plus配置类
    9. * @author heyunlin
    10. * @version 1.0
    11. */
    12. @Configuration
    13. public class MybatisPlusConfig {
    14. @Bean
    15. public MybatisPlusInterceptor mybatisPlusInterceptor() {
    16. MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    17. // 添加分页插件
    18. interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
    19. return interceptor;
    20. }
    21. }

    创建响应相关实体类

    响应状态码枚举类

    1. package cn.edu.sgu.www.pager.restful;
    2. /**
    3. * 响应状态码
    4. * @author heyunlin
    5. * @version 1.0
    6. */
    7. public enum ResponseCode {
    8. /**
    9. * 请求成功
    10. */
    11. OK(200),
    12. /**
    13. * 失败的请求
    14. */
    15. BAD_REQUEST(400),
    16. /**
    17. * 未授权
    18. */
    19. UNAUTHORIZED(401),
    20. /**
    21. * 禁止访问
    22. */
    23. FORBIDDEN(403),
    24. /**
    25. * 找不到
    26. */
    27. NOT_FOUND(404),
    28. /**
    29. * 不可访问
    30. */
    31. NOT_ACCEPTABLE(406),
    32. /**
    33. * 冲突
    34. */
    35. CONFLICT(409),
    36. /**
    37. * 服务器发生异常
    38. */
    39. ERROR(500);
    40. private final Integer value;
    41. ResponseCode(Integer value) {
    42. this.value = value;
    43. }
    44. public Integer getValue() {
    45. return value;
    46. }
    47. }

    web响应实体类

    1. package cn.edu.sgu.www.pager.restful;
    2. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    3. import lombok.Data;
    4. import java.io.Serializable;
    5. /**
    6. * @author heyunlin
    7. * @version 1.0
    8. */
    9. @Data
    10. public class JsonResult implements Serializable {
    11. private static final long serialVersionUID = 18L;
    12. /**
    13. * 响应数据
    14. */
    15. private T data;
    16. /**
    17. * 响应状态码
    18. */
    19. private Integer code;
    20. /**
    21. * 响应提示信息
    22. */
    23. private String message;
    24. /**
    25. * 请求是否成功
    26. */
    27. private boolean success;
    28. /**
    29. * 成功提示
    30. */
    31. private static final String successMessage = "请求成功";
    32. public static JsonResult success(String message, T data) {
    33. JsonResult jsonResult = new JsonResult<>();
    34. jsonResult.setCode(ResponseCode.OK.getValue());
    35. jsonResult.setMessage(message);
    36. jsonResult.setSuccess(true);
    37. jsonResult.setData(data);
    38. return jsonResult;
    39. }
    40. public static JsonResult> restPage(Page page) {
    41. JsonPage jsonPage = JsonPage.restPage(page);
    42. return success(successMessage, jsonPage);
    43. }
    44. }

    分页查询结果

    1. package cn.edu.sgu.www.pager.restful;
    2. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    3. import lombok.Data;
    4. import java.io.Serializable;
    5. import java.util.List;
    6. /**
    7. * @author heyunlin
    8. * @version 1.0
    9. */
    10. @Data
    11. public class JsonPage implements Serializable {
    12. private static final long serialVersionUID = 18L;
    13. /**
    14. * 总记录数
    15. */
    16. private Long total;
    17. /**
    18. * 查询结果
    19. */
    20. private List rows;
    21. /**
    22. * 根据Page对象构建JsonPage对象
    23. * @param page Page
    24. * @return JsonPage
    25. */
    26. public static JsonPage restPage(Page page) {
    27. JsonPage jsonPage = new JsonPage<>();
    28. jsonPage.setTotal(page.getTotal());
    29. jsonPage.setRows(page.getRecords());
    30. return jsonPage;
    31. }
    32. }

     

    分页参数的对象

    1. package cn.edu.sgu.www.pager.restful;
    2. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    3. import lombok.Data;
    4. /**
    5. * @author heyunlin
    6. * @version 1.0
    7. */
    8. @Data
    9. public class Pager {
    10. /**
    11. * 页数
    12. */
    13. private Integer page;
    14. /**
    15. * 每页的记录数
    16. */
    17. private Integer rows;
    18. /**
    19. * 根据Pager创建Page对象
    20. * @param pager Pager
    21. * @return Page
    22. */
    23. public static Page ofPage(Pager pager) {
    24. return new Page(pager.getPage(), pager.getRows());
    25. }
    26. }

    创建数据库实体类

    1. package cn.edu.sgu.www.pager.entity;
    2. import com.fasterxml.jackson.annotation.JsonFormat;
    3. import lombok.Data;
    4. import java.io.Serializable;
    5. import java.time.LocalDateTime;
    6. /**
    7. * @author heyunlin
    8. * @version 1.0
    9. */
    10. @Data
    11. public class Song implements Serializable {
    12. private static final long serialVersionUID = 18L;
    13. private String id;
    14. /**
    15. * 歌曲名
    16. */
    17. private String name;
    18. /**
    19. * 歌手
    20. */
    21. private String singer;
    22. /**
    23. * 描述信息
    24. */
    25. private String note;
    26. /**
    27. * 最后一次修改时间
    28. */
    29. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    30. private LocalDateTime lastUpdateTime;
    31. }

    创建持久层接口

    继承mybatis-plus的BaseMapper接口

    1. package cn.edu.sgu.www.pager.mapper;
    2. import cn.edu.sgu.www.pager.entity.Song;
    3. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    4. import org.apache.ibatis.annotations.Mapper;
    5. /**
    6. * @author heyunlin
    7. * @version 1.0
    8. */
    9. @Mapper
    10. public interface SongMapper extends BaseMapper {
    11. }

    创建业务层接口

    SongService

    1. package cn.edu.sgu.www.pager.service;
    2. import cn.edu.sgu.www.pager.entity.Song;
    3. import cn.edu.sgu.www.pager.restful.Pager;
    4. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    5. /**
    6. * @author heyunlin
    7. * @version 1.0
    8. */
    9. public interface SongService {
    10. /**
    11. * 分页查询歌曲列表
    12. * @param pager 分页参数
    13. * @return Page
    14. */
    15. Page selectByPage(Pager pager);
    16. }

    SongServiceImpl

    1. package cn.edu.sgu.www.pager.service.impl;
    2. import cn.edu.sgu.www.pager.entity.Song;
    3. import cn.edu.sgu.www.pager.mapper.SongMapper;
    4. import cn.edu.sgu.www.pager.restful.Pager;
    5. import cn.edu.sgu.www.pager.service.SongService;
    6. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    7. import org.springframework.beans.factory.annotation.Autowired;
    8. import org.springframework.stereotype.Service;
    9. /**
    10. * @author heyunlin
    11. * @version 1.0
    12. */
    13. @Service
    14. public class SongServiceImpl implements SongService {
    15. private final SongMapper songMapper;
    16. @Autowired
    17. public SongServiceImpl(SongMapper songMapper) {
    18. this.songMapper = songMapper;
    19. }
    20. @Override
    21. public Page selectByPage(Pager pager) {
    22. Page page = Pager.ofPage(pager);
    23. return songMapper.selectPage(page, null);
    24. }
    25. }

    创建控制器类

    1. package cn.edu.sgu.www.pager.controller;
    2. import cn.edu.sgu.www.pager.entity.Song;
    3. import cn.edu.sgu.www.pager.restful.JsonPage;
    4. import cn.edu.sgu.www.pager.restful.JsonResult;
    5. import cn.edu.sgu.www.pager.restful.Pager;
    6. import cn.edu.sgu.www.pager.service.SongService;
    7. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    8. import org.springframework.beans.factory.annotation.Autowired;
    9. import org.springframework.web.bind.annotation.GetMapping;
    10. import org.springframework.web.bind.annotation.RequestMapping;
    11. import org.springframework.web.bind.annotation.RestController;
    12. /**
    13. * @author heyunlin
    14. * @version 1.0
    15. */
    16. @RestController
    17. @RequestMapping(path = "/song", produces = "application/json;charset=utf-8")
    18. public class SongController {
    19. private final SongService songService;
    20. @Autowired
    21. public SongController(SongService songService) {
    22. this.songService = songService;
    23. }
    24. @GetMapping("/selectByPage")
    25. public JsonResult> selectByPage(Pager pager) {
    26. Page page = songService.selectByPage(pager);
    27. return JsonResult.restPage(page);
    28. }
    29. }

    创建前端页面

    song_list.html

    在页面创建一个带标题的表格,然后在表格下方创建一个简单的分页组件~

    可以调整每页显示几条数据,上一页、下一页、指定页数的跳转。

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>权限管理页面title>
    6. <script src="/js/jquery.min.js">script>
    7. <script src="/js/ajaxUtils.js">script>
    8. <script src="/js/song_list.js">script>
    9. <style>
    10. .table {
    11. height: 635px;
    12. overflow-y: scroll;
    13. border: 1px black solid;
    14. }
    15. #pageNum {
    16. width: 50px;
    17. }
    18. style>
    19. head>
    20. <body>
    21. <div class="table">
    22. <table border="1">
    23. <thead>歌曲列表thead>
    24. <tbody id="song_list">tbody>
    25. table>
    26. div>
    27. <div>
    28. 每页展示<select id="rows">select>条记录
    29. <button type="button" id="pre">上一页button>
    30. 当前第<span id="page">1span>页
    31. <button type="button" id="next">下一页button>
    32. 前往第<input type="number" id="pageNum" />
    33. div>
    34. body>
    35. html>

    song_list.js

    1. /**
    2. * 总页数
    3. */
    4. let total;
    5. /**
    6. * 当前页
    7. */
    8. let page = 1;
    9. /**
    10. * 每页显示的记录数
    11. */
    12. let rows = 15;
    13. /**
    14. * 页码
    15. */
    16. let pageList = [15, 50 ,100, 200, 500];
    17. /**
    18. * 加载表格数据
    19. */
    20. function loadData() {
    21. /**
    22. * 表头
    23. */
    24. let thead = "";
    25. /**
    26. * 表格体
    27. */
    28. let tbody = "";
    29. thead += "";
    30. thead += "歌曲ID";
    31. thead += "歌曲名";
    32. thead += "歌手";
    33. thead += "歌曲信息";
    34. thead += "最后一次修改时间";
    35. thead += "";
    36. ajaxGet("/song/selectByPage", {
    37. page: page,
    38. rows: rows
    39. }, function (resp) {
    40. let data = resp.data;
    41. let list = data.rows;
    42. total = data.total;
    43. for (let i = 0; i < list.length; i++) {
    44. let data = list[i];
    45. tbody += "";
    46. tbody += "" + data.id + "";
    47. tbody += "" + data.name + "";
    48. tbody += "" + data.singer + "";
    49. tbody += "" + data.note + "";
    50. tbody += "" + data.lastUpdateTime + "";
    51. tbody += "";
    52. }
    53. $("#song_list").empty().append(thead + tbody);
    54. }, error);
    55. }
    56. /**
    57. * 渲染分页组件
    58. */
    59. function fetchRows() {
    60. let options = "";
    61. for (let i = 0; i < pageList.length; i++) {
    62. let page = pageList[i];
    63. options += " + page + "";
    64. }
    65. $("#rows").append(options);
    66. }
    67. $(document).ready(function () {
    68. // 加载表格数据
    69. loadData();
    70. // 渲染分页组件
    71. fetchRows();
    72. /**
    73. * 绑定下拉框改变事件
    74. */
    75. $("#rows").change(function () {
    76. // 设置每页记录数为下拉框的值
    77. rows = $(this).val();
    78. loadData();
    79. });
    80. /**
    81. * 上一页
    82. */
    83. $("#pre").on("click", function () {
    84. if (page > 1) {
    85. page--;
    86. // 重新加载表格数据
    87. loadData();
    88. // 设置当前是第几页
    89. $("#page").html(page);
    90. }
    91. });
    92. /**
    93. * 下一页
    94. */
    95. $("#next").on("click", function () {
    96. let maxPage = (total % rows) > 0 ? (total / rows + 1) : (total / rows);
    97. if (page <= maxPage - 1) {
    98. page++;
    99. // 重新加载表格数据
    100. loadData();
    101. // 设置当前是第几页
    102. $("#page").html(page);
    103. }
    104. });
    105. /**
    106. * 绑定键盘事件
    107. */
    108. $("#pageNum").keydown(function () {
    109. let event = window.event;
    110. // 不允许输入空格
    111. if(event.keyCode === 32) {
    112. event.returnValue = false;
    113. } else if(event.keyCode === 13) {
    114. // 获取最大页数
    115. let maxPage = total % rows > 0 ? (total / rows + 1) : (total / rows);
    116. // 获取输入框内容
    117. let pageNum = parseInt($(this).val());
    118. // 修改页数
    119. page = pageNum > maxPage ? maxPage : pageNum;
    120. // 加载表格
    121. loadData();
    122. // 设置当前输入框的值
    123. $(this).val(page);
    124. // 设置当前在第几页
    125. $("#page").html(page);
    126. }
    127. });
    128. });

    好了,文章就分享到这里了,需要代码的可以从git下载:

    jquey+mybatis-plus实现简单分页功能icon-default.png?t=N7T8https://gitee.com/muyu-chengfeng/pager.git

  • 相关阅读:
    在一次又一次的失败中, 我总结了这份万字的《MySQL 性能调优笔记》
    一文概括AxureRP的优缺点和替代软件
    【华为OD机试】分苹果【2023 B卷|100分】
    牛血清白蛋白/人血清白蛋白/卵清白蛋白/小鼠血清白蛋白纳米粒改性Angiopep血管肽(2022已更新)
    # SpringBoot 集成 Netty
    数据结构(十一) -- 树(三) -- 堆排序
    Spring整合RabbitMQ
    敏感词检测库ToolGood.Words中IllegalWordsSearch类使用简介
    PyTorch笔记 - LSTM(Long Short-Term Memory) 和 LSTMP(Projection)
    应用方案 | 内置ALC的音频前置放大器D2538A和D3308芯片
  • 原文地址:https://blog.csdn.net/heyl163_/article/details/139773438