springboot ssm 整合 PageHelper

先声明本人也是小白一枚
单纯记录分享一下
如果哪里错 欢迎指正

整个项目结构
springboot ssm 整合 PageHelper_第1张图片
pom

<!-- pagehelper 分页插件 -->
 <dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.5</version>
</dependency>

application.properties

#分页配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=true 
pagehelper.supportMethodsArguments=true 
pagehelper.pageSizeZero=false #pageSize=0 

BookMapper 里头的查询方法

<select id="selectAllBookByPage" resultType="com.ldw.pojo.Book">
  select * from book
  </select>

dao(指的是上面mapper路径下)

List<Book> selectAllBookByPage();

service

List<Book> selectAllBookByPage();

serviceImpl

    @Autowired
    private BookMapper bookMapper;

    @Override
    public List<Book> selectAllBookByPage() {
        return bookMapper.selectAllBookByPage();
    }

controller

@RequestMapping("/bookController")
@Controller
public class BookController {
    @Autowired
    private BookService bookServiceImpl;

    @RequestMapping("/selectAllBookByPage")
    public  String selectAllBookByPage(@RequestParam(defaultValue = "1") int pageNum,
                                       @RequestParam(defaultValue = "10") int pageSize,
                                       HttpServletRequest request){
        PageHelper.startPage(pageNum,pageSize);
        PageInfo pageInfo = new PageInfo(bookServiceImpl.selectAllBookByPage());
        request.setAttribute("pageInfo",pageInfo);
        return "index";
    }
}

jsp

<%@ 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>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title></title>
</head>
<body>
<table>
    <tr>
        <td>id</td>
        <td>书名</td>
        <td>作者</td>
    </tr>
    <c:forEach items="${pageInfo.list}" var="book">
        <tr>
            <td>${book.id}</td>
            <td>${book.name}</td>
            <td>${book.auther}</td>
        </tr>
    </c:forEach>
</table>
<p>当前 ${pageInfo.pageNum },总${pageInfo.pages },总 ${pageInfo.total } 条记录
</p>
<a href="/page/bookController/selectAllBookByPage?pageNum=1">首页</a>

<c:if test="${pageInfo.pageNum>1 }">
    <a href="/page/bookController/selectAllBookByPage?pageNum=${pageInfo.pageNum-1}">上一页</a>
</c:if>

<c:if test="${pageInfo.pageNum>
    <a href="/page/bookController/selectAllBookByPage?pageNum=${pageInfo.pageNum+1}">下一页</a>
</c:if>

<a href="/page/bookController/selectAllBookByPage?pageNum=${pageInfo.pages }">末页</a>
</body>
</html>

实际效果
springboot ssm 整合 PageHelper_第2张图片
如果对你有帮助
可以给我留言!
不要让我觉得在单机ToT

你可能感兴趣的:(web,java)