问题描述
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;
}
标签:YashanDB,url,contains,else,DbType,plus,import,Mybatis,return
From: https://www.cnblogs.com/YashanDB/p/18349144