Mybatis-Plus增加对国产数据库的支持
需要将mybatis-plus依赖包中的相关类反编译后替换。Mybatis-Plus不支持国产数据库。
·
以sundb8为例
目录
1、mybatis-plus-annotation-3.5.3.2.jar
2、mybatis-plus-extension-3.5.3.2.jar
问题描述
Mybatis-Plus不支持国产数据库
错误信息
Mybatis Plus Cannot Read Database type or The Database's Not Supported! The Database's IDialect Not Supported!
解决方案
需要将mybatis-plus依赖包中的相关类反编译后替换
1、mybatis-plus-annotation-3.5.3.2.jar
包名:com.baomidou.mybatisplus.annotation
类名:DbType
修改为:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.baomidou.mybatisplus.annotation;
public enum DbType {
MYSQL("mysql", "MySql数据库"),
MARIADB("mariadb", "MariaDB数据库"),
ORACLE("oracle", "Oracle11g及以下数据库(高版本推荐使用ORACLE_NEW)"),
ORACLE_12C("oracle12c", "Oracle12c+数据库"),
DB2("db2", "DB2数据库"),
H2("h2", "H2数据库"),
HSQL("hsql", "HSQL数据库"),
SQLITE("sqlite", "SQLite数据库"),
POSTGRE_SQL("postgresql", "Postgre数据库"),
SQL_SERVER2005("sqlserver2005", "SQLServer2005数据库"),
SQL_SERVER("sqlserver", "SQLServer数据库"),
DM("dm", "达梦数据库"),
XU_GU("xugu", "虚谷数据库"),
KINGBASE_ES("kingbasees", "人大金仓数据库"),
PHOENIX("phoenix", "Phoenix HBase数据库"),
GAUSS("zenith", "Gauss 数据库"),
CLICK_HOUSE("clickhouse", "clickhouse 数据库"),
GBASE("gbase", "南大通用(华库)数据库"),
GBASE_8S("gbase-8s", "南大通用数据库 GBase 8s"),
/** @deprecated */
@Deprecated
GBASEDBT("gbasedbt", "南大通用数据库"),
/** @deprecated */
@Deprecated
GBASE_INFORMIX("gbase 8s", "南大通用数据库 GBase 8s"),
SINODB("sinodb", "星瑞格数据库"),
OSCAR("oscar", "神通数据库"),
SYBASE("sybase", "Sybase ASE 数据库"),
OCEAN_BASE("oceanbase", "OceanBase 数据库"),
FIREBIRD("Firebird", "Firebird 数据库"),
HIGH_GO("highgo", "瀚高数据库"),
CUBRID("cubrid", "CUBRID数据库"),
GOLDILOCKS("goldilocks", "GOLDILOCKS数据库"),
CSIIDB("csiidb", "CSIIDB数据库"),
SAP_HANA("hana", "SAP_HANA数据库"),
IMPALA("impala", "impala数据库"),
VERTICA("vertica", "vertica数据库"),
XCloud("xcloud", "行云数据库"),
REDSHIFT("redshift", "亚马逊redshift数据库"),
OPENGAUSS("openGauss", "华为 opengauss 数据库"),
TDENGINE("TDengine", "TDengine数据库"),
INFORMIX("informix", "Informix数据库"),
UXDB("uxdb", "优炫数据库"),
LEALONE("lealone", "Lealone数据库"),
SUNDB("SUNDB","SUNDB数据库"),
OTHER("other", "其他数据库");
private final String db;
private final String desc;
public static DbType getDbType(String dbType) {
DbType[] var1 = values();
int var2 = var1.length;
for(int var3 = 0; var3 < var2; ++var3) {
DbType type = var1[var3];
if (type.db.equalsIgnoreCase(dbType)) {
return type;
}
}
return OTHER;
}
public String getDb() {
return this.db;
}
public String getDesc() {
return this.desc;
}
private DbType(final String db, final String desc) {
this.db = db;
this.desc = desc;
}
}
2、mybatis-plus-extension-3.5.3.2.jar
2.1增加类
2.1.1SUNDBKeyGenerator
包名:com.baomidou.mybatisplus.extension.incrementer
类名:SUNDBKeyGenerator
package com.baomidou.mybatisplus.extension.incrementer;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
/**
* @version V1.0
* @Description: (用一句话描述该文件做什么)
* @author: qixiongfei
* @date: 2024/5/21 16:50
*/
public class SUNDBKeyGenerator implements IKeyGenerator {
public SUNDBKeyGenerator() {
}
public String executeSql(String incrementerName) {
return "SELECT " + incrementerName + ".NEXTVAL FROM DUAL";
}
public DbType dbType() {
return DbType.SUNDB;
}
}
2.1.2SUNDBDialect
包名:com.baomidou.mybatisplus.extension.plugins.pagination.dialects
类名:SUNDBDialect
package com.baomidou.mybatisplus.extension.plugins.pagination.dialects;
/**
* @version V1.0
* @Description: (用一句话描述该文件做什么)
* @author: qixiongfei
* @date: 2024/5/21 16:43
*/
public class SUNDBDialect extends OracleDialect {
}
2.2修改类
2.2.1DialectFactory
包名:com.baomidou.mybatisplus.extension.plugins.pagination
类名:DialectFactory
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.baomidou.mybatisplus.extension.plugins.pagination;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.*;
import java.util.EnumMap;
import java.util.Map;
public class DialectFactory {
private static final Map<DbType, IDialect> DIALECT_ENUM_MAP = new EnumMap(DbType.class);
public DialectFactory() {
}
public static IDialect getDialect(DbType dbType) {
IDialect dialect = (IDialect)DIALECT_ENUM_MAP.get(dbType);
if (null == dialect) {
if (dbType == DbType.OTHER) {
throw ExceptionUtils.mpe("%s database not supported.", new Object[]{dbType.getDb()});
}
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.GOLDILOCKS && dbType != DbType.CSIIDB) {
if (dbType != DbType.ORACLE && dbType != DbType.DM && dbType != DbType.GAUSS) {
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) {
if (dbType != DbType.ORACLE_12C && dbType != DbType.FIREBIRD && dbType != DbType.SQL_SERVER) {
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) {
if (dbType == DbType.INFORMIX) {
dialect = new InformixDialect();
}
} else {
dialect = new GBase8sDialect();
}
} else {
dialect = new Oracle12cDialect();
}
} else {
dialect = new PostgreDialect();
}
} else {
dialect = new OracleDialect();
}
} else {
dialect = new MySqlDialect();
}
if(dbType == DbType.SUNDB){
dialect = new SUNDBDialect();
}
DIALECT_ENUM_MAP.put(dbType, dialect);
}
return (IDialect)dialect;
}
}
2.2.2JdbcUtils
包名:com.baomidou.mybatisplus.extension.toolkit
类名:JdbcUtils
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
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.CollectionUtils;
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.function.Function;
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)CollectionUtils.computeIfAbsent(JDBC_DB_TYPE_CACHE, 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 (url.contains(":lealone:")) {
return DbType.LEALONE;
} 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(":ch:") && !url.contains(":clickhouse:")) {
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 if (url.contains(":redshift:")) {
return DbType.REDSHIFT;
} else if (url.contains(":opengauss:")) {
return DbType.OPENGAUSS;
} else if (url.contains(":sundb:")) {
return DbType.SUNDB;
} else if (!url.contains(":taos:") && !url.contains(":taos-rs:")) {
if (url.contains(":informix")) {
return DbType.INFORMIX;
} else if (url.contains(":sinodb")) {
return DbType.SINODB;
} else if (url.contains(":uxdb:")) {
return DbType.UXDB;
} 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.TDENGINE;
}
} else {
return DbType.CLICK_HOUSE;
}
} 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();
}
}
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献3条内容
所有评论(0)