赞
踩
1.application.properties
- # 解决升级springboot2.6 因循环引用导致启动时报错
- spring.main.allow-circular-references=true
-
- # pagehelper
- #如果不配置会自动检测
- pagehelper.helperDialect=mysql
- #分页参数合理化
- pagehelper.reasonable=true
- #支持通过mapper接口参数来传递分页参数
- pagehelper.supportMethodsArguments=true
- #用于从对象属性名中取值
- pagehelper.params=count=countSql
2.pom.xml
- <dependency>
- <groupId>com.github.pagehelper</groupId>
- <artifactId>pagehelper-spring-boot-starter</artifactId>
- <version>1.2.5</version>
- <exclusions>
- <exclusion>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- </exclusion>
- <exclusion>
- <groupId>mybatis-spring</groupId>
- <artifactId>org.mybatis</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
3.测试
- @Test
- public void test2(){
- PageHelper.startPage(5,2);
- List<Book> bookList=bookMapper.selectList();
- PageInfo pageInfo=new PageInfo(bookList);
- System.out.println("总记录数:"+pageInfo.getTotal());
- System.out.println("总页数:"+pageInfo.getPages());
- System.out.println("一页的大小:"+pageInfo.getSize());
- System.out.println(pageInfo);
- }
4.问题解决
报错原因:
依赖循环引用是不鼓励的,默认情况下是禁止的。更新应用程序以删除bean之间的依赖循环。作为最后的手段,可以通过设置spring.main来自动打破循环。allow-circular-references为true。
在application.properties文件中添加以下代码:
# 解决升级springboot2.6 因循环引用导致启动时报错
spring.main.allow-circular-references=true
- # 解决升级springboot2.6 因循环引用导致启动时报错
- spring.main.allow-circular-references=true
- package com.sunjob.zyx2.entity;
-
- import lombok.Data;
-
- @Data
- public class BaseDTO {
- // 当前查询第几页,从一开始
- private Integer pageNow;
- // 每页查询多少条
- private Integer pageSize;
- }
package com.sunjob.zyx2.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; @Data @NoArgsConstructor @AllArgsConstructor public class Book extends BaseDTO implements Serializable{ private Integer bid; private String name; private String author; private Long price; private String sales; private String stock; private String image; private String descc; }
package com.sunjob.zyx2.entity; import io.swagger.models.auth.In; import lombok.Data; import java.util.List; @Data public class TableResult<T> { // 查询聊天记录 private List<T> rows; // 总条数 private Long totalCount; // 总的页数 private Long pageCount; // 上一页 private Long prepageNum; // 下一页 private Long nextpageNum; // 当前页 // private Long pageNum; // 设置总的页数 public void setPageCountByHelper(Long pageCount){ this.pageCount = pageCount; } // 设置上一页 public void setprepageNum(Long pageNum){ if (pageNum==1){ this.prepageNum = 1L; }else { this.prepageNum = pageNum-1; } } // 设置下一页 public void setnextpageNum(Long pageNum){ this.nextpageNum = pageNum+1; } }
- package com.sunjob.zyx2.service;
-
- import com.github.pagehelper.PageInfo;
- import com.sunjob.zyx2.entity.TableResult;
- import com.sunjob.zyx2.entity.Book;
-
- import java.util.List;
- public interface BookService {
- // 分页第一步
- TableResult<Book> listByPageViaPageHelper(Book book);
- }
@Service public class BookServiceImpl implements BookService { @Autowired private BookMapper bookMapper; @Override public TableResult<Book> listByPageViaPageHelper(Book book) { // 传入参数 PageHelper.startPage(book.getPageNow(),book.getPageSize()); TableResult<Book> tableResult = new TableResult<>(); // 总数据 List<Book> books = bookMapper.selectListByPageViaPageHelper(book); // 不需要强制类型转换 PageInfo<Book> bookPageInfo=new PageInfo<>(books); tableResult.setRows(bookPageInfo.getList()); tableResult.setTotalCount(bookPageInfo.getTotal()); tableResult.setPageCountByHelper((long) bookPageInfo.getPages()); tableResult.setprepageNum((long) bookPageInfo.getPageNum()); tableResult.setnextpageNum((long) bookPageInfo.getPageNum()); // 需要强制类型转换 // Page<Book> page=(Page<Book>) books; // 总条数 // Long totalCount = bookMapper.selectCount(book); // tableResult.setRows(books); // tableResult.setTotalCount(page.getTotal()); // tableResult.setPageCountByHelper(page.getPages()); return tableResult; } }
@RestController @RequestMapping("/book") public class BookController { @Autowired private BookService bookService; @GetMapping("/list") //等价于@RequestMapping(method = RequestMethod.GET) //@RequestParam是将请求参数绑定到控制器的方法参数上 //@RequestParam(defaultValue="1") int pageNum, @RequestParam(defaultValue="3") int pageSize public Object listPages(@RequestParam(defaultValue="1") int pageNum) { if (pageNum <= 0) { pageNum = 1; } // if (pageSize == null) { // pageSize = 8; // 设置默认每页显示的数据数 // } Book book=new Book(); Integer pageSize=3; book.setPageNow(pageNum); book.setPageSize(pageSize); TableResult<Book> tableResult=bookService.listByPageViaPageHelper(book); System.out.println(tableResult); System.out.println(tableResult.getRows()); System.out.println(tableResult.getRows().size()); ModelAndView mv=new ModelAndView("add"); mv.addObject("page", tableResult.getRows()); mv.addObject("pagess", tableResult); System.out.println(mv); return mv; } }
- @Mapper
- @Repository
- public interface BookMapper{
- // 分页第二步
- List<Book> selectListByPageViaPageHelper(Book book);
- }
<?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.sunjob.zyx2.mapper.BookMapper"> <sql id="whereComn"> <where> <if test=' name !=null and name!="" '> name like concat('%',#{name},'%') </if> </where> </sql> <!-- 第三步--> <select id="selectListByPageViaPageHelper" resultType="com.sunjob.zyx2.entity.Book"> select * from book <include refid="whereComn"/> order by bid asc </select> </mapper>
<!--<%@ page language="java" contentType="text/html; charset=UTF-8"--> <!--pageEncoding="UTF-8" %>--> <!--<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>--> <!--文件头开始--> <!DOCTYPE> <!--<html xmlns:c="http://mybatis.org/schema/mybatis-mapper" xmlns:th="http://www.w3.org/1999/xhtml">--> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <meta charset="UTF-8"> <title>Title</title> <head> <meta http-equiv="Content-Language" content="zh-cn"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="description" content="顾客信息表"> <title>所有顾客信息表</title> <LINK href="../../css/main.css" rel=stylesheet> <link rel="stylesheet" type="text/css" href="../../css/test3.css"> <script language="JavaScript" src="../../js/main.js"></script> <script src="../../js/jquery-1.12.3.min.js"></script> <!-- <span th:text="${page}"/>--> </head> <body> <div class=""> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <table width="100" border="0" cellspacing="0" cellpadding="0" align="center"> </table> </td> </tr> </table> <table cellspacing="1" cellpadding="3" align="center" border="0" width="98%"> </table> <!--文件体开始--> <br> <br> <table cellspacing=1 cellpadding=3 align=center class=tableBorder2> <tr> <td height=25 valign=middle bgcolor="#E4F3FF" align="center"> <a href="AFirstLog">返回首页</a> </td> <td height=25 valign=middle bgcolor="#E4F3FF" align="center"> <b>顾客信息如下!</b> </td> </tr> </table> <br> <form method="get" name="reg" action=""> <table cellpadding="3" cellspacing="1" align="center" class="tableborder3" id="table1"> <tr> <td valign="middle" colspan="2" align="center" height="25" color="#9999FF"> <font><b></b></font></td> </tr> <tr> <td width="10%" class="tablebody1" align="center"><b>客户编号</b> </td> <td width="10%" class="tablebody1" align="center"><b>用户名</b><br> </td> <td width="10%" class="tablebody1" align="center"><b>密码</b> </td> <td width="10%" class="tablebody1" align="center"><b>姓名 </b> </td> <td width="10%" class="tablebody1" align="center"><b>性别 </b> </td> <td width="10%" class="tablebody1" align="center"><b>年龄</b> </td> <td width="10%" class="tablebody1" align="center"><b>联系方式</b> </td> <td width="10%" class="tablebody1" align="center"><b>Email地址</b> </td> <td width="10%" class="tablebody1" align="center"><b>联系地址</b> </td> <td width="10%" class="tablebody1" align="center"><b>信息修改</b> </td> </tr> <!-- current展示当前的user对象--> <!-- index是索引属性,从0开始--> <!-- count是计数,下标从1开始--> <!-- first,last,odd,even均是返回boolean值,分别判断下标是否为第一个/最后一个/奇数/偶数--> <!-- size指的是当前userList的大小,返回的是同一个值--> <!-- 1.book指的是当前循环的对象的变量名称,可以随意定义,但要于下面 " . 属性"引用保持一致相当于增强for循环的临时变量;--> <!-- 2.bookStat指当前循环对象状态的变量(可选,默认就是你第一步设置的对象变量名称+ Stat)--> <!-- 3.${bookList }是当前循环的集合--> <!-- <c:forEach items="${page.rows}" var="book" begin="0" varStatus="stusts">--> <tbody th:each="book,itemStats : ${page}" > <tr> <td width="10%" class="tablebody1" align="center"><span th:text="${book.bid}"/> </td> <td width="10%" class="tablebody1" align="center"><span th:text="${book.name}"/> </td> <td width="10%" class="tablebody1" align="center"><span th:text="${book.author}"/> </td> <td width="10%" class="tablebody1" align="center"><span th:text="${book.stock}"/> </td> <td width="10%" class="tablebody1" align="center"><span th:text="${book.sales}"/> </td> <td width="10%" class="tablebody1" align="center"><span th:text="${book.price}"/> </td> <td width="10%" class="tablebody1" align="center"><span th:text="${book.descc}"/> </td> <td width="10%" class="tablebody1" align="center"><img th:src="${book.image}" style="width: 200px;height: 200px"/> </td> <td width="10%" class="tablebody1" align="center"><span th:text="${itemStats.count}"/> </td> <td width="15%" class="tablebody1" align="center"> <a href="ACInf?account=${customer.account}">修改</a> </td> </tr> </tbody> </table> </form> <div style="text-align: center;font-size: 15px;" id="p"> <!-- <a th:href="@{/book/list?pageNum=2&pageSize=3}">超链接</a>--> <a th:href="@{'/book'+'/list?'+'pageNum=1'}">第一页</a> <a th:href="@{'/book'+'/list?'+'pageNum='+${pagess.prepageNum}}">上一页</a> 当前:<span th:text="${page.pageNum}"/> <a th:href="@{'/book'+'/list?'+'pageNum='+${pagess.nextpageNum}}">下一页</a> <a th:href="@{'/book'+'/list?'+'pageNum='+${page.pages}}">尾页</a> </div> </div> </body> </html> <!--<span th:text="${page}"/>-->
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。