写点什么

【YashanDB 数据库】Mybatis-plus 分页框架识别不到 Yashandb

作者:YashanDB
  • 2024-08-08
    广东
  • 本文字数:2972 字

    阅读完需:约 10 分钟

问题描述

Mybatis-plus 无法识别 Yashandb 数据库,应用有如下报错。

问题分析

从 Mybatis-plus 源码里面看到,getDBtype 函数是没有 Yashandb 的方言。

当 Yashandb 使用 mybatis-plus 分页时候,会抛出异常即 other database not supported。

package com.baomidou.mybatisplus.extension.toolkit; import com.baomidou.mybatisplus.annotation.DbType;import com.baomidou.mybatisplus.core.toolkit.Assert;import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;import com.baomidou.mybatisplus.core.toolkit.StringUtils;import java.sql.Connection;import java.sql.SQLException;import java.util.Map;import java.util.concurrent.ConcurrentHashMap;import java.util.regex.Pattern;import org.apache.ibatis.executor.Executor;import org.apache.ibatis.logging.Log;import org.apache.ibatis.logging.LogFactory; public class JdbcUtils {    private static final Log logger = LogFactory.getLog(JdbcUtils.class);    private static final Map<String, DbType> JDBC_DB_TYPE_CACHE = new ConcurrentHashMap();     public JdbcUtils() {    }     public static DbType getDbType(Executor executor) {        try {            Connection conn = executor.getTransaction().getConnection();            return (DbType)JDBC_DB_TYPE_CACHE.computeIfAbsent(conn.getMetaData().getURL(), JdbcUtils::getDbType);        } catch (SQLException var2) {            throw ExceptionUtils.mpe(var2);        }    }     public static DbType getDbType(String jdbcUrl) {        Assert.isFalse(StringUtils.isBlank(jdbcUrl), "Error: The jdbcUrl is Null, Cannot read database type", new Object[0]);        String url = jdbcUrl.toLowerCase();        if (!url.contains(":mysql:") && !url.contains(":cobar:")) {            if (url.contains(":mariadb:")) {                return DbType.MARIADB;            } else if (url.contains(":oracle:")) {                return DbType.ORACLE;            } else if (!url.contains(":sqlserver:") && !url.contains(":microsoft:")) {                if (url.contains(":sqlserver2012:")) {                    return DbType.SQL_SERVER;                } else if (url.contains(":postgresql:")) {                    return DbType.POSTGRE_SQL;                } else if (url.contains(":hsqldb:")) {                    return DbType.HSQL;                } else if (url.contains(":db2:")) {                    return DbType.DB2;                } else if (url.contains(":sqlite:")) {                    return DbType.SQLITE;                } else if (url.contains(":h2:")) {                    return DbType.H2;                } else if (regexFind(":dm\\d*:", url)) {                    return DbType.DM;                } else if (url.contains(":xugu:")) {                    return DbType.XU_GU;                } else if (regexFind(":kingbase\\d*:", url)) {                    return DbType.KINGBASE_ES;                } else if (url.contains(":phoenix:")) {                    return DbType.PHOENIX;                } else if (url.contains(":zenith:")) {                    return DbType.GAUSS;                } else if (url.contains(":gbase:")) {                    return DbType.GBASE;                } else if (!url.contains(":gbasedbt-sqli:") && !url.contains(":informix-sqli:")) {                    if (url.contains(":clickhouse:")) {                        return DbType.CLICK_HOUSE;                    } else if (url.contains(":oscar:")) {                        return DbType.OSCAR;                    } else if (url.contains(":sybase:")) {                        return DbType.SYBASE;                    } else if (url.contains(":oceanbase:")) {                        return DbType.OCEAN_BASE;                    } else if (url.contains(":highgo:")) {                        return DbType.HIGH_GO;                    } else if (url.contains(":cubrid:")) {                        return DbType.CUBRID;                    } else if (url.contains(":goldilocks:")) {                        return DbType.GOLDILOCKS;                    } else if (url.contains(":csiidb:")) {                        return DbType.CSIIDB;                    } else if (url.contains(":sap:")) {                        return DbType.SAP_HANA;                    } else if (url.contains(":impala:")) {                        return DbType.IMPALA;                    } else if (url.contains(":vertica:")) {                        return DbType.VERTICA;                    } else if (url.contains(":xcloud:")) {                        return DbType.XCloud;                    } else if (url.contains(":firebirdsql:")) {                        return DbType.FIREBIRD;                    } else {                        logger.warn("The jdbcUrl is " + jdbcUrl + ", Mybatis Plus Cannot Read Database type or The Database's Not Supported!");                        return DbType.OTHER;                    }                } else {                    return DbType.GBASE_8S;                }            } else {                return DbType.SQL_SERVER2005;            }        } else {            return DbType.MYSQL;        }    }     public static boolean regexFind(String regex, CharSequence input) {        return null == input ? false : Pattern.compile(regex).matcher(input).find();    }}
复制代码

解决办法

办法 1:

添加 java 配置类,业务系统指定 ORM 框架需要使用 Oracle(MySQL)方言

package com.sics.config;import com.baomidou.mybatisplus.annotation.DbType;import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration; @Configurationpublic class MybatisPlusConfig {    @Bean    public MybatisPlusInterceptor mybatisPlusInterceptor() {        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.ORACLE));        return interceptor;    }
复制代码


用户头像

YashanDB

关注

全自研国产新型大数据管理系统 2022-02-15 加入

还未添加个人简介

评论

发布
暂无评论
【YashanDB数据库】Mybatis-plus分页框架识别不到Yashandb_yashandb_YashanDB_InfoQ写作社区