Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ PageHelper-Spring-Boot-Starter 帮助你集成分页插件到 Spring Boot。

PageHelper-Spring-Boot-Starter will help you use PageHelper with Spring Boot.

Support PageHelper 5.x
Support PageHelper 6.x

## How to use
在 pom.xml 中添加如下依赖:
Expand All @@ -16,10 +16,37 @@ Add the following dependency to your pom.xml:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.7</version>
<version>2.1.0</version>
</dependency>
```

## 微信公众号

<img src="wx_mybatis.jpg" height="300"/>

## v2.1.0 - 2023-12-17

- 升级 PageHelper 到 6.1.0,支持异步 count
等功能,详细查看 [6.1.0](https://github.com/pagehelper/Mybatis-PageHelper/releases/tag/v6.1.0)
- 升级 MyBatis 到 3.5.15
- 升级 springboot 到 2.7.18
- 新增参数 `orderBySqlParser`,`OrderBySqlParser`改为接口,允许通过`orderBySqlParser`参数替换为自己的实现
- 新增参数 `sqlServerSqlParser`,`SqlServerSqlParser`改为接口,允许通过`sqlServerSqlParser`参数替换为自己的实现
- 接口 `CountSqlParser`,`OrderBySqlParser`,`SqlServerSqlParser` 还支持SPI方式覆盖默认实现,优先级低于参数指定

## v2.0.0 - 2023-11-05

- 升级 PageHelper 到 6.0.0,支持异步 count 等功能,详细查看 [6.0](https://github.com/pagehelper/Mybatis-PageHelper/releases/tag/v6.0.0)
- 升级 MyBatis 到 3.5.15
- 升级 springboot 到 2.7.17
- 新增参数 `asyncCount`,增加异步count支持,默认`false`,单次设置:`PageHelper.startPage(1, 10).enableAsyncCount()`;
- 新增参数 `countSqlParser`,`CountSqlParser`改为接口,允许通过`countSqlParser`参数替换为自己的实现

参数示例:
```properties
pagehelper.async-count=true
```

## v1.4.7 - 2023-06-03

- 升级 PageHelper 到 5.3.3
Expand Down Expand Up @@ -219,3 +246,9 @@ If you want to control the order in which the interceptor plug-in, you can use t
//Or
@AutoConfigureBefore(PageHelperAutoConfiguration.class)
```

## 感谢所有项目贡献者!

