Springboot+PageHelper实现分页

 

1.工程搭建

  • 导入依赖
 <!--jdbc-->
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-jdbc</artifactId>
 </dependency>
 <!--加载springboot-web-->
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 <!--数据库-->
 <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <version>5.1.38</version>
 </dependency>
 <!-- 通用mapper -->
 <dependency>
     <groupId>tk.mybatis</groupId>
     <artifactId>mapper-spring-boot-starter</artifactId>
     <version>2.1.5</version>
 </dependency>
<!--thymeleaf-->
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-thymeleaf</artifactId>
 </dependency>
 <!-- druid连接池 -->
 <dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>druid</artifactId>
     <version>1.1.10</version>
 </dependency>
 <!-- 分页插件 -->
 <dependency>
 <groupId>com.github.pagehelper</groupId>
 <artifactId>pagehelper-spring-boot-starter</artifactId>
 <version>1.2.10</version>
  </dependency>
  <!--热部署-->
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-devtools</artifactId>
     <scope>runtime</scope>
     <optional>true</optional>
 </dependency>
 <!--属性注入bean-->
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-configuration-processor</artifactId>
     <optional>true</optional>
 </dependency>
 <!--lombok-->
 <dependency>
     <groupId>org.projectlombok</groupId>
     <artifactId>lombok</artifactId>
     <optional>true</optional>
 </dependency>

yml配置

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/demo
    username: root
    password: 1234
  thymeleaf:
    cache: false  #关闭缓存
#mybatis配置
mybatis:
#输出sql
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: mapper/*.xml #扫描mapper.xml文件
  type-aliases-package: com.whpu.pojo #javabean别名包扫描
pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  params: count=countSql

2. 业务代码

public interface UserMapperT extends Mapper<UserT> {
    //继承通用mapper
    //此处不需要任何方法
}

 user类

@Data
@Table(name="user") //指向数据库表名
public class UserT {
  @Id
  @KeySql(useGeneratedKeys = true)//表示在insert语句中,可将自动生成的主键id返回
  @GeneratedValue
    private Integer id;
    private String username;
    private String password;
}

 3.测试代码

@SpringBootTest
class Springboot06ApplicationTests {
@Autowired
private UserMapperT userMapperT;
@Test
void test2() {
//执行分页,pageNum: 当前页 , pageSize :每页显示数据条数
PageHelper.startPage(1, 5);
List<UserT> user=userMapperT.selectAll();
//查询结果封装到pageinfo对象
PageInfo<UserT> pageInfo =new PageInfo<>(user);
System.out.println(pageInfo);
}
}
  •  输出结果

 

 4.后台代码

@Autowired
private UserMapperT userMapperT;
@RequestMapping("lookuser")
public ModelAndView lookuser(ModelAndView mv,
@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
    //分页
    PageHelper.startPage(pageNum,5);
    //查询
    List<UserT> user=userMapperT.selectAll();
    //查询结果封装到pageinfo对象
    PageInfo<UserT> pageInfo =new PageInfo<>(user);
    mv.addObject("user",pageInfo);
    mv.setViewName("/select");
     return mv;
    }

 其中: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是否有下一页

 5.前端代码

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/amazeui/assets/css/amazeui.css}">
    <script th:src="@{/amazeui/assets/js/jquery.min.js}"></script>
    <script  th:src="@{/amazeui/assets/js/amazeui.js}"></script>
    <script th:src="@{/amazeui/pagination/amazeui-pagination.js}"></script>
</head>
<body>

<div class="am-g">
<div class="am-u-sm-5">

    <table class="am-table am-table-bordered  am-table-striped  am-table-centered am-table-hover am-table-compact">
        <h3 style="margin-bottom: 0px;margin-top: 30px">用户信息</h3>
        <tr>
            <td colspan ="4" >
                <a class="am-btn am-btn-primary" style="float: right " th:href="@{/addtk(flag=1)}">添加</a>
            </td>
        </tr>
        <tr>
            <th>编号</th>
            <th>账号</th>
            <th>密码</th>
            <th>操作</th>
        </tr>
        <!-- 遍历结果集list-->
       <tr th:each="u:${user.list}" >
            <td th:text="${u.id}"></td>
            <td th:text="${u.username}"></td>
            <td th:text="${u.password}"></td>
            <td ><a class="am-btn am-btn-success" th:href="@{/updatetk(flag=1,id=${u.id})}">修改</a><a class="am-btn am-btn-danger" th:href="@{/deletetk(id=${u.id})}">删除</a></td>
        </tr>
    </table>
    <p>当前<span th:text="${user.pageNum}"></span>页,总<span th:text="${user.pages}"> </span>页,共<span th:text="${user.total}"></span>条数据</p>
    <a th:href="@{/lookuser}">首页</a>
    <a th:href="@{/lookuser(pageNum=${user.hasPreviousPage}?${user.prePage}:1)}">上一页</a>
    <a th:href="@{/lookuser(pageNum=${user.hasNextPage}?${user.nextPage}:${user.pages})}">下一页</a>
    <a th:href="@{/lookuser(pageNum=${user.pages})}">尾页</a>
</div>
</div>
</body>
</html>
  •  结果

 

 

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Spring Boot和Layui实现分页,可以按照以下步骤进行操作: 1. 添加依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.13</version> </dependency> ``` 这个依赖是用来实现分页的,它是基于MyBatis的分页插件,可以帮助我们快速实现分页功能。 2. 编写Controller 在Controller中定义一个方法,用于查询数据并返回分页结果。可以使用PageHelper.startPage()方法来开启分页功能,并将查询结果包装成PageInfo对象返回。例如: ```java @GetMapping("/list") public PageInfo<User> list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) { PageHelper.startPage(pageNum, pageSize); List<User> userList = userService.list(); return new PageInfo<>(userList); } ``` 3. 编写前端页面 使用Layui的表格组件来展示分页数据。在html文件中引入Layui的相关文件,并创建一个表格: ```html <table class="layui-table" lay-data="{url: '/user/list', page:true, limit:10}"> <thead> <tr> <th>ID</th> <th>姓名</th> <th>年龄</th> <th>性别</th> </tr> </thead> <tbody> <tr> <td>{{d.id}}</td> <td>{{d.name}}</td> <td>{{d.age}}</td> <td>{{d.gender}}</td> </tr> </tbody> </table> ``` 这里使用了Layui的数据表格组件,通过设置url属性来指定数据接口的地址,page属性来开启分页功能,limit属性来设置每页显示的数据量。在表格中使用{{d.xxx}}的形式来渲染数据。 4. 启动应用程序 现在可以启动Spring Boot应用程序并访问前端页面,就能看到分页数据了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值