SpringBoot分页功能的实现

1.1项目结构

在这里插入图片描述

1.2引入依赖

<dependencies>
    <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
     </dependency>

     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
         <exclusions>
             <exclusion>
                 <groupId>org.junit.vintage</groupId>
                 <artifactId>junit-vintage-engine</artifactId>
             </exclusion>
         </exclusions>
     </dependency>
     
     //mybatis-plus的starter
     
     <dependency>
         <groupId>com.baomidou</groupId>
         <artifactId>mybatis-plus-boot-starter</artifactId>
         <version>3.5.2</version>
     </dependency>
     
	//mysql的connector
	
     <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
     </dependency>
     
     <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
     </dependency>
     
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-thymeleaf</artifactId>
     </dependency>
     
 </dependencies>

2.1在Indexcontroller中构建 /table 请求并利用分页功能获取数据库中的数据

 @RequestMapping("/table")
 	//pn参数就是代表着第几页,默认为第一页
    public ModelAndView table(@RequestParam(value = "pn",defaultValue = "1")Integer pn){
        ModelAndView mv = new ModelAndView();
        Page<User> userPage = new Page<>(pn, 2); 
        Page<User> page = userService.page(userPage,null); //利用page方法进行分页
        mv.addObject("page",page);
        mv.setViewName("tables");
        return mv;
    }

2.2在Indexcontroller中构建 /deteleUser/{id} 通过地址传参删除对应id的用户

 @RequestMapping("/deteleUser/{id}")
    public String deteleUser(RedirectAttributes r,@PathVariable("id")Integer id,
    @RequestParam("pn")Integer pn){
        userService.removeById(id);
        r.addAttribute("pn",pn);
        return "redirect:/table";
    }

3.导入分页插件

创建一个MybatisConfig 类,注入MybatisPlusInterceptor的Bean
@Configuration
public class MybatisConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
    
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return mybatisPlusInterceptor;
    }
}

4.html中的thymeleaf代码编写

 <table class="table table-bordered">
    <thead>
       <tr>
           <th>id</th>
           <th>Name</th>
           <th>Age</th>
           <th>Email</th>
           <th>Detele</th>
       </tr>
       </thead>
       
       <tbody>
       //page.records就是page中的记录,也就是全部的user
       <tr th:each="user : ${page.records}">
           <td th:text="${user.id}">Mark</td>
           <td th:text="${user.name}">Mark</td>
           <td th:text="${user.age}">Mark</td>
           <td th:text="${user.email}">Mark</td>
           <td >
        // {id}(id=${user.id} 意为把user.id传给{id}拼接到地址中
        //如: deteleUser?id=2 此处是将用户所对应得id传入地址中并根据id删除用户
               <a th:href="@{/deteleUser/{id}(id=${user.id},pn=${page.current})}" class="btn btn-danger">
                   Detele
               </a>
           </td>
       </tr>

       </tbody>

   </table>

5.效果展示

当我们发送请求http://localhost:8080/table?pn=1 效果如下:

在这里插入图片描述

6.添加页码

1.在html中添加页码栏
<ul class="pagination pagination-lg">
   <li class="disabled"><a href="#">«</a></li>
     <li
     	 #page.current就是当前页意思
         th:class="${page.current==num?'active':''}"
         #numbers是thymeleaf的一个工具类,此处调用的sequence是表示从1展示到page的最后一页,
         th:each="num:${#numbers.sequence(1,page.pages)}">
         
         <a th:href="@{/table(pn=${num})}">[[${num}]]</a></li>
     <li><a href="#">»</a></li>
 </ul>

#告诉用户页面的信息,包括当前是第几页,共有几页,一共有多少条数据
 <ul>
   当前是第[[${page.current}]]  页,共[[${page.pages}]], 共有[[${page.total}]] 条数据
 </ul>
2.效果展示
此时就可以通过点击页码进行页面跳转了

在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值