<a href="https://github.com/pagehelper/pagehelper-spring-boot/graphs/contributors">
<img src="https://contributors-img.web.app/image?repo=pagehelper/pagehelper-spring-boot" />
</a>
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
* 自定注入分页插件
Expand All @@ -54,23 +58,49 @@ public class PageHelperAutoConfiguration implements InitializingBean {

private final PageHelperProperties properties;

public PageHelperAutoConfiguration(List<SqlSessionFactory> sqlSessionFactoryList, PageHelperStandardProperties standardProperties) {
private ApplicationContext applicationContext;

public PageHelperAutoConfiguration(List<SqlSessionFactory> sqlSessionFactoryList, PageHelperStandardProperties standardProperties, ApplicationContext applicationContext) {
this.sqlSessionFactoryList = sqlSessionFactoryList;
this.properties = standardProperties.getProperties();
this.applicationContext = applicationContext;
}

@Override
public void afterPropertiesSet() throws Exception {
PageInterceptor interceptor = new PageInterceptor();
interceptor.setProperties(this.properties);
// 加载需要排除的SqlSessionFactory
Set<SqlSessionFactory> excludeSqlSessionFactory = loadExcludeSqlSessionFactory();
for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
if (excludeSqlSessionFactory.contains(sqlSessionFactory)) {
continue;
}
org.apache.ibatis.session.Configuration configuration = sqlSessionFactory.getConfiguration();
if (!containsInterceptor(configuration, interceptor)) {
configuration.addInterceptor(interceptor);
}
}
}

/**
* 加载需要排除的 SqlSessionFactory
* @return 不需要增加 pageHelper 的集合
*/
private Set<SqlSessionFactory> loadExcludeSqlSessionFactory() {
Set<SqlSessionFactory> excludeSqlSessionFactory = new HashSet<>();
Optional.ofNullable(properties.getExcludeSqlSessionFactoryName())
.ifPresent(sqlSessionFactoryBeanNameList -> {
sqlSessionFactoryBeanNameList.forEach(sqlSessionFactoryBeanName -> {
SqlSessionFactory sqlSessionFactory = applicationContext.getBean(sqlSessionFactoryBeanName, SqlSessionFactory.class);
excludeSqlSessionFactory.add(sqlSessionFactory);
});
});
return excludeSqlSessionFactory;
}



/**
* 是否已经存在相同的拦截器
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.List;
import java.util.Properties;

/**
Expand All @@ -37,6 +38,8 @@
public class PageHelperProperties extends Properties {
public static final String PAGEHELPER_PREFIX = "pagehelper";

private List<String> excludeSqlSessionFactoryName;

public Boolean getOffsetAsPageNum() {
return Boolean.valueOf(getProperty("offsetAsPageNum"));
}
Expand Down Expand Up @@ -148,4 +151,44 @@ public String getAutoDialectClass() {
public void setAutoDialectClass(String autoDialectClass) {
setProperty("autoDialectClass", autoDialectClass);
}

public Boolean getAsyncCount() {
return Boolean.valueOf(getProperty("asyncCount"));
}

public void setAsyncCount(Boolean asyncCount) {
setProperty("asyncCount", asyncCount.toString());
}

public String getCountSqlParser() {
return getProperty("countSqlParser");
}

public void setCountSqlParser(String countSqlParser) {
setProperty("countSqlParser", countSqlParser);
}

public String getOrderBySqlParser() {
return getProperty("orderBySqlParser");
}

public void setOrderBySqlParser(String orderBySqlParser) {
setProperty("orderBySqlParser", orderBySqlParser);
}

public String getSqlServerSqlParser() {
return getProperty("sqlServerSqlParser");
}

public void setSqlServerSqlParser(String sqlServerSqlParser) {
setProperty("sqlServerSqlParser", sqlServerSqlParser);
}

public List<String> getExcludeSqlSessionFactoryName() {
return excludeSqlSessionFactoryName;
}

public void setExcludeSqlSessionFactoryName(List<String> excludeSqlSessionFactoryName) {
this.excludeSqlSessionFactoryName = excludeSqlSessionFactoryName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.List;
import java.util.Optional;

/**
Expand Down Expand Up @@ -44,6 +45,11 @@ public class PageHelperStandardProperties {
private Boolean keepOrderBy;
private Boolean keepSubSelectOrderBy;
private String sqlParser;
private Boolean asyncCount;
private String countSqlParser;
private String orderBySqlParser;
private String sqlServerSqlParser;
private List<String> excludeSqlSessionFactoryName;

@Autowired
public PageHelperStandardProperties(PageHelperProperties properties) {
Expand Down Expand Up @@ -251,4 +257,49 @@ public void setSqlParser(String sqlParser) {
this.sqlParser = sqlParser;
Optional.ofNullable(sqlParser).ifPresent(v -> properties.setProperty("sqlParser", v));
}

public Boolean getAsyncCount() {
return asyncCount;
}

public void setAsyncCount(Boolean asyncCount) {
this.asyncCount = asyncCount;
Optional.ofNullable(asyncCount).ifPresent(v -> properties.setProperty("asyncCount", v.toString()));
}

public String getCountSqlParser() {
return countSqlParser;
}

public void setCountSqlParser(String countSqlParser) {
this.countSqlParser = countSqlParser;
Optional.ofNullable(countSqlParser).ifPresent(v -> properties.setProperty("countSqlParser", v));
}

public String getOrderBySqlParser() {
return orderBySqlParser;
}

public void setOrderBySqlParser(String orderBySqlParser) {
this.orderBySqlParser = orderBySqlParser;
Optional.ofNullable(orderBySqlParser).ifPresent(v -> properties.setProperty("orderBySqlParser", v));
}

public String getSqlServerSqlParser() {
return sqlServerSqlParser;
}

public void setSqlServerSqlParser(String sqlServerSqlParser) {
this.sqlServerSqlParser = sqlServerSqlParser;
Optional.ofNullable(sqlServerSqlParser).ifPresent(v -> properties.setProperty("sqlServerSqlParser", v));
}

public List<String> getExcludeSqlSessionFactoryName() {
return excludeSqlSessionFactoryName;
}

public void setExcludeSqlSessionFactoryName(List<String> excludeSqlSessionFactoryName) {
this.excludeSqlSessionFactoryName = excludeSqlSessionFactoryName;
Optional.ofNullable(excludeSqlSessionFactoryName).ifPresent(properties::setExcludeSqlSessionFactoryName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package tk.mybatis.pagehelper;

import com.github.pagehelper.parser.defaults.DefaultCountSqlParser;

public class MyCountSqlParser extends DefaultCountSqlParser {
@Override
public String getSmartCountSql(String sql, String countColumn) {
return "/* count */" + super.getSmartCountSql(sql, countColumn);
}

@Override
public String getSimpleCountSql(String sql) {
return "/* count */" + super.getSimpleCountSql(sql);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package tk.mybatis.pagehelper;

import com.github.pagehelper.parser.defaults.DefaultOrderBySqlParser;

public class MyOrderBySqlParser extends DefaultOrderBySqlParser {

@Override
public String converToOrderBySql(String sql, String orderBy) {
return "/* order-by */" + super.converToOrderBySql(sql, orderBy);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,20 @@ public static void main(String[] args) {

@Override
public void run(String... args) throws Exception {
PageHelper.startPage(1, 20);
PageHelper.startPage(1, 20).disableAsyncCount();
List<User> users = userMapper.findAll();
System.out.println("Total: " + ((Page) users).getTotal());
for (User user : users) {
System.out.println("Name: " + user.getName());
}

PageHelper.orderBy("id desc");
users = userMapper.findAll();
System.out.println("Total: " + ((Page) users).getTotal());
for (User user : users) {
System.out.println("Name: " + user.getName());
}

PageRowBounds rowBounds = new PageRowBounds(3, 5);
users = userMapper.findAll(rowBounds);
System.out.println("Total: " + rowBounds.getTotal());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tk.mybatis.pagehelper.MyCountSqlParser
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ pagehelper.closeConn=true
pagehelper.hello=\u4F60\u597D
pagehelper.nihao=Hello
pagehelper.offset-as-page-num=true
pagehelper.count-column=*
pagehelper.count-column=*
pagehelper.async-count=true
pagehelper.orderBySqlParser=tk.mybatis.pagehelper.MyOrderBySqlParser
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>pagehelper-spring-boot-samples</artifactId>
<groupId>com.github.pagehelper</groupId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>pagehelper-spring-boot-samples-multi-sqlsessionfactory</artifactId>
<packaging>jar</packaging>
<name>pagehelper-spring-boot-samples-multi-sqlsessionfactory</name>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
</plugin>
</plugins>
</build>

</project>
Loading