Spring Boot入门,整合Pagehelper分页插件

1.环境准备

参考我的上两篇博客

(1)SpringBoot入门,快速搭建简单Web应用环境

(2)SpringBoot入门,整合Mybatis并使用Mybatis-Generator自动生成所需代码

2.在pom文件中引入Pagehelper分页插件


<!-- 分页插件 -->

<dependency>

    <groupId>com.github.pagehelper</groupId>

    <artifactId>pagehelper-spring-boot-starter</artifactId>

    <version>1.2.5</version>

</dependency>

3.配置分页插件

打开application.properties,添加如下几行配置信息

至此,pagehelper分页插件整合完毕,下面开始测试

4.测试

测试之前,我们先给上次创建的Person表增加几条数据

然后修改PersonController的代码


@Controller

public class PersonController {

    @Autowired

    private PersonService personService;

 

    @GetMapping("/getAllPerson")

    public String getAllPerson(Model model,@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){

        PageHelper.startPage(pageNum,5);

        List<Person> list = personService.getAllPerson();

        PageInfo<Person> pageInfo = new PageInfo<Person>(list);

        model.addAttribute("pageInfo",pageInfo);

        return "list";

    }

}
 

其中:PageHelper.startPage(int PageNum,int PageSize):用来设置页面的位置和展示的数据条目数,我们设置每页展示5条数据。PageInfo用来封装页面信息,返回给前台界面。PageInfo中的一些我们需要用到的参数如下表:

PageInfo.list
            结果集
        PageInfo.pageNum
            当前页码
        PageInfo.pageSize
            当前页面显示的数据条目
        PageInfo.pages
            总页数
        PageInfo.total
            数据的总条目数
        PageInfo.prePage
            上一页
        PageInfo.nextPage
            下一页
        PageInfo.isFirstPage
            是否为第一页
        PageInfo.isLastPage
            是否为最后一页
        PageInfo.hasPreviousPage
            是否有上一页
        PageHelper.hasNextPage
            是否有下一页

下面处理返回到list界面的数据信息,依然使用thymeleaf模版,代码如下


<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org">

<head>

    <meta charset="UTF-8">

    <title>Title</title>

</head>

<body>

<div align="center">

    <table border="1">

        <tr>

            <th>id</th>

            <th>name</th>

            <th>sex</th>

            <th>age</th>

        </tr>

        <tr th:each="person:${pageInfo.list}">

            <td th:text="${person.id}"></td>

            <td th:text="${person.name}"></td>

            <td th:text="${person.sex}"></td>

            <td th:text="${person.age}"></td>

        </tr>

    </table>

    <p>当前 <span th:text="${pageInfo.pageNum}"></span> 页,总 <span th:text="${pageInfo.pages}"></span> 页,共 <span th:text="${pageInfo.total}"></span> 条记录</p>

    <a th:href="@{/getAllPerson}">首页</a>

    <a th:href="@{/getAllPerson(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}">上一页</a>

    <a th:href="@{/getAllPerson(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}">下一页</a>

    <a th:href="@{/getAllPerson(pageNum=${pageInfo.pages})}">尾页</a>

</div>

</body>

</html>

打开浏览器查看效果

5.扩展

在springboot的项目中,如果我们修改了前台界面,会发现刷新界面后没有效果,因此需要重启项目,这样很麻烦,简单两步教你搞定它:

(1)在配置文件中给thymeleaf添加如下配置:

spring.thymeleaf.cache=false

2)在Intellij Idea按Ctrl+Shift+F9

之后在浏览器中刷新界面,即可显示出对页面的更改信息。

--------------------- 
作者:gaoye_takano 
来源:CSDN 
原文:https://blog.csdn.net/qq_27317475/article/details/81168241 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
spring boot整合mybatis-plus和pagehelper分页插件是一种常见的开发方式,可以实现数据库的分页查询功能。下面是一个简单示例项目的源码,以供参考: 首先,需要在pom.xml文件中添加相关依赖: ```xml <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- MyBatis Plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency> <!-- PageHelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <!-- MySQL Connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version> </dependency> </dependencies> ``` 在application.properties(或application.yml)文件中配置数据库连接信息: ```yaml spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver mybatis-plus.configuration.map-underscore-to-camel-case=true ``` 创建一个简单的实体类User: ```java public class User { private Long id; private String username; private String password; // 省略getter和setter方法 } ``` 创建一个Mapper接口UserMapper: ```java @Mapper public interface UserMapper extends BaseMapper<User> { // 省略其他方法 List<User> getUsersByPage(Page<User> page, @Param("username") String username); } ``` 创建一个Service接口UserService以及其实现类UserServiceImpl: ```java @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { @Autowired private UserMapper userMapper; @Override public IPage<User> getUsersByPage(Page<User> page, String username) { return userMapper.getUsersByPage(page, username); } } ``` 在Controller中注入UserService,并进行分页查询操作: ```java @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/users") public IPage<User> getUsers(@RequestParam(value = "page", defaultValue = "1") Integer pageNum, @RequestParam(value = "size", defaultValue = "10") Integer pageSize, @RequestParam(value = "username", required = false) String username) { Page<User> page = new Page<>(pageNum, pageSize); return userService.getUsersByPage(page, username); } } ``` 至此,就完成了spring boot整合mybatis-plus和pagehelper分页插件的配置和使用。 请注意,这是一个简单示例项目,实际使用中可能需要根据需求进行适当调整和修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值