Add support for kingbase.

This commit is contained in:
HanHuimin001 2019-10-18 11:16:55 +08:00
parent 91a922a017
commit 40b10d63d6
14 changed files with 464 additions and 0 deletions

View File

@ -71,6 +71,7 @@ ext {
"postgresql" : "org.postgresql:postgresql:9.4.1212", "postgresql" : "org.postgresql:postgresql:9.4.1212",
"oracle" : fileTree(dir: 'libs', includes: ['ojdbc-11.2.0.3-jdk16.jar']), "oracle" : fileTree(dir: 'libs', includes: ['ojdbc-11.2.0.3-jdk16.jar']),
"dm" : fileTree(dir: 'libs', includes: ["jdbcDriver-18.jar"]), "dm" : fileTree(dir: 'libs', includes: ["jdbcDriver-18.jar"]),
"kingbase" : fileTree(dir: 'libs', includes: ["kingbase8-8.2.0.jar"]),
"h2" : "com.h2database:h2:1.4.197", "h2" : "com.h2database:h2:1.4.197",
"mysql" : "mysql:mysql-connector-java:8.0.15", "mysql" : "mysql:mysql-connector-java:8.0.15",
"sqlite" : "org.xerial:sqlite-jdbc:3.27.2.1", "sqlite" : "org.xerial:sqlite-jdbc:3.27.2.1",

BIN
libs/kingbase8-8.2.0.jar Normal file

Binary file not shown.

View File

@ -78,6 +78,10 @@ public enum DbType {
* xugu * xugu
*/ */
XU_GU("xugu", "虚谷数据库", "com.baomidou.mybatisplus.extension.plugins.pagination.dialects.XuGuDialect"), XU_GU("xugu", "虚谷数据库", "com.baomidou.mybatisplus.extension.plugins.pagination.dialects.XuGuDialect"),
/**
* Kingbase
*/
KINGBASE_ES("kingbasees", "人大金仓数据库", "com.baomidou.mybatisplus.extension.plugins.pagination.dialects.KingbaseDialect"),
/** /**
* UNKONWN DB * UNKONWN DB
*/ */

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2011-2020, baomidou (jobob@qq.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.baomidou.mybatisplus.extension.incrementer;
import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
/**
* Kingbase Sequence
*
* @author kingbase
* @since 2019-10-17
*/
public class KingbaseKeyGenerator implements IKeyGenerator {
@Override
public String executeSql(String incrementerName) {
return "select nextval('" + incrementerName + "')";
}
}

View File

@ -0,0 +1,25 @@
/*
* Copyright (c) 2011-2020, baomidou (jobob@qq.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.baomidou.mybatisplus.extension.plugins.pagination.dialects;
/**
* Kingbase 数据库分页语句组装实现
*
* @author kingbase
* @since 2019-10-12
*/
public class KingbaseDialect extends PostgreDialect {
}

View File

@ -63,6 +63,8 @@ public class JdbcUtils {
return DbType.DM; return DbType.DM;
} else if (jdbcUrl.contains(":xugu:")) { } else if (jdbcUrl.contains(":xugu:")) {
return DbType.XU_GU; return DbType.XU_GU;
} else if (jdbcUrl.contains(":kingbase:") || jdbcUrl.contains(":kingbase8:")) {
return DbType.KINGBASE_ES;
} else { } else {
logger.warn("The jdbcUrl is " + jdbcUrl + ", Mybatis Plus Cannot Read Database type or The Database's Not Supported!"); logger.warn("The jdbcUrl is " + jdbcUrl + ", Mybatis Plus Cannot Read Database type or The Database's Not Supported!");
return DbType.OTHER; return DbType.OTHER;

View File

@ -9,6 +9,7 @@ dependencies {
testCompile "${lib.postgresql}" testCompile "${lib.postgresql}"
testCompile lib.oracle as ConfigurableFileTree testCompile lib.oracle as ConfigurableFileTree
testCompile lib.dm as ConfigurableFileTree testCompile lib.dm as ConfigurableFileTree
testCompile lib.kingbase as ConfigurableFileTree
testCompile "${lib.h2}" testCompile "${lib.h2}"
testCompile "${lib.mysql}" testCompile "${lib.mysql}"
testCompile "${lib.sqlite}" testCompile "${lib.sqlite}"

View File

@ -96,6 +96,9 @@ public class DataSourceConfig {
case DM: case DM:
dbQuery = new DMQuery(); dbQuery = new DMQuery();
break; break;
case KINGBASE_ES:
dbQuery = new KingbaseESQuery();
break;
default: default:
// 默认 MYSQL // 默认 MYSQL
dbQuery = new MySqlQuery(); dbQuery = new MySqlQuery();
@ -147,6 +150,8 @@ public class DataSourceConfig {
return DbType.MARIADB; return DbType.MARIADB;
} else if (str.contains("h2")) { } else if (str.contains("h2")) {
return DbType.H2; return DbType.H2;
} else if (str.contains("kingbase") || str.contains("kingbase8")) {
return DbType.KINGBASE_ES;
} else { } else {
return null; return null;
} }
@ -176,6 +181,9 @@ public class DataSourceConfig {
case MARIADB: case MARIADB:
typeConvert = new MySqlTypeConvert(); typeConvert = new MySqlTypeConvert();
break; break;
case KINGBASE_ES:
typeConvert = new KingbaseESTypeConvert();
break;
default: default:
// 默认 MYSQL // 默认 MYSQL
typeConvert = new MySqlTypeConvert(); typeConvert = new MySqlTypeConvert();

View File

@ -421,6 +421,15 @@ public class ConfigBuilder {
} }
tablesSql = String.format(tablesSql, schema); tablesSql = String.format(tablesSql, schema);
} }
if (DbType.KINGBASE_ES == dbQuery.dbType()) {
String schema = dataSourceConfig.getSchemaName();
if (schema == null) {
//kingbase 默认 schema=PUBLIC
schema = "PUBLIC";
dataSourceConfig.setSchemaName(schema);
}
tablesSql = String.format(tablesSql, schema);
}
if (DbType.DB2 == dbQuery.dbType()) { if (DbType.DB2 == dbQuery.dbType()) {
String schema = dataSourceConfig.getSchemaName(); String schema = dataSourceConfig.getSchemaName();
if (schema == null) { if (schema == null) {
@ -551,6 +560,8 @@ public class ConfigBuilder {
Set<String> h2PkColumns = new HashSet<>(); Set<String> h2PkColumns = new HashSet<>();
if (DbType.POSTGRE_SQL == dbType) { if (DbType.POSTGRE_SQL == dbType) {
tableFieldsSql = String.format(tableFieldsSql, dataSourceConfig.getSchemaName(), tableName); tableFieldsSql = String.format(tableFieldsSql, dataSourceConfig.getSchemaName(), tableName);
} else if (DbType.KINGBASE_ES == dbType) {
tableFieldsSql = String.format(tableFieldsSql, dataSourceConfig.getSchemaName(), tableName);
} else if (DbType.DB2 == dbType) { } else if (DbType.DB2 == dbType) {
tableFieldsSql = String.format(tableFieldsSql, dataSourceConfig.getSchemaName(), tableName); tableFieldsSql = String.format(tableFieldsSql, dataSourceConfig.getSchemaName(), tableName);
} else if (DbType.ORACLE == dbType) { } else if (DbType.ORACLE == dbType) {

View File

@ -0,0 +1,88 @@
/*
* Copyright (c) 2011-2020, baomidou (jobob@qq.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.baomidou.mybatisplus.generator.config.converts;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.ITypeConvert;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
/**
* KingbaseES 字段类型转换
*
* @author kingbase
* @since 2019-10-12
*/
public class KingbaseESTypeConvert implements ITypeConvert {
@Override
public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
String t = fieldType.toLowerCase();
if (t.contains("char")) {
return DbColumnType.STRING;
} else if (t.contains("bigint")) {
return DbColumnType.LONG;
} else if (t.contains("int")) {
return DbColumnType.INTEGER;
} else if (t.contains("date") || t.contains("time")) {
switch (globalConfig.getDateType()) {
case ONLY_DATE:
return DbColumnType.DATE;
case SQL_PACK:
switch (t) {
case "date":
return DbColumnType.DATE_SQL;
case "time":
return DbColumnType.TIME;
default:
return DbColumnType.TIMESTAMP;
}
case TIME_PACK:
switch (t) {
case "date":
return DbColumnType.LOCAL_DATE;
case "time":
return DbColumnType.LOCAL_TIME;
default:
return DbColumnType.LOCAL_DATE_TIME;
}
default:
return DbColumnType.DATE;
}
} else if (t.contains("text")) {
return DbColumnType.STRING;
} else if (t.contains("bit")) {
return DbColumnType.BOOLEAN;
} else if (t.contains("decimal")) {
return DbColumnType.BIG_DECIMAL;
} else if (t.contains("clob")) {
return DbColumnType.CLOB;
} else if (t.contains("blob")) {
return DbColumnType.BYTE_ARRAY;
} else if (t.contains("float")) {
return DbColumnType.FLOAT;
} else if (t.contains("double")) {
return DbColumnType.DOUBLE;
} else if (t.contains("json") || t.contains("enum")) {
return DbColumnType.STRING;
} else if (t.contains("boolean")) {
return DbColumnType.BOOLEAN;
} else if (t.contains("numeric")) {
return DbColumnType.BIG_DECIMAL;
}
return DbColumnType.STRING;
}
}

View File

@ -0,0 +1,84 @@
/*
* Copyright (c) 2011-2020, baomidou (jobob@qq.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.baomidou.mybatisplus.generator.config.querys;
import com.baomidou.mybatisplus.annotation.DbType;
/**
* KingbaseES 表数据查询
*
* @author kingbase
* @since 2019-10-12
*/
public class KingbaseESQuery extends AbstractDbQuery {
@Override
public DbType dbType() {
return DbType.KINGBASE_ES;
}
@Override
public String tablesSql() {
return "SELECT A.tablename, obj_description(relfilenode, 'sys_class') AS comments FROM sys_tables A, sys_class B WHERE A.schemaname='%s' AND A.tablename = B.relname";
}
@Override
public String tableFieldsSql() {
return "SELECT A.attname AS name, format_type(A.atttypid, A.atttypmod) AS type,col_description(A.attrelid, A.attnum) AS comment, (CASE C.contype WHEN 'p' THEN 'PRI' ELSE '' END) AS key " +
"FROM sys_attribute A LEFT JOIN sys_constraint C ON A.attnum = C.conkey[1] AND A.attrelid = C.conrelid " +
"WHERE A.attrelid = '%s.%s'::regclass AND A.attnum > 0 AND NOT A.attisdropped ORDER BY A.attnum";
}
@Override
public String tableName() {
return "tablename";
}
@Override
public String tableComment() {
return "comments";
}
@Override
public String fieldName() {
return "name";
}
@Override
public String fieldType() {
return "type";
}
@Override
public String fieldComment() {
return "comment";
}
@Override
public String fieldKey() {
return "key";
}
}

View File

@ -0,0 +1,170 @@
/*
* Copyright (c) 2011-2020, baomidou (jobob@qq.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.baomidou.mybatisplus.test.generator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.converts.OracleTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
/**
* KingbaseESGenerator
*
* @author kingbase
* @since 2019/10/12
*/
public class KingbaseESGenerator extends GeneratorTest {
public static void main(String[] args) {
int result = scanner();
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir("D://");
gc.setFileOverride(true);
gc.setActiveRecord(true);// 开启 activeRecord 模式
gc.setEnableCache(false);// XML 二级缓存
gc.setBaseResultMap(true);// XML ResultMap
gc.setBaseColumnList(false);// XML columList
//gc.setKotlin(true); // 是否生成 kotlin 代码
//gc.setSwagger2(true); // 是否生成 Swagger2 注解
gc.setAuthor("kingbase");
gc.setIdType(IdType.AUTO);
// 自定义文件命名注意 %s 会自动填充表实体属性
// gc.setEntityName("%sEntity");
// gc.setMapperName("%sDao");
// gc.setXmlName("%sDao");
// gc.setServiceName("MP%sService");
// gc.setServiceImplName("%sServiceDiy");
// gc.setControllerName("%sAction");
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setSchemaName("PUBLIC");// 指定 SCHEMA
dsc.setDbType(DbType.KINGBASE_ES);
dsc.setTypeConvert(new OracleTypeConvert() {
// 自定义数据库表字段类型转换可选
@Override
public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
System.out.println("转换类型:" + fieldType);
return super.processTypeConvert(globalConfig, fieldType);
}
});
// 自定义数据库信息查询
dsc.setDbQuery(new MyKingbaseESQuery());
dsc.setDriverName("com.kingbase8.Driver");
dsc.setUsername("SYSTEM");
dsc.setPassword("123456");
dsc.setUrl("jdbc:kingbase8://localhost:54321/mybatis-plus");
mpg.setDataSource(dsc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
// strategy.setCapitalMode(true);// 全局大写命名
// strategy.setDbColumnUnderline(true);// 全局下划线命名
strategy.setTablePrefix("BMD_", "MP_");// 表前缀
strategy.setFieldPrefix("A_");
strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
strategy.setColumnNaming(NamingStrategy.underline_to_camel);// 允许字段策略独立设置默认为 naming 策略
strategy.setInclude("T_USER", "^MP.*", "OK"); // 需要生成的表支持正则表达式
// strategy.setExclude("test"); // 排除生成的表支持正则表达式
// 自定义实体父类
// strategy.setSuperEntityClass("com.baomidou.demo.TestEntity");
// 自定义实体公共字段
// strategy.setSuperEntityColumns(new String[] { "test_id", "age" });
// 自定义 mapper 父类
// strategy.setSuperMapperClass("com.baomidou.demo.TestMapper");
// 自定义 service 父类
// strategy.setSuperServiceClass("com.baomidou.demo.TestService");
// 自定义 service 实现类父类
// strategy.setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl");
// 自定义 controller 父类
// strategy.setSuperControllerClass("com.baomidou.demo.TestController");
// 实体是否生成字段常量默认 false
// public static final String ID = "test_id";
// strategy.setEntityColumnConstant(true);
// 实体是否为构建者模型默认 false
// public User setName(String name) {this.name = name; return this;}
// strategy.setEntityBuliderModel(true);
mpg.setStrategy(strategy);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName("test");
pc.setParent("com.baomidou");// 自定义包路径
pc.setController("controller");// 这里是控制器包名默认 web
mpg.setPackageInfo(pc);
// 注入自定义配置可以在 VM 中使用 cfg.abc 设置的值
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
Map<String, Object> map = new HashMap<>();
map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
this.setMap(map);
}
};
List<FileOutConfig> focList = new ArrayList<>();
focList.add(new FileOutConfig("/templates/dto.java" + ((1 == result) ? ".ftl" : ".vm")) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输入文件名称
return "D://test/my_" + tableInfo.getEntityName() + StringPool.DOT_JAVA;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 自定义模板配置模板可以参考源码 /mybatis-plus/src/main/resources/template 使用 copy
// 至您项目 src/main/resources/template 目录下模板名称也可自定义如下配置
// TemplateConfig tc = new TemplateConfig();
// tc.setController("...");
// tc.setEntity("...");
// tc.setMapper("...");
// tc.setXml("...");
// tc.setService("...");
// tc.setServiceImpl("...");
// mpg.setTemplate(tc);
// 执行生成
if (1 == result) {
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
}
mpg.execute();
// 打印注入设置
System.err.println(mpg.getCfg().getMap().get("abc"));
}
}

View File

@ -0,0 +1,37 @@
/*
* Copyright (c) 2011-2020, baomidou (jobob@qq.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.baomidou.mybatisplus.test.generator;
import com.baomidou.mybatisplus.generator.config.querys.KingbaseESQuery;
public class MyKingbaseESQuery extends KingbaseESQuery {
@Override
public String tableFieldsSql() {
// 固定 abc def 内容实际可以查询字段大小等信息
return "SELECT 1 AS abc, 2 AS def, A.attname AS name, format_type(A.atttypid, A.atttypmod) AS type,col_description(A.attrelid, A.attnum) AS comment, (CASE C.contype WHEN 'p' THEN 'PRI' ELSE '' END) AS key " +
"FROM sys_attribute A LEFT JOIN sys_constraint C ON A.attnum = C.conkey[1] AND A.attrelid = C.conrelid " +
"WHERE A.attrelid = '%s.%s'::regclass AND A.attnum > 0 AND NOT A.attisdropped ORDER BY A.attnum";
}
@Override
public String[] fieldCustom() {
// 返回自定义查询字段
return new String[]{"abc", "def"};
}
}

View File

@ -43,6 +43,7 @@ class DbTypeTest {
DIALECT_MAP.put(DbType.SQLITE, SQLiteDialect.class); DIALECT_MAP.put(DbType.SQLITE, SQLiteDialect.class);
DIALECT_MAP.put(DbType.HSQL, HSQLDialect.class); DIALECT_MAP.put(DbType.HSQL, HSQLDialect.class);
DIALECT_MAP.put(DbType.XU_GU, XuGuDialect.class); DIALECT_MAP.put(DbType.XU_GU, XuGuDialect.class);
DIALECT_MAP.put(DbType.KINGBASE_ES, KingbaseDialect.class);
DIALECT_MAP.put(DbType.OTHER, UnknownDialect.class); DIALECT_MAP.put(DbType.OTHER, UnknownDialect.class);
} }