Skip to content

Pagination_QuickStart_MyBatis

fangjinuo edited this page May 20, 2020 · 12 revisions

Use it in MyBatis application

Installation

case 1, use it with springboot application:

just import dependencies:

<dependency>
    <groupId>com.github.fangjinuo.sqlhelper</groupId>
    <artifactId>sqlhelper-mybatis-spring-boot-autoconfigure</artifactId>
    <version>${sqlhelper.version}</version>
</dependency>
<dependency>
    <groupId>com.github.fangjinuo.sqlhelper</groupId>
    <artifactId>sqlhelper-mybatis-spring-boot-starter</artifactId>
    <version>${sqlhelper.version}</version>
</dependency>

also see sqlhelper-examples module

从 3.0.3 版本开始,需要引入:

<dependency>
    <groupId>com.github.fangjinuo.sqlhelper</groupId>
    <artifactId>sqlhelper-jsqlparser</artifactId>
    <version>${sqlhelper.version}</version>
</dependency>

case 2, other applictaion (without springboot):

1.import dependencies:

<dependency>
    <groupId>com.github.fangjinuo.sqlhelper</groupId>
    <artifactId>sqlhelper-dialect</artifactId>
    <version>${sqlhelper.version}</version>
</dependency>

2.config mybatis-config.xml

<configuration>
     ...
     <databaseIdProvider type="DB_VENDOR">
         <property name="SQL Server" value="sqlserver"/>
         <property name="DB2" value="db2"/>
         <property name="Oracle" value="oracle" />
     </databaseIdProvider>
     ...
     <settings>
         ...
         <setting name="defaultScriptingLanguage" value="com.jn.sqlhelper.mybatis.plugins.pagination.CustomScriptLanguageDriver" />
         ...
     </settings>
         ...
</configuration>
    
<plugins>
    <plugin interceptor="com.jn.sqlhelper.mybatis.plugins.pagination.MybatisPaginationPlugin" />
</plugins>

case 3, in a springboot application, but without spring datasource:

In this scenario, the mybatis-spring-boot-autoconfigure, sqlhelper-mybatis-spring-boot-autoconfigure will be invalid, so the mybatis application will start fail. So you have to consum your SqlSessionFactoryBean . So when you consum you SqlSessionFactoryBean, you should do like this:


case 4, in springboot application + mybatis + mybatis-pageHelper , migrate to sqlhelper

step 1: remove mybatis-pageHelper dependency
step 2: import sqlhelper's:
  1. sqlhelper-mybatis
  2. sqlhelper-mybatis-spring-boot-autoconfigure
  3. sqlhelper-mybatis-spring-boot-starter
  4. sqlhelper-mybatis-over-pagehelper

How to

you can use it like this:

    @GetMapping
    public PagingResult list(){
        User queryCondtion = new User();
        queryCondtion.setAge(10);
   
        PagingRequest request = SqlPaginations.preparePagination(1,10);
        List<User> users = userDao.selectByLimit(queryCondtion); // users is the data list        
        return request.getResult(); // result has: total ,pagesize, pageNo, maxPage ...
    }

Migrate from mybatis-pagehelper

just replace mybatis-pagehelper dependencies to sqlhelper-mybatis-over-pagehelper:

<dependency>
    <groupId>com.github.fangjinuo.sqlhelper</groupId>
    <artifactId>sqlhelper-mybatis-over-pagehelper</artifactId>
    <version>${sqlhelper.version}</version>
</dependency>

use it, your code will not make any changes