springboot集成pagehelper实现分页

我们两清 提交于 2020-03-19 17:31:57

3 月,跳不动了?>>>

1.首先加入pagehelper的pom依赖

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

2.准备实体类User.java

public class User {
    private int id;
    private String username;
    private String password;
	省略getter setter 方法
}  

3.controller层代码

[@Controller](https://my.oschina.net/u/1774615)
public class UserController {

    @Autowired
    UserMapper userMapper;

    //第一种实现方式
    //http://localhost:8080/getUser?pageNum=1&pageSize=2
    @RequestMapping(value = {"/getUser","/"})
    public String getUser(Integer pageNum, Integer pageSize, Model model){
        if (pageNum==null&&pageSize==null){
            pageNum=1;
            pageSize=5;
        }
        if (pageNum!=null&&pageSize==null){
            pageSize=3;
        }
        PageHelper.startPage(pageNum,pageSize);
        Page<User> userList = userMapper.getUserList();
        model.addAttribute("userStat",userList);
        return "index";
    }
}

4.mapper类

@Mapper
public interface UserMapper {
    @Select("select * from user")
    Page<User> getUserList();
}

5.页面展示index.html

<body>
<h1>查询所有的用户。。。</h1>
<table class="table table-hover">
    <tr>
        <td class="active">编号</td>
        <td class="success">用户名</td>
        <td class="warning">密码</td>
    </tr>
    <tr th:each="user : ${userStat}">
        <td class="active" th:text="${user.id}">编号</td>
        <td class="success" th:text="${user.username}">用户名</td>
        <td class="warning" th:text="${user.password}" >密码</td>
    </tr>
</table>

<nav aria-label="Page navigation">
    <ul class="pagination">
        <li>
            <a href="#" aria-label="Previous">
                <span aria-hidden="true">&laquo;</span>
            </a>
        </li>
        <li><a href="#" th:href="@{/getUser?pageNum=1}">1</a></li>
        <li><a href="#" th:href="@{/getUser?pageNum=2}">2</a></li>
        <li><a href="#" th:href="@{/getUser?pageNum=3}">3</a></li>
        <li><a href="#" th:href="@{/getUser?pageNum=4}">4</a></li>
        <li><a href="#" th:href="@{/getUser?pageNum=5}">5</a></li>
        <li>
            <a href="#" aria-label="Next">
                <span aria-hidden="true">&raquo;</span>
            </a>
        </li>
    </ul>
</nav>
</body>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!