经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MyBatis » 查看文章
MyBatis-Plus结合Layui实现分页方法
来源:jb51  时间:2021/8/5 11:21:31  对本文有异议

MyBatis-Plus 使用简单,内置通用 Mapper、通用 Service,仅仅通过少量配置,即可实现单表大部分 CRUD 操作。下面介绍使用 service 中的 page 方法结合 Layui 前端框架,较快速的实现分页效果。

在 pom.xml 中引入依赖

  1. <!-- mybatisplus -->
  2. <dependency>
  3. <groupId>com.baomidou</groupId>
  4. <artifactId>mybatis-plus-boot-starter</artifactId>
  5. <version>${mybatisplus.version}</version>
  6. </dependency>

使用 MyBatis-Plus 内置的 mapper。首先编写好实体类,然后编写 mapper 接口,并继承 BaseMapper。BaseMapper 中包含大部分的 CRUD 方法,不需要编写 mapper.xml 。如果需要多表查询的话,可根据自己的业务需要编写 mapper.xml 。

  1. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  2. import com.systop.pojo.School;
  3. import org.springframework.stereotype.Repository;
  4.  
  5. /**
  6. * @author: Miranda
  7. * @Date: 2021/8/2
  8. * @description:
  9. */
  10. @Repository
  11. public interface SchoolMapper extends BaseMapper<School> {
  12.  
  13. }

使用 MyBatis-Plus 内置的 service。编写 service 接口,并继承 IService。

  1. import com.baomidou.mybatisplus.extension.service.IService;
  2. import com.systop.pojo.School;
  3.  
  4. /**
  5. * @author: Miranda
  6. * @Date: 2021/8/2
  7. * @description:
  8. */
  9. public interface SchoolService extends IService<School> {
  10.  
  11. }

编写 service 实现类,继承 MyBatis-Plus 的 ServiceImpl ,同时实现 SchoolService 接口。

  1. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  2. import com.systop.mapper.SchoolMapper;
  3. import com.systop.pojo.School;
  4. import com.systop.service.SchoolService;
  5. import org.springframework.stereotype.Service;
  6.  
  7. /**
  8. * @author: Miranda
  9. * @Date: 2021/8/2
  10. * @description:
  11. */
  12. @Service
  13. public class SchoolServiceImpl extends ServiceImpl<SchoolMapper, School> implements SchoolService {
  14.  
  15. }

使用 MyBatis-plus 分页,必须写一个配置类

  1. import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5.  
  6. /**
  7. * @author: Miranda
  8. * @Date: 2021/8/3
  9. * @description:
  10. */
  11. @Configuration
  12. @MapperScan("com.systop.mapper")
  13. public class MybatisPlusConfig {
  14. /**
  15. * 分页插件
  16. */
  17. @Bean
  18. public PaginationInterceptor paginationInterceptor() {
  19. return new PaginationInterceptor();
  20. }
  21. }

需要一个 Layui 返回值的类

  1. import lombok.AllArgsConstructor;
  2. import lombok.Data;
  3. import lombok.NoArgsConstructor;
  4. import java.util.List;
  5.  
  6. /**
  7. * @author: Miranda
  8. * @Date: 2021/8/2
  9. * @description:
  10. */
  11. @Data
  12. @AllArgsConstructor
  13. @NoArgsConstructor
  14. public class LayuiPage<T> {
  15.  
  16. private int code;
  17. private String msg;
  18. private Long count;
  19. private List<T> data;
  20.  
  21. /**
  22. * 只有总条数和分页数据的构造方法
  23. * @param count 总条数
  24. * @param data 分页数据
  25. */
  26. public LayuiPage( Long count, List<T> data) {
  27. this.count = count;
  28. this.data = data;
  29. }
  30. }
  31.  

