无论多数据源,还是单数据源,分页都一样,刚开始出了点错,是因为PageHelper的版本问题
这里用的SpringBoot3 SpringBoot2应该是没有问题的
相关代码
dynamic-datasource+Mybatis多数据源使用-CSDN博客
依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.2.8</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>dynamic-datasource</artifactId><version>0.0.1-SNAPSHOT</version><name>dynamic-datasource</name><description>Demo project for Spring Boot</description><url/><licenses><license/></licenses><developers><developer/></developers><scm><connection/><developerConnection/><tag/><url/></scm><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>3.0.3</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.33</version></dependency><!--dynamic-datasource --><dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot3-starter</artifactId><version>4.3.1</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.21</version></dependency><!--分页 低版本要自己额外配置下 -->
<!-- <dependency>-->
<!-- <groupId>com.github.pagehelper</groupId>-->
<!-- <artifactId>pagehelper-spring-boot-starter</artifactId>-->
<!-- <version>1.3.1</version>-->
<!-- </dependency>--><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>2.1.0</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>
PageHelper配置
#PageHelper 配置 #推荐明确指定数据库方言 pagehelper.helperDialect=mysql #合理化分页参数,避免异常页码导致的错误 启用后,当 pageNum 小于等于 0 时,会自动查询第一页的数据;当 pageNum 大于总页数时,会自动查询最后一页的数据。 pagehelper.reasonable=true #支持通过方法参数传递分页参数 PageHelper.startPage(1,2) pagehelper.supportMethodsArguments=true #自动生成 count 查询语句用于计算总记录数(默认配置) pagehelper.params=count=countSql #在运行时自动识别和设置数据库方言 #pagehelper.autoRuntimeDialect=true
持久层 PageHelper会拦截sql 拼接 select * from xxx limit (pageNum-1)*pageSize,pageSize
如果非要在SpringBoot3使用低版本PageHelper
package com.example.dynamicdatasource.config;import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.sql.DataSource;
import java.util.Properties;/*** @author hrui* @date 2024/8/6 21:03*/
@Configuration
public class MyBatisConfig {/*因使用的SpringBoot3 低版本PageHelp要额外配置 application.properties中的pageHelp无法set到参数 高版本没有问题 不需要设置*/
// @Bean
// public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
// org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
// SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
// factoryBean.setDataSource(dataSource);
//
// // 添加PageHelper分页插件
// PageInterceptor pageInterceptor = new PageInterceptor();
// Properties properties = new Properties();
// properties.setProperty("helperDialect", "mysql");
// properties.setProperty("reasonable", "true");
// properties.setProperty("supportMethodsArguments", "true");
// properties.setProperty("params", "count=countSql");
// pageInterceptor.setProperties(properties);
//
// configuration.addInterceptor(pageInterceptor);
// factoryBean.setConfiguration(configuration);
// return factoryBean.getObject();
// }
}