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

相关推荐
NineData3 小时前
NineData 迁移评估功能正式上线
数据库·dba
怒放吧德德5 小时前
Netty 4.2 入门指南:从概念到第一个程序
java·后端·netty
雨中飘荡的记忆7 小时前
大流量下库存扣减的数据库瓶颈:Redis分片缓存解决方案
java·redis·后端
NineData8 小时前
数据库迁移总踩坑?用 NineData 迁移评估,提前识别所有兼容性风险
数据库·程序员·云计算
心之语歌9 小时前
基于注解+拦截器的API动态路由实现方案
java·后端
赵渝强老师10 小时前
【赵渝强老师】PostgreSQL中表的碎片
数据库·postgresql
华仔啊11 小时前
Stream 代码越写越难看?JDFrame 让 Java 逻辑回归优雅
java·后端
ray_liang11 小时前
用六边形架构与整洁架构对比是伪命题?
java·架构
Ray Liang12 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
Java水解12 小时前
Java 中间件:Dubbo 服务降级(Mock 机制)
java·后端