controller 类

  1. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  2. import com.baomidou.mybatisplus.core.metadata.IPage;
  3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  4. import com.systop.pojo.School;
  5. import com.systop.service.SchoolService;
  6. import com.systop.utils.LayuiPage;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Controller;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.ResponseBody;
  11.  
  12. /**
  13. * @author: Miranda
  14. * @Date: 2021/8/2
  15. * @description:
  16. */
  17. @Controller
  18. public class SchoolController {
  19.  
  20. @Autowired
  21. private SchoolService schoolService;
  22. @RequestMapping("schoolList")
  23. @ResponseBody
  24. public LayuiPage schoolList(int page,int limit){
  25. //传入分页的属性
  26. Page<School> pager = new Page<>(page,limit);
  27. //分页查询学校信息
  28. IPage<School> schoolPage = schoolService.page(pager, new QueryWrapper<>());
  29. // schoolPage.getTotal() 信息总条数
  30. // schoolPage.getRecords() 分页数据
  31. return new LayuiPage(schoolPage.getTotal(),schoolPage.getRecords());
  32. }
  33. }
  34.  

Layui 页面代码实现

  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>学校信息管理</title>
  6. <meta name="renderer" content="webkit">
  7. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  8. <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  9. <!-- 引入layuiadmin的样式 -->
  10. <link rel="stylesheet" href="../layuiadmin/layui/css/layui.css" rel="external nofollow" th:href="@{layuiadmin/layui/css/layui.css}" rel="external nofollow" media="all">
  11. <link rel="stylesheet" href="../layuiadmin/style/admin.css" rel="external nofollow" th:href="@{layuiadmin/style/admin.css}" rel="external nofollow" media="all">
  12. </head>
  13. <body>
  14. <div class="layui-fluid">
  15. <div class="layui-row layui-col-space15">
  16. <div class="layui-col-md12">
  17. <div class="layui-card">
  18. <div class="layui-card-body">
  19. <!-- id="test-table-simple" -->
  20. <table class="layui-table" id="test-table-simple" lay-filter="curd" ></table>
  21. </div>
  22. </div>
  23. </div>
  24. </div>
  25. </div>
  26. <script src="../layuiadmin/layui/layui.js" th:src="@{layuiadmin/layui/layui.js}"></script>
  27. <script>
  28. layui.use(['layer', 'table', 'element','form', 'layedit','util'], function(){
  29. var layer = layui.layer, //弹层
  30. table = layui.table, //表格
  31. element = layui.element, //元素操作
  32. form = layui.form,
  33. layedit = layui.layedit,
  34. util = layui.util;
  35. table.render({
  36. elem: '#test-table-simple',
  37. url: 'schoolList',
  38. method: 'post',
  39. cellMinWidth: 80, //全局定义常规单元格的最小宽度
  40. cols: [
  41. [{type: 'checkbox'},
  42. {field: 'sid', title: 'ID', sort: true, align: 'center', width:80},
  43. {field: 'sname', title: '名称', align: 'center'},
  44. {field: 'arrangement', title: '层次', align: 'center'},
  45. {title: '操作', align: 'center', toolbar: '#bar', width:150, fixed: 'right'}]
  46. ],
  47. // field 的值和实体类属性名称保持一致,如果数据表格没有渲染,可以看看浏览器解析后的名称
  48. done: function(res){
  49. // 在控制台输出后台传送的数据
  50. console.log(res);
  51. },
  52. page: true, //是否显示分页
  53. limits: [5, 7, 10],
  54. limit: 5 //每页默认显示的数量
  55. });
  56. });
  57. </script>
  58. </body>
  59. </html>

页面效果如下:

排雷:
刚开始定义 Layui 返回数据类的时候,将 code 定义成 Integer 类型,并且在 controller 类中使用的是两个参数的构造方法,导致传给前台数据中 code 的值是 null,所以数据渲染一直报 “返回的数据状态异常”。

解决:
将 code 定义成 int 类型,或者在 controller 中使用时,传四个参数。

到此这篇关于MyBatis-Plus结合Layui实现分页方法的文章就介绍到这了,更多相关MyBatis-Plus Layui分页内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号