spring boot Mybatis Plus分页

文章目录

Mybatis Plus自带分页和PageHelper有什么区别?

网上描述:

Mapper Plus自带分页PaginationInterceptor对象,虽然说目前没有什么问题,并且使用简单,但是个人感觉有个弊端:目前个人使用中,想要用Mapper Plus自带的分页功能的话需要在mapper对象中传入一个Page对象才可以实现分页,这样耦合度是不是太高了一点,从web到service到mapper,这个Page对象一直都在传入,这样的使用让人感觉有点麻烦~

Mybatis Plus整合PageHelper分页

Mybatis Plus整合PageHelper分页

参考URL: https://blog.csdn.net/m0_37701381/article/details/100719280

SpringBoot2.1+MybatisPlus+Pagehelper框架整合(其中与Dubbo整合时分页失效的疑问与解决)

参考UIRL: https://blog.csdn.net/lstcui/article/details/89068918

springboot自定义拦截器获取分页参数

ThreadLocal Pager 分页的一种解决方案

参考URL: https://blog.csdn.net/cmdsmith/article/details/66969728

spring boot下配置mybatis-plus分页插件

springBoot 使用 mybatis-plus 插件 实现分页

https://blog.csdn.net/sinat_34338162/article/details/83543994?depth_1-utm_source=distribute.pc_relevant.none-task\&utm_source=distribute.pc_relevant.none-task

需要写一个分页的配置类分页功能才能生效

java 复制代码
/**
 *  //Spring boot方式
 * @Description: MybatisPlus配置类
 */
@Configuration
public class MyBatisPlusConfig {
 
    /**
     * 分页插件
     * @return
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

单表分页查询

如果只是单表,那么分页查询就容易的多了。

这里的@ModelAttribute注解可以将前端传过来的current和size字段映射到Page对象中。

java 复制代码
   /**
     * @param page 查询一般传入参数为current和size, 例如/listPage?current=1&size=5,
     * @return 返回分页数据
     */
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public ResponseObj<Page<T>> listPage(@ModelAttribute Page<T> page, @ModelAttribute T model) {
        Page<T> pageList = service.selectPage(page, new EntityWrapper<>(model));
        for (T eachObj : pageList.getRecords()) {
            queryFilter(eachObj);
        }
        return new ResponseObj<>(pageList, RetCode.SUCCESS);
    }
java 复制代码
    @RestController
    @RequestMapping("/student")
    public class StudentController {
    
        @Autowired
        IStudentService studentService;
    
        @RequestMapping(value = "/findAll",method = RequestMethod.POST)
        public Object findAll(HttpServletRequest request){
            //获取前台发送过来的数据
            Integer pageNo = Integer.valueOf(request.getParameter("pageNo"));
            Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
            IPage<Student> page = new Page<>(pageNo, pageSize);
            QueryWrapper<Student> wrapper = new QueryWrapper<>();
            Student student = new Student();
            student.setId(1);
            wrapper.setEntity(student);
            return studentService.page(page,wrapper);
        }
    
    }

总结: 整体思路很简单,需要2个参数,一个是 IPage page实例,传入pageNo、pageSize ,一个是QueryWrapper wrapper实例。 使用时把page传入会自动在sql语句后面添加limit。

自定义sql分页查询

有时候查询的数据难免会出现多表连接查询,或者是一些复杂的sql语句,但是这些语句也是需要支持分页查询的。

先定义查询接口,第一个参数要是分页的参数。

步骤一:在mapper文件中,编写对应的分页查询接口。

步骤二:在xml中编写对应的sql语句,小编这里演示的 "${ew.customSqlSegment}",这个是如果你想自定义的sql语句,也想使用wrapper查询条件构造器,则需要在mapper接口中添加参数,以及xml中也要有固定。

PageHelper

PageHelper用于查询语句分页,让分页更简单、代码更优雅。

参考

MyBatis-Plus 分页查询以及自定义sql分页

参考URL: https://blog.csdn.net/weixin_38111957/article/details/91554108?depth_1-utm_source=distribute.pc_relevant.none-task\&utm_source=distribute.pc_relevant.none-task

mybatis-plus分页查询

参考URL: https://www.jianshu.com/p/43bfe6fe8d89

相关推荐
努力的小雨4 小时前
还在为调试提示词头疼?一个案例教你轻松上手!
后端
魔都吴所谓5 小时前
【go】语言的匿名变量如何定义与使用
开发语言·后端·golang
陈佬昔没带相机5 小时前
围观前后端对接的 TypeScript 最佳实践,我们缺什么?
前端·后端·api
旋风菠萝6 小时前
JVM易混淆名称
java·jvm·数据库·spring boot·redis·面试
Livingbody7 小时前
大模型微调数据集加载和分析
后端
Livingbody7 小时前
第一次免费使用A800显卡80GB显存微调Ernie大模型
后端
77qqqiqi7 小时前
解决Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are required报错问题
java·数据库·微服务·mybatis·mybatisplus
weisian1517 小时前
Java WEB技术-序列化和反序列化认识(SpringBoot的Jackson序列化行为?如何打破序列化过程的驼峰规则?如何解决学序列化循环引用问题?)
java·spring boot
橘子编程7 小时前
SpringMVC核心原理与实战指南
java·spring boot·spring·tomcat·mybatis
Goboy8 小时前
Java 使用 FileOutputStream 写 Excel 文件不落盘?
后端·面试·架构