spring-data-mybatis-pagination

Mybatis implementation for pagination interfaces in spring-data-common

License

License

Categories

Categories

Data MyBatis ORM
GroupId

GroupId

com.github.sea-huang
ArtifactId

ArtifactId

spring-data-mybatis-pagination
Last Version

Last Version

2.1.2
Release Date

Release Date

Type

Type

jar
Description

Description

spring-data-mybatis-pagination
Mybatis implementation for pagination interfaces in spring-data-common
Project URL

Project URL

https://github.com/sea-huang/spring-data-mybatis-pagination
Source Code Management

Source Code Management

https://github.com/sea-huang/spring-data-mybatis-pagination.git

Download spring-data-mybatis-pagination

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.sea-huang/spring-data-mybatis-pagination/ -->
<dependency>
    <groupId>com.github.sea-huang</groupId>
    <artifactId>spring-data-mybatis-pagination</artifactId>
    <version>2.1.2</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.sea-huang/spring-data-mybatis-pagination/
implementation 'com.github.sea-huang:spring-data-mybatis-pagination:2.1.2'
// https://jarcasting.com/artifacts/com.github.sea-huang/spring-data-mybatis-pagination/
implementation ("com.github.sea-huang:spring-data-mybatis-pagination:2.1.2")
'com.github.sea-huang:spring-data-mybatis-pagination:jar:2.1.2'
<dependency org="com.github.sea-huang" name="spring-data-mybatis-pagination" rev="2.1.2">
  <artifact name="spring-data-mybatis-pagination" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.sea-huang', module='spring-data-mybatis-pagination', version='2.1.2')
)
libraryDependencies += "com.github.sea-huang" % "spring-data-mybatis-pagination" % "2.1.2"
[com.github.sea-huang/spring-data-mybatis-pagination "2.1.2"]

Dependencies

compile (6)

Group / Artifact Type Version
org.mybatis : mybatis jar 3.4.6
org.springframework.data : spring-data-commons jar 1.13.13.RELEASE
com.github.pagehelper : pagehelper jar 5.1.4
org.slf4j : slf4j-api jar 1.7.25
org.mybatis.spring.boot : mybatis-spring-boot-starter Optional jar 1.3.2
org.springframework : spring-web Optional jar 4.3.18.RELEASE

test (7)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-test jar 1.5.13.RELEASE
com.h2database : h2 jar 1.4.197
org.mybatis.spring.boot : mybatis-spring-boot-starter-test jar 1.3.2
org.flywaydb : flyway-core jar 5.1.4
org.springframework.boot : spring-boot-starter-web jar 1.5.13.RELEASE
com.alibaba : fastjson jar 1.2.0
javax.servlet : servlet-api jar 2.5

Project Modules

There are no modules declared in this project.

中文

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>
    

Versions

Version
2.1.2
2.1.1
2.1.0
2.0.0
1.1.2
1.1.1
1.1.0
1.0.0