Mybatis-Plus分页的使用与注意事项是什么

发布时间:2022-04-22 16:36:04 作者:iii
来源:亿速云 阅读:199

这篇文章主要介绍“Mybatis-Plus分页的使用与注意事项是什么”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Mybatis-Plus分页的使用与注意事项是什么”文章能帮助大家解决问题。

1.写个Mybatis-plus配置类:

是通过拦截器实现分页

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

官网复制即可,只是你需要把数据库改为你使用的,这里我是使用mysql

Mybatis-Plus分页的使用与注意事项是什么

2.写接口测试

很简单

@GetMapping("/test")
    public Response test(){
        Page<Produce> producePage = new Page<>(1,1);
        Page<Produce> page = produceService.page(producePage);
        System.out.println(producePage == page);
        List<Produce> records = page.getRecords();
        for (Produce record : records) {
            System.out.println(record);
        }
        return new Response<>(records, ResultEnum.SUCCESS);
    }

Mybatis-Plus分页的使用与注意事项是什么

默认是会查询总条数,都有get、set方法,可以根据自己的需求设置(点开Page类看看)

Mybatis-Plus分页的使用与注意事项是什么

3.注意

我们传入的page对象和查询返回的page对象是同一个

Mybatis-Plus分页的使用与注意事项是什么

Mybatis-Plus分页的使用与注意事项是什么

4.如果你还有查询条件

比如我们只查询id和price,id小于5的分页查询

Mybatis-Plus分页的使用与注意事项是什么

1.Lambda表达式

@GetMapping("/test")
public Response test(){
    Page<Produce> producePage = new Page<>(1,2);
    Page<Produce> page = new LambdaQueryChainWrapper<>(produceService.getBaseMapper())
            .select(Produce::getPid,Produce::getPrice)
            .lt(Produce::getPid,5)
            .page(producePage);

    return new Response<>(page.getRecords(), ResultEnum.SUCCESS);
}

Mybatis-Plus分页的使用与注意事项是什么

2.普通查询

@GetMapping("/test")
public Response test(){
    Page<Produce> producePage = new Page<>(1,2);
    QueryWrapper<Produce> queryWrapper = new QueryWrapper<>();
    queryWrapper.select("pid","price");
    queryWrapper.lt("pid",5);
    Page<Produce> page = produceService.page(producePage, queryWrapper);
    return new Response<>(page.getRecords(), ResultEnum.SUCCESS);
}

Mybatis-Plus分页的使用与注意事项是什么

Mybatis-Plus分页的使用与注意事项是什么

关于“Mybatis-Plus分页的使用与注意事项是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

推荐阅读:
  1. 使用Mybatis-Plus怎么实现多表联查分页
  2. MyBatis-Plus 分页查询以及自定义sql分页的实现

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mybatis-plus

上一篇:c语言怎么实现猜数字小游戏

下一篇:vue中如何配置请求本地json数据

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》