栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Mybatis的分页方式详解以及Mybatis-plus分页。

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Mybatis的分页方式详解以及Mybatis-plus分页。

目录:   一、Mybatis 分页        一、LIMIT关键字        二、RowBounds实现分页        三、PageHelper   二、Mybatis-plus分页

一、Mybatis 实现分页

(1)使用 LIMIT 关键字

业务层将pageNo 和pageSize传过来,即可实现分页操作。

灵活性高,可优化空间大,但是对于新手比较复杂。


    select * from user limit #{pageNo}, #{pageSize}

(2)RowBounds实现分页

Mybatis官方提供RowBounds类来实现逻辑分页。RowBounds中有2个字段offset和limit。这种方式获取所有的ResultSet,从ResultSet中的offset位置开始获取limit个记录。

 只需要填充两个参数到RowBounds中,即可使用。

controller层:

   RowBounds rowBounds = new RowBounds(page, size);

mapper层:

 List findpage(RowBounds rowBounds);

mapper.xml:

   

三、PageHelper实现分页

PageHelper是一个第三方实现的分页拦截器插件。

 添加pom依赖:

		
			com.github.pagehelper
			pagehelper
			5.3.1
		

 直接使用pagehelper方法,更加简便。

  二、Mybatis-plus分页

(1)加入pom依赖

        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.1.0
        

(2)在启动类同层写个Mybatis-plus配置类:

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
@Configuration
@MapperScan("com.exam.service.*.mapper*")
public class MybatisPlusConfig {
    
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

 (3)传入参数,构建Page<>对象 

最后,实现完整的查询操作即可。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1028911.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号