当前位置:   article > 正文

分页pageHelper_pagehelper需要 main allow

pagehelper需要 main allow

1.application.properties

  1. # 解决升级springboot2.6 因循环引用导致启动时报错
  2. spring.main.allow-circular-references=true
  3. # pagehelper
  4. #如果不配置会自动检测
  5. pagehelper.helperDialect=mysql
  6. #分页参数合理化
  7. pagehelper.reasonable=true
  8. #支持通过mapper接口参数来传递分页参数
  9. pagehelper.supportMethodsArguments=true
  10. #用于从对象属性名中取值
  11. pagehelper.params=count=countSql

2.pom.xml

  1. <dependency>
  2. <groupId>com.github.pagehelper</groupId>
  3. <artifactId>pagehelper-spring-boot-starter</artifactId>
  4. <version>1.2.5</version>
  5. <exclusions>
  6. <exclusion>
  7. <groupId>org.mybatis</groupId>
  8. <artifactId>mybatis</artifactId>
  9. </exclusion>
  10. <exclusion>
  11. <groupId>mybatis-spring</groupId>
  12. <artifactId>org.mybatis</artifactId>
  13. </exclusion>
  14. </exclusions>
  15. </dependency>

3.测试

  1. @Test
  2. public void test2(){
  3. PageHelper.startPage(5,2);
  4. List<Book> bookList=bookMapper.selectList();
  5. PageInfo pageInfo=new PageInfo(bookList);
  6. System.out.println("总记录数:"+pageInfo.getTotal());
  7. System.out.println("总页数:"+pageInfo.getPages());
  8. System.out.println("一页的大小:"+pageInfo.getSize());
  9. System.out.println(pageInfo);
  10. }

4.问题解决

报错原因:

依赖循环引用是不鼓励的,默认情况下是禁止的。更新应用程序以删除bean之间的依赖循环。作为最后的手段,可以通过设置spring.main来自动打破循环。allow-circular-references为true。

在application.properties文件中添加以下代码:

# 解决升级springboot2.6 因循环引用导致启动时报错

