如何在 MyBatis 中使用 PagingAndSorting?

How use PagingAndSorting with MyBatis?

我使用 mybatis 从数据库中检索数据。但我会使用 Pageable 对象(来自 spring)来实现分页功能。这可能吗?用 PaginationAndSortRepository 扩展 myMapper 仍然足够吗?

示例:

@Mapper
public interface MyObjetcMapper extends PagingAndSortingRepository {
    List<MyObjetc> findAllMyObjetcs(Pageable pageable);
}

然后我在我的服务中使用它:

List<MyObjetc> myObjetc= myObjetcMapper.findAllCharacteristics(pageable);
return new PageImpl<>(characteristics, new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), pageable.getSort()), MyObjetc.size());

问题是,所以我将 return 所有 MyObjects 而不仅仅是请求的集合.... 每次都要提取感兴趣的列表?

几周前我也在搜索同样的东西,但似乎这是不可能的。 对于我发现你必须使用 RowBound 参数

实现你自己的分页器

(The RowBounds parameter causes MyBatis to skip the number of records specified, as well as limit the number of results returned to some number)

如本回答所述 我确定你已经变红了 How to do Pagination with mybatis?

使用 PaginationAndSorting 扩展映射器将无法完成这项工作。

This project on github 似乎在做你正在搜索的事情,但我没有尝试过,也没有评论,所以我不知道它是否可靠,是否可以用作解决方案。

我一直在使用 Mybatis-PageHelper 插件针对 Sql Server

进行分页

支持的数据库:

  • 甲骨文
  • Mysql/MariaDB
  • SQLite
  • Hsqldb
  • PostgreSQL
  • DB2
  • Sql服务器(2005 年至 2016 年)
  • Informix
  • H2

maven 中的声明

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>4.1.6</version>
</dependency>

在mybatis中指定为插件-config.xml

<plugins>
    <plugin interceptor="com.github.pagehelper.PageHelper">
    </plugin>
</plugins>

用法示例:

// Get the results for page 1 where every page has 10 rows
PageHelper.startPage(1, 10);

List<MyObjetc> myObjetc= myObjetcMapper.findAllCharacteristics();

assertEquals(10, list.size());