mybatisPlus分页方言设置错误问题 mybatisPlus对于Oceanbase的Oracle租户分页识别错误

问题描述

Oceanbase的Oracle租户时分页报错不认识Limit,由于Limit是mysql的分页形式,这里认为应该是Oceanbase的Oracle租户被错误的识别成了mysql方言


原因分析:

观察pageInteceptor拦截器配置

java 复制代码
@Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        //多数据源不能写死DbType
        // paginationInnerInterceptor.setDbType(DbType.ORACLE);
        paginationInnerInterceptor.setOverflow(false);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        return interceptor;
    }

发现使用的拦截器PaginationInnerInterceptor中配置获取方言的逻辑为

java 复制代码
    /**
     * 获取分页方言类的逻辑
     *
     * @param executor Executor
     * @return 分页方言类
     */
    protected IDialect findIDialect(Executor executor) {
        if (dialect != null) {
            return dialect;
        }
        if (dbType != null) {
            dialect = DialectFactory.getDialect(dbType);
            return dialect;
        }
        return DialectFactory.getDialect(JdbcUtils.getDbType(executor));
    }

其中DialectFactory.getDialect方法为

java 复制代码
public static IDialect getDialect(DbType dbType) {
        IDialect dialect = DIALECT_ENUM_MAP.get(dbType);
        if (null == dialect) {
            if (dbType == DbType.OTHER) {
                throw ExceptionUtils.mpe("%s database not supported.", dbType.getDb());
            }
            // mysql same type
            else if (dbType == DbType.MYSQL
                || dbType == DbType.MARIADB
                || dbType == DbType.GBASE
                || dbType == DbType.OSCAR
                || dbType == DbType.XU_GU
                || dbType == DbType.CLICK_HOUSE
                || dbType == DbType.OCEAN_BASE
                || dbType == DbType.CUBRID
                || dbType == DbType.SUNDB) {
                dialect = new MySqlDialect();
            }
            // oracle same type
            else if (dbType == DbType.ORACLE
                || dbType == DbType.DM
                || dbType == DbType.GAUSS) {
                dialect = new OracleDialect();
            }
            // postgresql same type
            else if (dbType == DbType.POSTGRE_SQL
                || dbType == DbType.H2
                || dbType == DbType.LEALONE
                || dbType == DbType.SQLITE
                || dbType == DbType.HSQL
                || dbType == DbType.KINGBASE_ES
                || dbType == DbType.PHOENIX
                || dbType == DbType.SAP_HANA
                || dbType == DbType.IMPALA
                || dbType == DbType.HIGH_GO
                || dbType == DbType.VERTICA
                || dbType == DbType.REDSHIFT
                || dbType == DbType.OPENGAUSS
                || dbType == DbType.TDENGINE
                || dbType == DbType.UXDB
                || dbType == DbType.GBASE8S_PG
                || dbType == DbType.GBASE_8C) {
                dialect = new PostgreDialect();
            }
            // other types
            else if (dbType == DbType.ORACLE_12C
                || dbType == DbType.FIREBIRD
                || dbType == DbType.SQL_SERVER) {
                dialect = new Oracle12cDialect();
            } else if (dbType == DbType.DB2) {
                dialect = new DB2Dialect();
            } else if (dbType == DbType.SQL_SERVER2005) {
                dialect = new SQLServer2005Dialect();
            } else if (dbType == DbType.SYBASE) {
                dialect = new SybaseDialect();
            } else if (dbType == DbType.XCloud) {
                dialect = new XCloudDialect();
            } else if (dbType == DbType.GBASE_8S
                || dbType == DbType.GBASEDBT
                || dbType == DbType.GBASE_INFORMIX
                || dbType == DbType.SINODB) {
                dialect = new GBase8sDialect();
            } else if (dbType == DbType.INFORMIX) {
                dialect = new InformixDialect();
            } else if (dbType == DbType.TRINO
                || dbType == DbType.PRESTO) {
                dialect = new TrinoDialect();
            }
            DIALECT_ENUM_MAP.put(dbType, dialect);
        }
        return dialect;
    }

此时发现oceanbase的方言设置为mysql

并未区分mysql租户和oracle租户


解决方案:

设置一个自定义拦截器MybatisPlusInterceptor

java 复制代码
package com.xquant.xtam.common.datasource.support;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.DialectFactory;
import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.IDialect;
import com.baomidou.mybatisplus.extension.toolkit.JdbcUtils;
import org.apache.ibatis.executor.Executor;

public class CustomPaginationInnerInterceptor extends PaginationInnerInterceptor {
    /**
     * 获取分页方言类的逻辑
     *
     * @param executor Executor
     * @return 分页方言类
     */
    @Override
    protected IDialect findIDialect(Executor executor) {
        if (this.getDialect() != null) {
            return this.getDialect();
        }
        if (this.getDialect() != null) {
            this.setDialect(DialectFactory.getDialect(this.getDbType()));
            return this.getDialect();
        }
        // 这个地方特殊处理一下,认为 OceanBase 是 Oracle,无法处理 OceanBase 的Oracle租户
        DbType dbType = JdbcUtils.getDbType(executor);
        if (dbType == DbType.OCEAN_BASE) {
            dbType = DbType.ORACLE;
        }
        return DialectFactory.getDialect(dbType);
    }
}

也可以通过修改jdbc工具类,或者自定义方言形式去添加一些小众,不识别的方言配置

相关推荐
侠客行031718 小时前
Mybatis连接池实现及池化模式
java·mybatis·源码阅读
蛇皮划水怪18 小时前
深入浅出LangChain4J
java·langchain·llm
剩下了什么19 小时前
MySQL JSON_SET() 函数
数据库·mysql·json
山峰哥19 小时前
数据库工程与SQL调优——从索引策略到查询优化的深度实践
数据库·sql·性能优化·编辑器
较劲男子汉20 小时前
CANN Runtime零拷贝传输技术源码实战 彻底打通Host与Device的数据传输壁垒
运维·服务器·数据库·cann
老毛肚20 小时前
MyBatis体系结构与工作原理 上篇
java·mybatis
java搬砖工-苤-初心不变20 小时前
MySQL 主从复制配置完全指南:从原理到实践
数据库·mysql
风流倜傥唐伯虎20 小时前
Spring Boot Jar包生产级启停脚本
java·运维·spring boot
Yvonne爱编码20 小时前
JAVA数据结构 DAY6-栈和队列
java·开发语言·数据结构·python
Re.不晚20 小时前
JAVA进阶之路——无奖问答挑战1
java·开发语言