問題描述
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;
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.ORACLE));
return interceptor;
}