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工具类,或者自定义方言形式去添加一些小众,不识别的方言配置

相关推荐
紫金修道1 小时前
【DeepAgent】概述
开发语言·数据库·python
Via_Neo1 小时前
JAVA中以2为底的对数表示方式
java·开发语言
孟章豪2 小时前
《SQL拼接 vs 参数化,为什么公司禁止拼接SQL?(附真实案例)》
服务器·数据库·sql
荒川之神2 小时前
ORACLE LEVEL函数练习
数据库·oracle
·云扬·2 小时前
【MySQL】实战:用pt-table-sync修复主从数据一致性问题
数据库·mysql·ffmpeg
野生技术架构师2 小时前
一线大厂Java面试八股文全栈通关手册(含源码级详解)
java·开发语言·面试
廋到被风吹走3 小时前
【AI】Codex 多语言实测:Python/Java/JS/SQL 效果横评
java·人工智能·python
swIn KWAL3 小时前
【MySQL】环境变量配置
数据库·mysql·adb
shark22222223 小时前
【JOIN】关键字在MySql中的详细使用
数据库·mysql
RATi GORI3 小时前
MySQL中的CASE WHEN语句:用法、示例与解析
android·数据库·mysql