Skip to content

sea-huang/spring-data-mybatis-pagination

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 

Repository files navigation

spring-data-mybatis-pagination

Support Pageable/Page/Sort/Order from spring-data-commons in Mybatis.

Auto generate and concatenate pagination sql for most database types, implemented by PageHelper.

Support full features of ordering including "direction", "ignore case" and "null handlings", refer to hibernate dialect(rewritten).

Add full features parsing for spring-data mvc-support.

Bean Adapters enhance which is more compatible for PRC serialization.

Usage Examples

  • spring-data like standard pagination, the sql in xml don't need involve pagination part, which is auto concatenated underneath.

      Page<User> select(@Param("name") String name, Pageable pageable);
    
  • If define the result type as list. no counting sql will be executed, but only offset/limit do

    List<User> select(@Param("name") String name, Pageable pageable);
    
  • For people who prefer @Select or @SqlProvider annotatain which don't explicitly define a result mapping strategy, Mybatis may not compatible to Page but only List results. In this cases, use ListPage<?> as the result which is a Page adapter to List.

      @Select("SELECT * FROM T_USER")
      ListPage<User> findPage(Pageable pageable);
      
  • If you don't like the spring-data standard invasive style. We have a non invasive way like PageHelper, the result can be cast to ListPage/Page

    SpringDataPageHelper.paginateNextCall(new PageRequest(0,4));
    List<User> users = userMapper.findUserByName("a");
    

Assert.assertEquals(com.github.seahuang.spring.data.mybatis.pagination.adapter.ListPageImpl.class, users.getClass());

  • Also, no-counting could be set. And in this case, result can't be cast to spring Page(you can consider it as a normal List)

    SpringDataPageHelper.paginateNextCall(new PageRequest(0,4), false);
    users = userMapper.findUserByName("a");
    Assert.assertEquals(com.github.pagehelper.Page.class, users.getClass());
    
  • Default Page/Pageable/Sort/Order implementation in spring-data don't support deserialization in RPC call very well. We offer a bean style version

    Pageable pageable = new PageRequest(0,4,sort);
    PageableBean pageable = PageableBean.from(pageable);
    PageBean<T> page = PageBean<T>.from(new PageImpl(), pageable);
    
  • Official mvc feature don't support "ignore case" or "null handling" we makeup that.

    /test?page=1&size=10&sort=AA,BB,CC,IGNORECASE|DESC|NULLS_LAST
    
  • Setup default, it's a supplimentary to official annotations

    	@MoreSortDefault(value={"AA","BB"}, ignoreCase=true
      	, nullHandling=NullHandling.NULLS_LAST) Pageable pageable
    
  • A more complicated case:

    @MoreSortDefaults({
      	@MoreSortDefault(value="AA", ignoreCase=true)
      	,@MoreSortDefault(sort="BB", nullHandling=NullHandling.NULLS_LAST)
      }) @PageableDefault(sort={"AA","BB"}, direction=Direction.DESC) Pageable pageable
    

Set up

  • Add maven dependencies: PageHelper is not very stable. for 4.2.x

    <dependency>
    		<groupId>com.github.sea-huang</groupId>
    		<artifactId>spring-data-mybatis-pagination</artifactId>
    		<version>1.1.2</version>
    </dependency>
  • For pagehelper 5.1.x

    <dependency>
    		<groupId>com.github.sea-huang</groupId>
    		<artifactId>spring-data-mybatis-pagination</artifactId>
    		<version>2.1.2</version>
    </dependency>
  • For spring boot app, if mybatis-spring-boot-starter(min-version 1.2.1) is on classpath. PaginationPlugin is autoconfigured

  • For other cases, PaginationObjectFactory/PaginationPlugin should be manually configured

    <configuration>
    		<objectFactory type="com.github.seahuang.spring.data.mybatis.pagination.adapter.PaginationObjectFactory"/>
    		<plugins>
      		<plugin interceptor="com.github.seahuang.spring.data.mybatis.pagination.adapter.PaginationPlugin">
      		</plugin>
      	</plugins>
    </configuration>
    
  • Or

      @Bean
      public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
      	SqlSessionFactoryBean result = new SqlSessionFactoryBean();
      	result.setDataSource(dataSource);
      	result.setObjectFactory(new PaginationObjectFactory());
      	result.setPlugins(new Interceptor[]{new PaginationPlugin()});
      	return result;
      }
    
  • To setup the enhanced mvc order parsing. we need to configure a MoreSortHandlerMethodArgumentResolver to take the place of SortHandlerMethodArgumentResolver. For boot:

    @SpringBootApplication
    public class TestApplication extends WebMvcConfigurerAdapter {
      
      @Override
      public void addArgumentResolvers(List argumentResolvers) {
          super.addArgumentResolvers(argumentResolvers);
          MoreSortHandlerMethodArgumentResolver sortResolver = new MoreSortHandlerMethodArgumentResolver();
          argumentResolvers.add(sortResolver);
          argumentResolvers.add(new PageableHandlerMethodArgumentResolver(sortResolver));
      }
     }
    
  • xml

    	<mvc:annotation-driven>  
          <mvc:argument-resolvers>
      		<ref bean="sortResolver"/>
              <ref bean="pageableResolver" />
      	</mvc:argument-resolvers>
      </mvc:annotation-driven>
      <bean id="sortResolver" class="com.github.seahuang.spring.data.mybatis.pagination.mvc.MoreSortHandlerMethodArgumentResolver" />
      <bean id="pageableResolver" class="org.springframework.data.web.PageableHandlerMethodArgumentResolver">
          <constructor-arg ref="sortResolver" />
      </bean>