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

mybatis-plus分页插件使用

mybatis-plus分页插件

  • 依赖版本
  • 配置分页插件
  • 注册插件
  • 自定义插件

依赖版本

  		<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus</artifactId><version>3.5.1</version></dependency>

配置分页插件

 @Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return interceptor;}

注册插件

	@Beanpublic SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();sqlSessionFactory.setDataSource(dataSource);sqlSessionFactory.setMapperLocations(resolveMapperLocations());MybatisConfiguration configuration = new MybatisConfiguration();configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);configuration.setJdbcTypeForNull(JdbcType.NULL);configuration.setMapUnderscoreToCamelCase(true);//注册分页插件、有其他自定义插件也可在此注册到configuration.addInterceptor(mybatisPlusInterceptor());sqlSessionFactory.setConfiguration(configuration);return sqlSessionFactory.getObject();}public Resource[] resolveMapperLocations() {ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();List<String> mapperLocations = new ArrayList<>();mapperLocations.add("classpath*:mybatis/mapper/*.xml");List<Resource> resources = new ArrayList();if (mapperLocations != null) {for (String mapperLocation : mapperLocations) {try {Resource[] mappers = resourceResolver.getResources(mapperLocation);System.out.println("mappers "+mappers.length);resources.addAll(Arrays.asList(mappers));} catch (IOException e) {// ignore}}}return resources.toArray(new Resource[resources.size()]);}

自定义插件

import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.plugin.*;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.sql.Connection;@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})
})
@Component // 确保它是一个Spring管理的Bean,以便在配置类中注入
public class CustomSqlInterceptor implements Interceptor {@Overridepublic Object intercept(Invocation invocation) throws Throwable {StatementHandler statementHandler = (StatementHandler) invocation.getTarget();BoundSql boundSql = statementHandler.getBoundSql();String originalSql = boundSql.getSql();// 这里是你的SQL修改逻辑,例如:String modifiedSql = originalSql + " LIMIT 10";// 通过反射将修改后的SQL设置回去Field sqlField = boundSql.getClass().getDeclaredField("sql");sqlField.setAccessible(true);sqlField.set(boundSql, modifiedSql);// 继续执行后续流程return invocation.proceed();}@Overridepublic Object plugin(Object target) {return Plugin.wrap(target, this);}@Overridepublic void setProperties(Properties properties) {// 如果需要从配置中获取参数,可以在这里处理}
}
http://www.dtcms.com/a/489993.html

相关文章:

  • 福建住房和城乡建设网站网站做提示框
  • 李宏毅机器学习笔记24
  • Leetcode每日一练--28
  • Vue Router 路由元信息(meta)详解
  • 列表标签之无序标签(本文为个人学习笔记,内容整理自哔哩哔哩UP主【非学者勿扰】的公开课程。 > 所有知识点归属原作者,仅作非商业用途分享)
  • sk13.【scikit-learn基础】-- 自定义模型与功能
  • (Spring)Spring Boot 中 @Valid 与全局异常处理器的联系详解
  • 数据库数据类型,数据值类型,字符串类型,日期类型详解
  • 怎么写网站规划方案买链接做网站 利润高吗
  • SAP MM物料主数据锁定及解锁接口分享
  • [FSCalendar] 可定制的iOS日历组件 | docs | Interface Builder
  • 中兴B860AV5.1-M2/B860AV5.2M_安卓9_S905L3SB_支持外置WIFI_线刷固件包
  • AI 模型部署体系全景:从 PyTorch 到 RKNN 的嵌入式类比解析
  • 全球汽车紧固件产业进入关键转型期,中国供应链加速融入世界市场
  • 17网站一起做网店下载自动发卡网站建设
  • PHP 类型比较
  • oracle:To_char
  • MySQL 数据库核心操作全解析:从创建到备份与连接管理
  • 环境函数 SYS_CONTEXT 在 DM8 与 Oracle 中的差异
  • 长春怎么做网站重庆网站建设aiyom
  • Linux中的管道与重定向:深入理解两者的本质区别
  • 上传OSS服务器图片文件视频(使用elemenplus上传组件)
  • 全面适配iOS 26液态玻璃,基于开源IM即时通讯框架MobileIMSDK:RainbowChat-iOS端v10.2发布
  • 【Kubernetes】常见面试题汇总(二十五)
  • 阿里云渠道商:哪些方法能给服务器加速?
  • 华为USG 6320之配置外网访问服务器NAT映射和NAT回流
  • 网站开发业务介绍深圳网站搭建找哪里
  • 常见的网站模板类型有哪些,新手该如何选择最合适的?
  • 【Linux】基础IO(一)Linux 文件操作从入门到实践:系统调用、文件描述符、重定向,为自定义Shell添加重定向
  • Docker 深度解析:从虚拟化到新一代应用构建、运行与交付