2016-09-27 57 views
1

我使用mybatis從數據庫中獲取數據。但是我會使用Pageable對象(從spring開始)具有分頁功能。這可能嗎?使用PaginationAndSortRepository擴展myMapper還是足夠了嗎?如何在MyBatis中使用PagingAndSorting?

例子:

@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不僅被請求的.... 我每次都必須提取感興趣的列表?

回答

1

幾周前我在找同樣的東西,看起來這是不可能的。 爲了什麼,我發現您在使用RowBound參數

實現自己的分頁程序(該RowBounds參數導致的MyBatis略過指定的記錄數,以及限制結果的數量恢復到部分號碼)

在這個答案 如解釋說,我敢肯定你已經紅How to do Pagination with mybatis?

擴展與PaginationAndSorting你的映射器不會做的工作。

This project on github 似乎做你在找什麼,但我沒有嘗試它,它沒有評論,所以我不知道它是否可靠,可以用作解決方案。

+0

Txs非常多。我希望在解決方案。但沒關係。我將通過手動擴展我的映射器來解決它。 – java4fun

+1

我也希望在解決方案。讓我們看看是否有人找到更好的選擇。 – amicoderozer

0

我一直在使用Mybatis-PageHelper插件做對SQL Server分頁支持

數據庫:

  • 甲骨文
  • 的Mysql/MariaDB的
  • SQLite的
  • HSQLDB
  • PostgreSQL的
  • DB2
  • SqlServer的(2005年對2016)
  • 的Informix
  • H2

宣言行家

<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()); 
+0

Txs。我沒有mybatis-config.xml。我該如何配置?我的應用程序屬性中只有一個條目:mybatis.mapperLocations = classpath *:**/mybatis/mappers/*。xml其中指向具有映射器的包。 – java4fun