spring.main.allow-circular-references=true

  1. # 解决升级springboot2.6 因循环引用导致启动时报错
  2. spring.main.allow-circular-references=true
  1. package com.sunjob.zyx2.entity;
  2. import lombok.Data;
  3. @Data
  4. public class BaseDTO {
  5. // 当前查询第几页,从一开始
  6. private Integer pageNow;
  7. // 每页查询多少条
  8. private Integer pageSize;
  9. }
  1. package com.sunjob.zyx2.entity;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. import java.io.Serializable;
  6. @Data
  7. @NoArgsConstructor
  8. @AllArgsConstructor
  9. public class Book extends BaseDTO implements Serializable{
  10. private Integer bid;
  11. private String name;
  12. private String author;
  13. private Long price;
  14. private String sales;
  15. private String stock;
  16. private String image;
  17. private String descc;
  18. }
  1. package com.sunjob.zyx2.entity;
  2. import io.swagger.models.auth.In;
  3. import lombok.Data;
  4. import java.util.List;
  5. @Data
  6. public class TableResult<T> {
  7. // 查询聊天记录
  8. private List<T> rows;
  9. // 总条数
  10. private Long totalCount;
  11. // 总的页数
  12. private Long pageCount;
  13. // 上一页
  14. private Long prepageNum;
  15. // 下一页
  16. private Long nextpageNum;
  17. // 当前页
  18. // private Long pageNum;
  19. // 设置总的页数
  20. public void setPageCountByHelper(Long pageCount){
  21. this.pageCount = pageCount;
  22. }
  23. // 设置上一页
  24. public void setprepageNum(Long pageNum){
  25. if (pageNum==1){
  26. this.prepageNum = 1L;
  27. }else {
  28. this.prepageNum = pageNum-1;
  29. }
  30. }
  31. // 设置下一页
  32. public void setnextpageNum(Long pageNum){
  33. this.nextpageNum = pageNum+1;
  34. }
  35. }
  1. package com.sunjob.zyx2.service;
  2. import com.github.pagehelper.PageInfo;
  3. import com.sunjob.zyx2.entity.TableResult;
  4. import com.sunjob.zyx2.entity.Book;
  5. import java.util.List;
  6. public interface BookService {
  7. // 分页第一步
  8. TableResult<Book> listByPageViaPageHelper(Book book);
  9. }
  1. @Service
  2. public class BookServiceImpl implements BookService {
  3. @Autowired
  4. private BookMapper bookMapper;
  5. @Override
  6. public TableResult<Book> listByPageViaPageHelper(Book book) {
  7. // 传入参数
  8. PageHelper.startPage(book.getPageNow(),book.getPageSize());
  9. TableResult<Book> tableResult = new TableResult<>();
  10. // 总数据
  11. List<Book> books = bookMapper.selectListByPageViaPageHelper(book);
  12. // 不需要强制类型转换
  13. PageInfo<Book> bookPageInfo=new PageInfo<>(books);
  14. tableResult.setRows(bookPageInfo.getList());
  15. tableResult.setTotalCount(bookPageInfo.getTotal());
  16. tableResult.setPageCountByHelper((long) bookPageInfo.getPages());
  17. tableResult.setprepageNum((long) bookPageInfo.getPageNum());
  18. tableResult.setnextpageNum((long) bookPageInfo.getPageNum());
  19. // 需要强制类型转换
  20. // Page<Book> page=(Page<Book>) books;
  21. // 总条数
  22. // Long totalCount = bookMapper.selectCount(book);
  23. // tableResult.setRows(books);
  24. // tableResult.setTotalCount(page.getTotal());
  25. // tableResult.setPageCountByHelper(page.getPages());
  26. return tableResult;
  27. }
  28. }
  1. @RestController
  2. @RequestMapping("/book")
  3. public class BookController {
  4. @Autowired
  5. private BookService bookService;
  6. @GetMapping("/list") //等价于@RequestMapping(method = RequestMethod.GET)
  7. //@RequestParam是将请求参数绑定到控制器的方法参数上
  8. //@RequestParam(defaultValue="1") int pageNum, @RequestParam(defaultValue="3") int pageSize
  9. public Object listPages(@RequestParam(defaultValue="1") int pageNum) {
  10. if (pageNum <= 0) {
  11. pageNum = 1;
  12. }
  13. // if (pageSize == null) {
  14. // pageSize = 8; // 设置默认每页显示的数据数
  15. // }
  16. Book book=new Book();
  17. Integer pageSize=3;
  18. book.setPageNow(pageNum);
  19. book.setPageSize(pageSize);
  20. TableResult<Book> tableResult=bookService.listByPageViaPageHelper(book);
  21. System.out.println(tableResult);
  22. System.out.println(tableResult.getRows());
  23. System.out.println(tableResult.getRows().size());
  24. ModelAndView mv=new ModelAndView("add");
  25. mv.addObject("page", tableResult.getRows());
  26. mv.addObject("pagess", tableResult);
  27. System.out.println(mv);
  28. return mv;
  29. }
  30. }
  1. @Mapper
  2. @Repository
  3. public interface BookMapper{
  4. // 分页第二步
  5. List<Book> selectListByPageViaPageHelper(Book book);
  6. }
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.sunjob.zyx2.mapper.BookMapper">
  6. <sql id="whereComn">
  7. <where>
  8. <if test=' name !=null and name!="" '>
  9. name like concat('%',#{name},'%')
  10. </if>
  11. </where>
  12. </sql>
  13. <!-- 第三步-->
  14. <select id="selectListByPageViaPageHelper" resultType="com.sunjob.zyx2.entity.Book">
  15. select * from book
  16. <include refid="whereComn"/>
  17. order by bid asc
  18. </select>
  19. </mapper>
  1. <!--<%@ page language="java" contentType="text/html; charset=UTF-8"-->
  2. <!--pageEncoding="UTF-8" %>-->
  3. <!--<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>-->
  4. <!--文件头开始-->
  5. <!DOCTYPE>
  6. <!--<html xmlns:c="http://mybatis.org/schema/mybatis-mapper" xmlns:th="http://www.w3.org/1999/xhtml">-->
  7. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  8. <meta charset="UTF-8">
  9. <title>Title</title>
  10. <head>
  11. <meta http-equiv="Content-Language" content="zh-cn">
  12. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  13. <meta name="description" content="顾客信息表">
  14. <title>所有顾客信息表</title>
  15. <LINK href="../../css/main.css" rel=stylesheet>
  16. <link rel="stylesheet" type="text/css" href="../../css/test3.css">
  17. <script language="JavaScript" src="../../js/main.js"></script>
  18. <script src="../../js/jquery-1.12.3.min.js"></script>
  19. <!-- <span th:text="${page}"/>-->
  20. </head>
  21. <body>
  22. <div class="">
  23. <table width="100%" border="0" cellspacing="0" cellpadding="0">
  24. <tr>
  25. <table width="100" border="0" cellspacing="0" cellpadding="0" align="center">
  26. </table>
  27. </td>
  28. </tr>
  29. </table>
  30. <table cellspacing="1" cellpadding="3" align="center" border="0" width="98%">
  31. </table>
  32. <!--文件体开始-->
  33. <br>
  34. <br>
  35. <table cellspacing=1 cellpadding=3 align=center class=tableBorder2>
  36. <tr>
  37. <td height=25 valign=middle bgcolor="#E4F3FF" align="center">
  38. <a href="AFirstLog">返回首页</a>
  39. </td>
  40. <td height=25 valign=middle bgcolor="#E4F3FF" align="center">
  41. <b>顾客信息如下!</b>
  42. </td>
  43. </tr>
  44. </table>
  45. <br>
  46. <form method="get" name="reg" action="">
  47. <table cellpadding="3" cellspacing="1" align="center" class="tableborder3" id="table1">
  48. <tr>
  49. <td valign="middle" colspan="2" align="center" height="25" color="#9999FF">
  50. <font><b></b></font></td>
  51. </tr>
  52. <tr>
  53. <td width="10%" class="tablebody1" align="center"><b>客户编号</b>
  54. </td>
  55. <td width="10%" class="tablebody1" align="center"><b>用户名</b><br>
  56. </td>
  57. <td width="10%" class="tablebody1" align="center"><b>密码</b>
  58. </td>
  59. <td width="10%" class="tablebody1" align="center"><b>姓名 </b>
  60. </td>
  61. <td width="10%" class="tablebody1" align="center"><b>性别 </b>
  62. </td>
  63. <td width="10%" class="tablebody1" align="center"><b>年龄</b>
  64. </td>
  65. <td width="10%" class="tablebody1" align="center"><b>联系方式</b>
  66. </td>
  67. <td width="10%" class="tablebody1" align="center"><b>Email地址</b>
  68. </td>
  69. <td width="10%" class="tablebody1" align="center"><b>联系地址</b>
  70. </td>
  71. <td width="10%" class="tablebody1" align="center"><b>信息修改</b>
  72. </td>
  73. </tr>
  74. <!-- current展示当前的user对象-->
  75. <!-- index是索引属性,从0开始-->
  76. <!-- count是计数,下标从1开始-->
  77. <!-- first,last,odd,even均是返回boolean值,分别判断下标是否为第一个/最后一个/奇数/偶数-->
  78. <!-- size指的是当前userList的大小,返回的是同一个值-->
  79. <!-- 1.book指的是当前循环的对象的变量名称,可以随意定义,但要于下面 " . 属性"引用保持一致相当于增强for循环的临时变量;-->
  80. <!-- 2.bookStat指当前循环对象状态的变量(可选,默认就是你第一步设置的对象变量名称+ Stat)-->
  81. <!-- 3.${bookList }是当前循环的集合-->
  82. <!-- <c:forEach items="${page.rows}" var="book" begin="0" varStatus="stusts">-->
  83. <tbody th:each="book,itemStats : ${page}" >
  84. <tr>
  85. <td width="10%" class="tablebody1" align="center"><span th:text="${book.bid}"/>
  86. </td>
  87. <td width="10%" class="tablebody1" align="center"><span th:text="${book.name}"/>
  88. </td>
  89. <td width="10%" class="tablebody1" align="center"><span th:text="${book.author}"/>
  90. </td>
  91. <td width="10%" class="tablebody1" align="center"><span th:text="${book.stock}"/>
  92. </td>
  93. <td width="10%" class="tablebody1" align="center"><span th:text="${book.sales}"/>
  94. </td>
  95. <td width="10%" class="tablebody1" align="center"><span th:text="${book.price}"/>
  96. </td>
  97. <td width="10%" class="tablebody1" align="center"><span th:text="${book.descc}"/>
  98. </td>
  99. <td width="10%" class="tablebody1" align="center"><img th:src="${book.image}" style="width: 200px;height: 200px"/>
  100. </td>
  101. <td width="10%" class="tablebody1" align="center"><span th:text="${itemStats.count}"/>
  102. </td>
  103. <td width="15%" class="tablebody1" align="center">
  104. <a href="ACInf?account=${customer.account}">修改</a>
  105. </td>
  106. </tr>
  107. </tbody>
  108. </table>
  109. </form>
  110. <div style="text-align: center;font-size: 15px;" id="p">
  111. <!-- <a th:href="@{/book/list?pageNum=2&pageSize=3}">超链接</a>-->
  112. <a th:href="@{'/book'+'/list?'+'pageNum=1'}">第一页</a>
  113. <a th:href="@{'/book'+'/list?'+'pageNum='+${pagess.prepageNum}}">上一页</a>
  114. 当前:<span th:text="${page.pageNum}"/>
  115. <a th:href="@{'/book'+'/list?'+'pageNum='+${pagess.nextpageNum}}">下一页</a>
  116. <a th:href="@{'/book'+'/list?'+'pageNum='+${page.pages}}">尾页</a>
  117. </div>
  118. </div>
  119. </body>
  120. </html>
  121. <!--<span th:text="${page}"/>-->
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/262781
推荐阅读
相关标签
  

闽ICP备14008679号