mybatis-plus分页插件使用

mybatis-plus分页插件

依赖版本

java 复制代码
  		<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>3.5.1</version>
        </dependency>

配置分页插件

java 复制代码
 @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

注册插件

java 复制代码
	@Bean
    public 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()]);
    }

自定义插件

java 复制代码
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 {

    @Override
    public 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();
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
        // 如果需要从配置中获取参数,可以在这里处理
    }
}
相关推荐
失足成万古风流人物4 小时前
面试官问MyBatis/OpenFeign的原理?我手搓了个MyHttp怼回去!(反八股版)
mybatis·springboot·openfeign·动态代理
fanruitian4 小时前
springboot-mybatisplus-demo
spring boot·后端·mybatis·mybatisplus
invicinble20 小时前
spring相关系统性理解,企业级应用
java·spring·mybatis
gAlAxy...1 天前
MyBatis 核心配置文件 SqlMapConfig.xml 全解析
xml·mybatis
2501_916766541 天前
【Mybatis】延迟加载与多级缓存
缓存·mybatis
YDS8292 天前
MyBatis-Plus精讲 —— 从快速入门到项目实战
java·后端·spring·mybatis·mybatis-plus
库库林_沙琪马2 天前
7、集成MyBatis
spring boot·mybatis
2501_916766542 天前
【Mybatis】注解开发与事务
mybatis
少年攻城狮2 天前
Mybatis-Plus系列---【自定义拦截器实现sql完整拼接及耗时打印】
数据库·sql·mybatis
清晓粼溪2 天前
Mybatis02:核心功能
java·mybatis