init: 导入项目
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package com.ag.mybatisplusgenerator;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class MybatisPlusGeneratorApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(MybatisPlusGeneratorApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.ag.mybatisplusgenerator.base;
|
||||
|
||||
import com.ag.mybatisplusgenerator.config.DateTypeConvertHandler;
|
||||
import com.ag.mybatisplusgenerator.config.GeneratorConfig;
|
||||
import com.baomidou.mybatisplus.generator.AutoGenerator;
|
||||
import com.baomidou.mybatisplus.generator.config.*;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class BaseGenerator {
|
||||
|
||||
//生成策略
|
||||
private final StrategyConfig.Builder STRATEGY_CONFIG = GeneratorConfig.getSTRATEGY_CONFIG();
|
||||
|
||||
private final TemplateConfig TEMPLATE_CONFIG = GeneratorConfig.getTemplateConfig();
|
||||
|
||||
private final PackageConfig.Builder PACKAGE_CONFIG = GeneratorConfig.getPACKAGE_CONFIG();
|
||||
|
||||
private final AutoGenerator generator;
|
||||
|
||||
public BaseGenerator(String dataSourceUrl, String user, String password, String schema, String parent, String project) {
|
||||
DataSourceConfig.Builder builder = new DataSourceConfig
|
||||
.Builder(dataSourceUrl, user, password)
|
||||
.typeConvertHandler(new DateTypeConvertHandler(GeneratorConfig.getGlobalConfig(project).build()));
|
||||
if (!StringUtils.isEmpty(schema)) {
|
||||
builder.schema(schema);
|
||||
}
|
||||
generator = new AutoGenerator(builder.build());
|
||||
|
||||
|
||||
generator.global(GeneratorConfig.getGlobalConfig(project).build());
|
||||
|
||||
//自定义xml位置
|
||||
Map<OutputFile, String> pathInfo = new HashMap<>();
|
||||
String parentProjectSrc = System.getProperty("user.dir");
|
||||
String srcBase = "\\src\\main\\resources\\mapper";
|
||||
String src = parentProjectSrc.substring(0, parentProjectSrc.lastIndexOf("\\") + 1) + project + srcBase;
|
||||
pathInfo.put(OutputFile.xml, src);
|
||||
generator.packageInfo(PACKAGE_CONFIG.parent(parent)
|
||||
.pathInfo(pathInfo)
|
||||
.build());
|
||||
|
||||
|
||||
generator.template(TEMPLATE_CONFIG);
|
||||
}
|
||||
|
||||
public void run(String... tableNames) {
|
||||
generator.strategy(STRATEGY_CONFIG.addInclude(tableNames).build());
|
||||
generator.execute();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package com.ag.mybatisplusgenerator.config;
|
||||
|
||||
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
|
||||
import com.baomidou.mybatisplus.generator.config.po.TableField;
|
||||
import com.baomidou.mybatisplus.generator.config.rules.DateType;
|
||||
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
|
||||
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
|
||||
import com.baomidou.mybatisplus.generator.type.ITypeConvertHandler;
|
||||
import com.baomidou.mybatisplus.generator.type.TypeRegistry;
|
||||
|
||||
import java.sql.Types;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @description:
|
||||
* @author: fengjun
|
||||
* @time: 2023/1/5 13:37
|
||||
*/
|
||||
public class DateTypeConvertHandler implements ITypeConvertHandler {
|
||||
private final GlobalConfig globalConfig;
|
||||
|
||||
private final Map<Integer, IColumnType> typeMap = new HashMap<>();
|
||||
public DateTypeConvertHandler(GlobalConfig globalConfig) {
|
||||
this.globalConfig = globalConfig;
|
||||
// byte[]
|
||||
typeMap.put(Types.BINARY, DbColumnType.BYTE_ARRAY);
|
||||
typeMap.put(Types.BLOB, DbColumnType.BYTE_ARRAY);
|
||||
typeMap.put(Types.LONGVARBINARY, DbColumnType.BYTE_ARRAY);
|
||||
typeMap.put(Types.VARBINARY, DbColumnType.BYTE_ARRAY);
|
||||
//byte
|
||||
typeMap.put(Types.TINYINT, DbColumnType.BYTE);
|
||||
//long
|
||||
typeMap.put(Types.BIGINT, DbColumnType.LONG);
|
||||
//boolean
|
||||
typeMap.put(Types.BIT, DbColumnType.BOOLEAN);
|
||||
typeMap.put(Types.BOOLEAN, DbColumnType.BOOLEAN);
|
||||
//short
|
||||
typeMap.put(Types.SMALLINT, DbColumnType.SHORT);
|
||||
//string
|
||||
typeMap.put(Types.CHAR, DbColumnType.STRING);
|
||||
typeMap.put(Types.CLOB, DbColumnType.STRING);
|
||||
typeMap.put(Types.VARCHAR, DbColumnType.STRING);
|
||||
typeMap.put(Types.LONGVARCHAR, DbColumnType.STRING);
|
||||
typeMap.put(Types.LONGNVARCHAR, DbColumnType.STRING);
|
||||
typeMap.put(Types.NCHAR, DbColumnType.STRING);
|
||||
typeMap.put(Types.NCLOB, DbColumnType.STRING);
|
||||
typeMap.put(Types.NVARCHAR, DbColumnType.STRING);
|
||||
//date
|
||||
typeMap.put(Types.DATE, DbColumnType.DATE);
|
||||
//timestamp
|
||||
typeMap.put(Types.TIMESTAMP, DbColumnType.TIMESTAMP);
|
||||
//double
|
||||
typeMap.put(Types.FLOAT, DbColumnType.DOUBLE);
|
||||
typeMap.put(Types.REAL, DbColumnType.DOUBLE);
|
||||
//int
|
||||
typeMap.put(Types.INTEGER, DbColumnType.INTEGER);
|
||||
//bigDecimal
|
||||
typeMap.put(Types.NUMERIC, DbColumnType.BIG_DECIMAL);
|
||||
typeMap.put(Types.DECIMAL, DbColumnType.BIG_DECIMAL);
|
||||
//TODO 类型需要补充完整
|
||||
}
|
||||
@Override
|
||||
public IColumnType convert(GlobalConfig globalConfig, TypeRegistry typeRegistry, TableField.MetaInfo metaInfo) {
|
||||
int typeCode = metaInfo.getJdbcType().TYPE_CODE;
|
||||
switch (typeCode) {
|
||||
// TODO 需要增加类型处理,尚未补充完整
|
||||
case Types.BIT:
|
||||
return getBitType(metaInfo);
|
||||
case Types.DATE:
|
||||
return getDateType(metaInfo);
|
||||
case Types.TIME:
|
||||
return getTimeType(metaInfo);
|
||||
case Types.DECIMAL:
|
||||
case Types.NUMERIC:
|
||||
return getNumber(metaInfo);
|
||||
case Types.TIMESTAMP:
|
||||
return getTimestampType(metaInfo);
|
||||
default:
|
||||
return typeMap.getOrDefault(typeCode, DbColumnType.OBJECT);
|
||||
}
|
||||
}
|
||||
private IColumnType getBitType(TableField.MetaInfo metaInfo) {
|
||||
if (metaInfo.getLength() > 1) {
|
||||
return DbColumnType.BYTE_ARRAY;
|
||||
}
|
||||
return DbColumnType.BOOLEAN;
|
||||
}
|
||||
|
||||
private IColumnType getNumber(TableField.MetaInfo metaInfo) {
|
||||
if (metaInfo.getScale() > 0 || metaInfo.getLength() > 18) {
|
||||
return typeMap.get(metaInfo.getJdbcType().TYPE_CODE);
|
||||
} else if (metaInfo.getLength() > 9) {
|
||||
return DbColumnType.LONG;
|
||||
} else if (metaInfo.getLength() > 4) {
|
||||
return DbColumnType.INTEGER;
|
||||
} else {
|
||||
return DbColumnType.SHORT;
|
||||
}
|
||||
}
|
||||
|
||||
private IColumnType getDateType(TableField.MetaInfo metaInfo) {
|
||||
DbColumnType dbColumnType;
|
||||
DateType dateType = globalConfig.getDateType();
|
||||
switch (dateType) {
|
||||
case SQL_PACK:
|
||||
dbColumnType = DbColumnType.DATE_SQL;
|
||||
break;
|
||||
case TIME_PACK:
|
||||
dbColumnType = DbColumnType.LOCAL_DATE;
|
||||
break;
|
||||
default:
|
||||
dbColumnType = DbColumnType.DATE;
|
||||
}
|
||||
return dbColumnType;
|
||||
}
|
||||
|
||||
private IColumnType getTimeType(TableField.MetaInfo metaInfo) {
|
||||
DbColumnType dbColumnType;
|
||||
DateType dateType = globalConfig.getDateType();
|
||||
if (dateType == DateType.TIME_PACK) {
|
||||
dbColumnType = DbColumnType.LOCAL_TIME;
|
||||
} else {
|
||||
dbColumnType = DbColumnType.TIME;
|
||||
}
|
||||
return dbColumnType;
|
||||
}
|
||||
|
||||
private IColumnType getTimestampType(TableField.MetaInfo metaInfo) {
|
||||
DbColumnType dbColumnType;
|
||||
DateType dateType = globalConfig.getDateType();
|
||||
if (dateType == DateType.TIME_PACK) {
|
||||
dbColumnType = DbColumnType.LOCAL_DATE_TIME;
|
||||
}else if (dateType == DateType.ONLY_DATE){
|
||||
dbColumnType = DbColumnType.DATE;
|
||||
} else {
|
||||
dbColumnType = DbColumnType.TIMESTAMP;
|
||||
}
|
||||
return dbColumnType;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.ag.mybatisplusgenerator.config;
|
||||
|
||||
import com.baomidou.mybatisplus.generator.config.*;
|
||||
import com.baomidou.mybatisplus.generator.config.rules.DateType;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
public class GeneratorConfig {
|
||||
|
||||
|
||||
//生成策略
|
||||
private static StrategyConfig.Builder STRATEGY_CONFIG = new StrategyConfig.Builder();
|
||||
|
||||
private static GlobalConfig.Builder GLOBAL_CONFIG = new GlobalConfig.Builder()
|
||||
.disableOpenDir();
|
||||
|
||||
private static PackageConfig.Builder PACKAGE_CONFIG = new PackageConfig.Builder();
|
||||
|
||||
private static TemplateConfig TEMPLATE_CONFIG = new TemplateConfig.Builder()
|
||||
.disable(TemplateType.CONTROLLER)
|
||||
// .disable(TemplateType.SERVICE)
|
||||
// .disable(TemplateType.SERVICE_IMPL)
|
||||
.build();
|
||||
|
||||
|
||||
|
||||
public static StrategyConfig.Builder getSTRATEGY_CONFIG() {
|
||||
STRATEGY_CONFIG.mapperBuilder()
|
||||
//xml
|
||||
// .enableBaseResultMap()
|
||||
// .enableBaseColumnList()
|
||||
//mapper接口的注解
|
||||
.mapperAnnotation(Mapper.class);
|
||||
STRATEGY_CONFIG.entityBuilder()
|
||||
.enableTableFieldAnnotation()
|
||||
//覆盖
|
||||
.enableFileOverride();
|
||||
return STRATEGY_CONFIG;
|
||||
}
|
||||
|
||||
/**
|
||||
* MYBATIS-PLUS 生成方法
|
||||
*/
|
||||
public static GlobalConfig.Builder getGlobalConfig(String projectName) {
|
||||
String src;
|
||||
String parentProjectSrc = System.getProperty("user.dir");
|
||||
String srcBase = "\\src\\main\\java";
|
||||
src = parentProjectSrc.substring(0,parentProjectSrc.lastIndexOf("\\")+1)+projectName+srcBase;
|
||||
GLOBAL_CONFIG.outputDir(src);
|
||||
GLOBAL_CONFIG.dateType(DateType.ONLY_DATE);
|
||||
return GLOBAL_CONFIG;
|
||||
}
|
||||
|
||||
public static PackageConfig.Builder getPACKAGE_CONFIG() {
|
||||
return PACKAGE_CONFIG;
|
||||
}
|
||||
|
||||
public static TemplateConfig getTemplateConfig() {
|
||||
return TEMPLATE_CONFIG;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
|
||||
spring:
|
||||
datasource:
|
||||
driver-class-name: org.h2.Driver
|
||||
url: jdbc:h2:mem:test
|
||||
username: root
|
||||
password: test
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.ag.mybatisplusgenerator;
|
||||
|
||||
import com.ag.mybatisplusgenerator.base.BaseGenerator;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class MybatisPlusGeneratorApplicationTests {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 数据源地址
|
||||
*/
|
||||
String dataSourceUrl = "jdbc:mysql://122.5.19.222:3909/ag_devmysql?autoReconnect=true&serverTimezone=GMT%2B8&useSSL=false";
|
||||
/**
|
||||
* 数据源账号
|
||||
*/
|
||||
String user = "root";
|
||||
/**
|
||||
* 数据源密码
|
||||
*/
|
||||
String password ="agmysqlroot";
|
||||
/**
|
||||
* 父类包名
|
||||
*/
|
||||
String parent = "com.ag.example";
|
||||
|
||||
/**
|
||||
* 数据库用户名(若使用默认则置空)
|
||||
*/
|
||||
String schema ="";
|
||||
|
||||
/**
|
||||
* 父类包名
|
||||
*/
|
||||
String project = "IlogDynamicModule";
|
||||
|
||||
|
||||
/**
|
||||
* 执行 run
|
||||
*/
|
||||
@Test
|
||||
public void generator() {
|
||||
new BaseGenerator(dataSourceUrl,user,password,schema,parent,project)
|
||||
.run("web_bk_vbill_head");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user