SpringBoot整合pagehelper

南楼画角 提交于 2020-03-09 11:29:58

SpringBoot整合pagehelper

pagehelper相关知识了解

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 是否有下一页
navigatepageNums 所有导航页号(数组)

1、导入依赖

		<!-- Springboot -->
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!-- Springweb-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 分页插件pagehelper -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.13</version>
        </dependency>
		 <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
		<!-- druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.6</version>
        </dependency>
        <!-- mysql-connector -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.19</version>
        </dependency>
		<!-- mysql-connector -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.2</version>
        </dependency>
        <!--spring-boot-starter-jdbc -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

2、配置文件

#分页插件
pagehelper.helper-dialect=mysql
pagehelper.params=count=countSql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true

spring.datasource.url=jdbc:mysql://192.168.150.130:3306/pagehelper?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
/**
 * 数据库配置类
 */
@Configuration
public class DataSourceConfig {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }
}

3、Sql

CREATE TABLE `user` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(32) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8


insert into user(id, name) values('1','毕淑儒');
insert into user(id, name) values('2','蔡兴熙');
insert into user(id, name) values('3','曾三杰');
insert into user(id, name) values('4','常元琴');
insert into user(id, name) values('5','陈栋芬');
insert into user(id, name) values('6','陈宁婷');
insert into user(id, name) values('7','陈瑞');
insert into user(id, name) values('8','陈武宵');
insert into user(id, name) values('9','陈晓丽');
insert into user(id, name) values('10','陈翼涛');
insert into user(id, name) values('11','陈宇然');
insert into user(id, name) values('12','陈震');
insert into user(id, name) values('13','程太君');
insert into user(id, name) values('14','程玉娜');
insert into user(id, name) values('15','丛贺轩');
insert into user(id, name) values('16','戴国宇');
insert into user(id, name) values('17','戴杰亮');
insert into user(id, name) values('18','丁玉华');
insert into user(id, name) values('19','董秋明');
insert into user(id, name) values('20','董政厚');
insert into user(id, name) values('21','杜玉焱');

4、控制器

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/getAllUser")
    public String getAllUser(
             	@RequestParam(value = "pageNum",defaultValue = "1") 
                Integer pageNum,
                Model model
                ){
		
		//注意:一定要先分页,确定好每页显示的数据个数,再查询数据
        PageHelper.startPage(pageNum,5);
        List<User> userList = userService.getAllUser();
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        model.addAttribute("pageInfo",pageInfo);
        return "index";
    }
}

5、页面

<!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>
        </tr>
        <tr th:each="user:${pageInfo.list}">
            <td th:text="${user.id}"></td>
            <td th:text="${user.name}"></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="@{/getAllUser}">首页</a>
    <a th:href="@{/getAllUser(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}">上一页</a>
    <a th:href="@{/getAllUser(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}">下一页</a>
    <a th:href="@{/getAllUser(pageNum=${pageInfo.pages})}">尾页</a>
</div>
</body>
</html>

附加

Pagehelper整合bootstrap

遍历导航页,效果如同下图:

<nav aria-label="Page navigation" align="center">
    <ul class="pagination" >

        <li>
            <a th:href="@{/getAllUser(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}" aria-label="Previous">
                <span aria-hidden="true">&laquo;</span>
            </a>
        </li>

        <li th:class="${pageNums == pageInfo.pageNum}?'active':''" th:each="pageNums : ${pageInfo.navigatepageNums}">
            <a th:href="@{/getAllUser(pageNum=${pageNums})}" th:if="${pageNums != pageInfo.pageNum}" th:text="${pageNums}"></a>
            <span th:if="${pageNums == pageInfo.pageNum}" th:text="${pageNums}"></span>
        </li>

        <li>
            <a th:href="@{/getAllUser(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}" aria-label="Next">
                <span aria-hidden="true">&raquo;</span>
            </a>
        </li>
    </ul>
</nav>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!