【问题标题】:How use PagingAndSorting with MyBatis?如何在 MyBatis 中使用 PagingAndSorting?
【发布时间】:2017-02-04 17:38:00
【问题描述】:

我使用 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());

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

【问题讨论】:

标签: spring pagination mybatis spring-mybatis


【解决方案1】:

我一直在使用Mybatis-PageHelper插件对Sql Server进行分页

支持的数据库:

  • 甲骨文
  • Mysql/MariaDB
  • SQLite
  • Hsqldb
  • PostgreSQL
  • DB2
  • SqlServer(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());

【讨论】:

  • Txs。我没有 mybatis-config.xml。我该如何配置?我的应用程序属性中只有一个条目: mybatis.mapperLocations=classpath*:**/mybatis/mappers/*.xml 其中指向具有映射器的包。
【解决方案2】:

几周前我正在搜索相同的内容,但似乎这是不可能的。 对于我发现你必须使用 RowBound 参数实现自己的分页器

(RowBounds参数使MyBatis跳过指定的记录数,以及将返回的结果数限制为某个数)

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

使用 PaginationAndSorting 扩展您的映射器不会完成这项工作。

This project on github 似乎做你正在搜索的东西,但我没有尝试过,它没有 cmets,所以我不知道它是否可靠并且可以用作解决方案。

【讨论】:

  • 非常感谢。我希望在解决方案中。但没关系。我将通过手动扩展我的映射器来解决它。
猜你喜欢
  • 2014-02-15
  • 2014-08-01
  • 2018-12-07
  • 2018-12-02
  • 1970-01-01
  • 2021-06-09
  • 2022-01-08
  • 2019-11-20
  • 1970-01-01
相关资源
最近更新 更多