mybatisPlus分页方言设置错误问题 mybatisPlus对于Oceanbase的Oracle租户分页识别错误
问题描述
Oceanbase的Oracle租户时分页报错不认识Limit,由于Limit是mysql的分页形式,这里认为应该是Oceanbase的Oracle租户被错误的识别成了mysql方言
原因分析:
观察pageInteceptor拦截器配置
@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();//多数据源不能写死DbType// paginationInnerInterceptor.setDbType(DbType.ORACLE);paginationInnerInterceptor.setOverflow(false);interceptor.addInnerInterceptor(paginationInnerInterceptor);return interceptor;}
发现使用的拦截器PaginationInnerInterceptor中配置获取方言的逻辑为
/*** 获取分页方言类的逻辑** @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方法为
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 typeelse 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 typeelse if (dbType == DbType.ORACLE|| dbType == DbType.DM|| dbType == DbType.GAUSS) {dialect = new OracleDialect();}// postgresql same typeelse 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 typeselse 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
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 分页方言类*/@Overrideprotected 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工具类,或者自定义方言形式去添加一些小众,不识别的方言配置