SpringBoot整合MybatisPlus分页插件

1:配置类。

@Configuration
public class PageConf {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

2:举例。

        Page<User> page = new Page<>(1,5);  //传入得参数是一页五条数据。

       userService.page(page, new QueryWrapper<User>)将page传入,然后第二个参数指定查询条件。

    /**
    * 搜索
    * */

    @GetMapping("/search")
    public Result search(Integer currentPage,String search){

        if(currentPage == null || currentPage < 1) {
            currentPage = 1;
        }
        Page<User> page = new Page<>(currentPage,5);
        IPage<User> page1 = userService.page(page, new QueryWrapper<User>().like("username","%"+search+"%"));
        return  Result.succ(page1);
    }

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
SpringBoot整合MybatisPlus可以很方便地实现分页查询,而MybatisPlus本身也提供了物理分页的功能。下面是实现步骤: 1.在pom.xml文件中添加MybatisPlus和分页插件的依赖。 2.在application.yml文件中配置分页插件。 3.在Mapper接口中添加分页查询方法,使用MybatisPlus提供的Page对象进行分页查询。 4.在Service层中调用Mapper接口中的分页查询方法,将查询结果封装到Page对象中返回给Controller层。 5.在Controller层中接收前端传来的分页参数,调用Service层中的分页查询方法,将查询结果返回给前端。 具体实现代码可以参考以下示例: 1.在pom.xml文件中添加MybatisPlus和分页插件的依赖: ``` <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> ``` 2.在application.yml文件中配置分页插件: ``` mybatis-plus: configuration: # 开启驼峰命名转换 map-underscore-to-camel-case: true # 配置分页插件 plugins: - com.github.pagehelper.PageInterceptor # 配置分页插件的参数 pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql ``` 3.在Mapper接口中添加分页查询方法,使用MybatisPlus提供的Page对象进行分页查询: ``` public interface UserMapper extends BaseMapper<User> { /** * 分页查询用户列表 * @param page 分页参数 * @return 用户列表 */ List<User> selectUserList(Page<User> page); } ``` 4.在Service层中调用Mapper接口中的分页查询方法,将查询结果封装到Page对象中返回给Controller层: ``` @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public Page<User> getUserList(Page<User> page) { return userMapper.selectUserList(page); } } ``` 5.在Controller层中接收前端传来的分页参数,调用Service层中的分页查询方法,将查询结果返回给前端: ``` @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/list") public Result getUserList(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) { Page<User> page = new Page<>(pageNum, pageSize); Page<User> userList = userService.getUserList(page); return Result.success(userList); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小泽不会Java

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

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

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

打赏作者

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

抵扣说明:

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

余额充值