当前位置: 首页 > news >正文

SpringBoot+Mybatis 分页

无论多数据源,还是单数据源,分页都一样,刚开始出了点错,是因为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();
//    }
}

http://www.lryc.cn/news/416966.html

相关文章:

  • 学习进行到了第十七天(2024.8.5)
  • 【Nuxt】Layout 布局和渲染模式
  • C:指针学习(1)-学习笔记
  • 【LVS】负载均衡之NAT模式
  • ASP.NET Core 基础 - 入门实例
  • 机器人主板维修|ABB机械手主板元器件故障
  • 大数据Flink(一百零六):什么是阿里云实时计算Flink版
  • ERCOT中的专业术语解释
  • Python酷库之旅-第三方库Pandas(069)
  • 基于hutools的国密SM2、3、4
  • 进程的等待(非阻塞轮询+阻塞)和替换控制详解
  • 24/8/6算法笔记 支持向量机
  • 测试用例等级划分
  • 打造Perl编译器前端:自定义语言处理的魔法
  • Visual Studio 和 Visual Studio Code 的比较与应用偏向
  • Python打开JSON/CSV文件的正确方式(针对UnicodeDecodeError)
  • 深入解析TikTok广告开户白名单:规范与申请指南
  • CSS技巧专栏:一日一例 19 -纯CSS实现超酷的水晶按钮特效
  • ArcGIS基础:基于数据图框实现地理坐标系下不同投影转换的可视化效果
  • ⚡4. Kubernetes核心资源管理操作实战
  • 【Wireshark 抓 CAN 总线】Wireshark 抓取 CAN 总线数据的实现思路
  • Linux网络编程3
  • gitlab 服务器安装
  • 【pytorch】全连接网络简单二次函数拟合
  • git提交到本地仓库了,怎么撤回
  • lua学习(1)
  • SQL报错注入之updatexml
  • 单元测试的重要性
  • mysql线上查询数据注意锁表问题
  • UE5 右键菜单缺少Generate Visual Studio project files