How use PagingAndSorting with MyBatis?

java4fun picture java4fun · Sep 27, 2016 · Viewed 9.6k times · Source

I use mybatis for retriving data from the DB. But I would use the Pageable object (from spring) to have the pagination functionalities. Is this possible? It is still enough to extend myMapper with a PaginationAndSortRepository?

Example:

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

and then I use it from my service:

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

The problem is that so I will return all MyObjects and not only the requested set.... I have to extract the interested list every time?

Answer

amicoderozer picture amicoderozer · Sep 27, 2016

I was searching the same thing some weeks ago and seems this is not possible. For what i found you have to implement your own paginator using RowBound parameter

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

as explained in this answer that i'm sure you have already red How to do Pagination with mybatis?

Extending your mapper with PaginationAndSorting will not doing the job.

This project on github seems do what you are searching but i haven't try it and it has no comments, so i don't know if it's reliable and can be used as solution.