MyBatis-Plus的分页功能

5 篇文章 0 订阅
2 篇文章 0 订阅

分页功能:就是将后端得到的数据通过分页的形式展示给前端(页面)。

此处使用:

  • 框架:SpringBoot2.6.6
  • 模板引擎:thymeleaf(SpringBoot版本仲裁)

1. 编写前端页面pagin.html(主要是编写一个表格来进行分页)

在这里插入图片描述

示例代码:

<table border="1px" width="500px" height="200px" >
        <thead>
        <tr>
            <th>id</th>
            <th>用户名</th>
            <th>age</th>
            <th>email</th>
        </tr>
        </thead>
        <tbody>
        <tr >
            <td>12</td>
            <td>毛哥</td>
            <td>18</td>
            <td>1457643551@qq.com</td>
        </tr>
        <tr >
            <td>12</td>
            <td>毛哥</td>
            <td>18</td>
            <td>1457643551@qq.com</td>
        </tr>
        <tr >
            <td>12</td>
            <td>毛哥</td>
            <td>18</td>
            <td>1457643551@qq.com</td>
        </tr>
        </tbody>
    </table>
    <div width="500px" >当前第 1 页 总计 10 页 共 10 条记录&nbsp;&nbsp;&nbsp;
        <span>首页</span>
        <span>1</span>
        <span>2</span>
        <span>3</span>
        <span>尾页</span>
    </div>

2.使用Mybatis-Plus方式编写 dao层 和 service层

示例代码:

dao:(UserMapper.java)
@Mapper
	public interface UserMapper extends BaseMapper<User> {
	}
service:(UserService.java & UserServiceImpl.java)
  • UserService.java(接口)

    public interface UserService extends IService<User> {
    
    }
    
  • UserServiceImpl.java(实现类)

    @Service
    public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    
    }
    

3. Controller中获取数据库中的数据并转发到前端页面

示例代码:

controller:(UserController.java)
@GetMapping("/paging")
    public String pagin(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model){
        Page<User> userPage = new Page<>(pn,2);
        Page<User> page = userService.page(userPage, null);
        model.addAttribute("page", page);
        return "pagin";
    }

这里要详细的解说一下:

userService.page方法是分页查出数据,方法中有两个参数:
	- @param page(翻页对象),继承于IPape
		Page对象也有两个参数:
			- @param current 当前页
			- @param size 每页显示条数
	- @param queryWrapper (实体对象封装操作类):查询的条件封装对象

Page<User> page = userService.page(userPage, null) 得到了分页查询的结果

通过得到的结果的page对象,
在这里插入图片描述

  • page.getPages():得到总页码
  • page.getCurrent:获取当前页码
  • page.getRecords:获取所有记录(也就是对象集,我们需要的数据列表)
  • page.getTotal:得到数据的总数
    …还有很多的方法,可以去Mybatis-Plus官网查看

所有最后只需要把数据放入model中,就能通过thymeleaf模板引擎去页面获取!!!

4.使用thymeleaf来进行页面渲染

示例代码:

<table border="1px" width="500px" height="200px" >
        <thead>
        <tr>
            <th>id</th>
            <th>用户名</th>
            <th>age</th>
            <th>email</th>
        </tr>
        </thead>
        <tbody>
        <tr th:each="user:${page.records}">
            <td th:text="${user.id}"></td>
            <td th:text="${user.name}">毛哥</td>
            <td th:text="${user.age}">18</td>
            <td th:text="${user.email}">1457643551@qq.com</td>
        </tr>
        </tbody>
    </table>
    <div width="500px" >当前第 [[${page.current}]] 页 总计 [[${page.pages}]] 页 共 [[${page.total}]] 条记录&nbsp;&nbsp;&nbsp;
        <a th:href="@{/paging(pn=1)}">首页</a>&nbsp;&nbsp;
        <span th:each="num:${#numbers.sequence(1,page.pages)}"><a th:href="@{/paging(pn=${num})}">[[${num}]]&nbsp;&nbsp;&nbsp;&nbsp;</a></span>

        <a th:href="@{/paging(pn=${page.pages})}">尾页</a>
    </div>

5.编写拦截器然后注入到SpringBoot容器中

写一个配置类,然后把MybatisPlusInterceptor通过@Bean注入到SpringBoot容器中:

/**
     * MybatisPlusInterceptor
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        //这是分页拦截器
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
//        设置请求页面大于最大页后操作,true调回到首页,false继续请求,默认false
        paginationInnerInterceptor.setOverflow(true);
//        设置最大单页限制数量,默认500条,-1不受限制
        paginationInnerInterceptor.setMaxLimit(500l);

        mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
        return mybatisPlusInterceptor;
    }

6.展示最终结果

Mybatis-Plus分页功能展示

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
Mybatis-Plus 是一个 Mybatis 的插件,提供了很多增强功能,其中包括分页查询。Mybatis-Plus 的分页查询使用起来非常简单,只需要引入相关依赖,然后在查询方法中使用 Page 对象即可。 具体步骤如下: 1. 引入 Mybatis-Plus 的分页插件依赖,例如: ``` <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version> </dependency> ``` 2. 在查询方法中创建一个 Page 对象,并且将当前页和每页显示条数设置好,例如: ``` Page<User> page = new Page<>(1, 10); ``` 3. 调用 Mybatis-Plus 提供的分页查询方法,例如: ``` IPage<User> userPage = userMapper.selectPage(page, new QueryWrapper<User>().lambda().eq(User::getAge, 18)); ``` 其中,userMapper 是 Mybatis-Plus 自动生成的 Mapper 接口,selectPage 是 Mybatis-Plus 提供的分页查询方法,第一个参数是 Page 对象,第二个参数是查询条件。 4. 最后,可以通过 userPage 对象获取分页查询的结果,例如: ``` List<User> userList = userPage.getRecords(); long total = userPage.getTotal(); ``` 其中,getRecords 方法返回当前页的数据列表,getTotal 方法返回总记录数。 这就是 Mybatis-Plus 的分页查询的基本用法。需要注意的是,Mybatis-Plus 默认使用的是物理分页,也就是先查询出所有符合条件的记录,然后再根据分页参数返回对应的数据,这种方式虽然能够实现分页,但是对于数据量非常大的情况下,会影响查询性能。因此,建议在需要分页的情况下,使用逻辑分页,也就是在查询条件中加上分页相关的参数,例如 limit offset。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

springboot大神

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值