spring boot integrates pagehelper of in two ways

  • 2020-12-10 00:43:43
  • OfStack

See the pagehelper - spring - boot, use rise very convenient, for more PageHelper can click https: / / github com/pagehelper/Mybatis - PageHelper.

When spring boot is integrated with mybatis and needs to be paginated, we first add maven support


 <dependency>
  <groupId>com.github.pagehelper</groupId>
  <artifactId>pagehelper</artifactId>
  <version>5.1.2</version>
 </dependency>
 <dependency>
  <groupId>com.github.pagehelper</groupId>
  <artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
  <version>1.2.3</version>
 </dependency>
 <dependency>
  <groupId>com.github.pagehelper</groupId>
  <artifactId>pagehelper-spring-boot-starter</artifactId>
  <version>1.2.3</version>
 </dependency>

Approach 1: We add application. yml(yml that spring needs to read)


pagehelper:
 helperDialect: mysql
 reasonable: true
 supportMethodsArguments: true
 params: count=countSql

Then restart it.

The configuration file is eventually read by java and injected into spring bean, so our method 2 is to configure its bean class, which is easy to modify by hot loading. Of course, method 1 is easier.

Option 2: Create a new PageHeleperConfig under the annotations covering package


import com.github.pagehelper.PageHelper;
import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zhuxiaomeng
 * @date 2018/1/2.
 * @email 154040976@qq.com
 */
@Configuration
public class PageHelperConfig {


 @Bean
 public PageHelper getPageHelper(){
 PageHelper pageHelper=new PageHelper();
 Properties properties=new Properties();
 properties.setProperty("helperDialect","mysql");
 properties.setProperty("reasonable","true");
 properties.setProperty("supportMethodsArguments","true");
 properties.setProperty("params","count=countSql");
 pageHelper.setProperties(properties);
 return pageHelper;
 }

}

Basic knowledge of pageHelper is:


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

Page<T> tPage= PageHelper.startPage(page,limit);

The next sentence of the query to page. You only need to use List < T > receive

If you have questions you can download the open source project lenos Rapid development scaffolding, spring boot version to familiarize yourself with learning.

Address: https: / / gitee com/bweird/lenosp


Related articles: