MyBatis Plus实现分页

1. 配置类

@Configuration
public class MpConfig {

    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

2. 实现步骤

//1.创建page对象
//传入两个参数:当前页 和 每页显示记录数
Page<User> page = new Page<>(1,3);
//调用mybatis-plus分页查询的方法
//第一个参数为把分页所以数据封装到page对象里面,第二个参数为查询筛选条件
userMapper.selectPage(page, null);
//每页数据list集合
System.out.println(page.getRecords());
//当前页
System.out.println(page.getCurrent());
//每页显示记录数
System.out.println(page.getSize());
//总记录数
System.out.println(page.getTotal());
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
1. 添加mybatis plus依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency> ``` 2. 配置mybatis plus 在application.yml文件中添加以下配置: ``` mybatis-plus: mapper-locations: classpath*:mapper/*.xml configuration: map-underscore-to-camel-case: true global-config: page-helper: offset-as-page-num: true limit-as-page-size: true page-size-zero: true page-size-max: 1000 ``` - mapper-locations:指定mapper文件的位置 - configuration:mybatis的配置项,这里开启下划线转驼峰 - global-config:全局配置,其中page-helper是分页插件的配置 3. 编写mapper.xml文件 在mapper目录下新建一个xxxMapper.xml文件,例如UserMapper.xml,编写查询语句和分页语句,如下: ``` <select id="getUserList" parameterType="java.util.Map" resultMap="BaseResultMap"> select * from user where 1 = 1 <if test="name != null"> and name like concat('%', #{name}, '%') </if> order by create_time desc </select> <select id="getUserPage" parameterType="java.util.Map" resultMap="BaseResultMap"> select * from user where 1 = 1 <if test="name != null"> and name like concat('%', #{name}, '%') </if> order by create_time desc limit #{startIndex}, #{pageSize} </select> ``` 其中getUserList是用于查询总记录数的语句,getUserPage是用于分页查询的语句。startIndex和pageSize是分页参数。 4. 编写service层 在service层中,注入UserMapper,并编写分页查询方法,如下: ``` @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public Page<User> getUserPage(int pageNum, int pageSize, String name) { Page<User> page = new Page<>(pageNum, pageSize); Map<String, Object> params = new HashMap<>(); params.put("startIndex", (pageNum - 1) * pageSize); params.put("pageSize", pageSize); params.put("name", name); List<User> userList = userMapper.getUserPage(params); int total = userMapper.getUserList(params).size(); page.setRecords(userList); page.setTotal(total); return page; } } ``` Page是mybatis plus提供的分页对象,通过调用setRecords和setTotal方法,将查询结果和总记录数设置到Page对象中。 5. 编写controller层 在controller层中,注入UserService,并编写分页查询接口,如下: ``` @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/page") public Page<User> getUserPage(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize, @RequestParam(required = false) String name) { return userService.getUserPage(pageNum, pageSize, name); } } ``` 通过调用getUserPage方法,返回分页查询结果。 至此,springboot项目myBatis plus实现分页查询就完成了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值