mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
feat: 新增注释配置。
This commit is contained in:
parent
e79cfea312
commit
1fbd11cfd8
@ -29,6 +29,7 @@ public class GlobalConfig {
|
||||
|
||||
// === 必须配置 ===
|
||||
|
||||
private final JavadocConfig javadocConfig;
|
||||
private final PackageConfig packageConfig;
|
||||
private final StrategyConfig strategyConfig;
|
||||
|
||||
@ -55,10 +56,15 @@ public class GlobalConfig {
|
||||
private boolean tableDefGenerateEnable;
|
||||
|
||||
public GlobalConfig() {
|
||||
this.javadocConfig = new JavadocConfig();
|
||||
this.packageConfig = new PackageConfig();
|
||||
this.strategyConfig = new StrategyConfig();
|
||||
}
|
||||
|
||||
public JavadocConfig getJavadocConfig() {
|
||||
return javadocConfig;
|
||||
}
|
||||
|
||||
public PackageConfig getPackageConfig() {
|
||||
return packageConfig;
|
||||
}
|
||||
|
||||
@ -0,0 +1,68 @@
|
||||
package com.mybatisflex.codegen.config;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 注释配置类。
|
||||
*
|
||||
* @author 王帅
|
||||
* @since 2023-05-17
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public class JavadocConfig {
|
||||
|
||||
/**
|
||||
* 作者。
|
||||
*/
|
||||
private String author = System.getProperty("user.name");
|
||||
|
||||
/**
|
||||
* 自。
|
||||
*/
|
||||
private Supplier<String> since = () -> DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDate.now());
|
||||
|
||||
/**
|
||||
* 表名格式化。
|
||||
*/
|
||||
private Function<String, String> tableRemarkFormat = Function.identity();
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public JavadocConfig setAuthor(String author) {
|
||||
this.author = author;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getSince() {
|
||||
return since.get();
|
||||
}
|
||||
|
||||
public JavadocConfig setSince(String since) {
|
||||
this.since = () -> since;
|
||||
return this;
|
||||
}
|
||||
|
||||
public JavadocConfig setSince(Supplier<String> since) {
|
||||
this.since = since;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String formatTableComment(String comment) {
|
||||
return tableRemarkFormat.apply(comment);
|
||||
}
|
||||
|
||||
public Function<String, String> getTableRemarkFormat() {
|
||||
return tableRemarkFormat;
|
||||
}
|
||||
|
||||
public JavadocConfig setTableRemarkFormat(Function<String, String> tableRemarkFormat) {
|
||||
this.tableRemarkFormat = tableRemarkFormat;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
@ -21,38 +21,47 @@ public class StrategyConfig {
|
||||
* 使用哪个模板引擎来生成代码。
|
||||
*/
|
||||
protected ITemplate templateEngine;
|
||||
|
||||
/**
|
||||
* 数据库表前缀,多个前缀用英文逗号(,) 隔开。
|
||||
*/
|
||||
private String tablePrefix;
|
||||
|
||||
/**
|
||||
* 逻辑删除的默认字段名称。
|
||||
*/
|
||||
private String logicDeleteColumn;
|
||||
|
||||
/**
|
||||
* 乐观锁的字段名称。
|
||||
*/
|
||||
private String versionColumn;
|
||||
|
||||
/**
|
||||
* 是否生成视图映射。
|
||||
*/
|
||||
private boolean generateForView;
|
||||
|
||||
/**
|
||||
* 是否覆盖之前生成的文件。
|
||||
*/
|
||||
private boolean overwriteEnable;
|
||||
|
||||
/**
|
||||
* 单独为某张表添加独立的配置。
|
||||
*/
|
||||
private Map<String, TableConfig> tableConfigMap;
|
||||
|
||||
/**
|
||||
* 设置某个列的全局配置。
|
||||
*/
|
||||
private Map<String, ColumnConfig> columnConfigMap;
|
||||
|
||||
/**
|
||||
* 生成那些表,白名单。
|
||||
*/
|
||||
private Set<String> generateTables;
|
||||
|
||||
/**
|
||||
* 不生成那些表,黑名单。
|
||||
*/
|
||||
|
||||
@ -41,7 +41,7 @@ public class Table {
|
||||
}
|
||||
|
||||
public String getRemarks() {
|
||||
return remarks;
|
||||
return globalConfig.getJavadocConfig().formatTableComment(remarks);
|
||||
}
|
||||
|
||||
public void setRemarks(String remarks) {
|
||||
@ -156,17 +156,6 @@ public class Table {
|
||||
return imports.stream().sorted(Comparator.naturalOrder()).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public String buildRemarks(){
|
||||
if (StringUtil.isBlank(remarks)){
|
||||
return "";
|
||||
}else {
|
||||
StringBuilder sb = new StringBuilder("/**\n")
|
||||
.append(" * ").append(remarks).append("\n")
|
||||
.append(" */");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public String getEntityJavaFileName() {
|
||||
String entityJavaFileName = name;
|
||||
String tablePrefix = globalConfig.getStrategyConfig().getTablePrefix();
|
||||
|
||||
@ -64,9 +64,10 @@ public class ControllerGenerator implements IGenerator {
|
||||
}
|
||||
|
||||
|
||||
Map<String, Object> params = new HashMap<>(3);
|
||||
Map<String, Object> params = new HashMap<>(4);
|
||||
params.put("table", table);
|
||||
params.put("packageConfig", packageConfig);
|
||||
params.put("javadocConfig", globalConfig.getJavadocConfig());
|
||||
params.put("controllerConfig", globalConfig.getControllerConfig());
|
||||
|
||||
strategyConfig.getTemplateEngine().generate(params, templatePath, controllerJavaFile);
|
||||
|
||||
@ -64,10 +64,11 @@ public class EntityGenerator implements IGenerator {
|
||||
}
|
||||
|
||||
|
||||
Map<String, Object> params = new HashMap<>(3);
|
||||
Map<String, Object> params = new HashMap<>(4);
|
||||
params.put("table", table);
|
||||
params.put("packageConfig", packageConfig);
|
||||
params.put("entityConfig", globalConfig.getEntityConfig());
|
||||
params.put("javadocConfig", globalConfig.getJavadocConfig());
|
||||
|
||||
strategyConfig.getTemplateEngine().generate(params, templatePath, entityJavaFile);
|
||||
}
|
||||
|
||||
@ -64,10 +64,11 @@ public class MapperGenerator implements IGenerator {
|
||||
}
|
||||
|
||||
|
||||
Map<String, Object> params = new HashMap<>(3);
|
||||
Map<String, Object> params = new HashMap<>(4);
|
||||
params.put("table", table);
|
||||
params.put("packageConfig", packageConfig);
|
||||
params.put("mapperConfig", globalConfig.getMapperConfig());
|
||||
params.put("javadocConfig", globalConfig.getJavadocConfig());
|
||||
|
||||
strategyConfig.getTemplateEngine().generate(params, templatePath, mapperJavaFile);
|
||||
}
|
||||
|
||||
@ -64,10 +64,11 @@ public class ServiceGenerator implements IGenerator {
|
||||
}
|
||||
|
||||
|
||||
Map<String, Object> params = new HashMap<>(3);
|
||||
Map<String, Object> params = new HashMap<>(4);
|
||||
params.put("table", table);
|
||||
params.put("packageConfig", packageConfig);
|
||||
params.put("serviceConfig", globalConfig.getServiceConfig());
|
||||
params.put("javadocConfig", globalConfig.getJavadocConfig());
|
||||
|
||||
strategyConfig.getTemplateEngine().generate(params, templatePath, serviceJavaFile);
|
||||
}
|
||||
|
||||
@ -64,9 +64,10 @@ public class ServiceImplGenerator implements IGenerator {
|
||||
}
|
||||
|
||||
|
||||
Map<String, Object> params = new HashMap<>(3);
|
||||
Map<String, Object> params = new HashMap<>(4);
|
||||
params.put("table", table);
|
||||
params.put("packageConfig", packageConfig);
|
||||
params.put("javadocConfig", globalConfig.getJavadocConfig());
|
||||
params.put("serviceImplConfig", globalConfig.getServiceImplConfig());
|
||||
|
||||
strategyConfig.getTemplateEngine().generate(params, templatePath, serviceImplJavaFile);
|
||||
|
||||
@ -64,9 +64,10 @@ public class TableDefGenerator implements IGenerator {
|
||||
}
|
||||
|
||||
|
||||
Map<String, Object> params = new HashMap<>(3);
|
||||
Map<String, Object> params = new HashMap<>(4);
|
||||
params.put("table", table);
|
||||
params.put("packageConfig", packageConfig);
|
||||
params.put("javadocConfig", globalConfig.getJavadocConfig());
|
||||
params.put("tableDefConfig", globalConfig.getTableDefConfig());
|
||||
|
||||
strategyConfig.getTemplateEngine().generate(params, templatePath, tableDefJavaFile);
|
||||
|
||||
@ -10,6 +10,12 @@ import org.springframework.stereotype.Controller;
|
||||
import #(controllerConfig.buildSuperClassImport())
|
||||
#end
|
||||
|
||||
/**
|
||||
* #(table.getRemarks()) 控制层。
|
||||
*
|
||||
* @author #(javadocConfig.getAuthor())
|
||||
* @since #(javadocConfig.getSince())
|
||||
*/
|
||||
#if(controllerConfig.restStyle)
|
||||
@RestController
|
||||
#else
|
||||
|
||||
@ -4,7 +4,12 @@ package #(packageConfig.entityPackage);
|
||||
import #(importClass);
|
||||
#end
|
||||
|
||||
#(table.buildRemarks())
|
||||
/**
|
||||
* #(table.getRemarks()) 实体类。
|
||||
*
|
||||
* @author #(javadocConfig.getAuthor())
|
||||
* @since #(javadocConfig.getSince())
|
||||
*/
|
||||
#(table.buildTableAnnotation())
|
||||
public class #(table.buildEntityClassName())#(table.buildExtends())#(table.buildImplements()) {
|
||||
#for(column: table.columns)
|
||||
|
||||
@ -3,6 +3,12 @@ package #(packageConfig.mapperPackage);
|
||||
import #(mapperConfig.buildSuperClassImport());
|
||||
import #(packageConfig.entityPackage).#(table.buildEntityClassName());
|
||||
|
||||
/**
|
||||
* #(table.getRemarks()) 映射层。
|
||||
*
|
||||
* @author #(javadocConfig.getAuthor())
|
||||
* @since #(javadocConfig.getSince())
|
||||
*/
|
||||
public interface #(table.buildMapperClassName()) extends #(mapperConfig.buildSuperClassName())<#(table.buildEntityClassName())> {
|
||||
|
||||
}
|
||||
|
||||
@ -3,6 +3,12 @@ package #(packageConfig.servicePackage);
|
||||
import #(serviceConfig.buildSuperClassImport());
|
||||
import #(packageConfig.entityPackage).#(table.buildEntityClassName());
|
||||
|
||||
/**
|
||||
* #(table.getRemarks()) 服务层。
|
||||
*
|
||||
* @author #(javadocConfig.getAuthor())
|
||||
* @since #(javadocConfig.getSince())
|
||||
*/
|
||||
public interface #(table.buildServiceClassName()) extends #(serviceConfig.buildSuperClassName())<#(table.buildEntityClassName())> {
|
||||
|
||||
}
|
||||
@ -6,6 +6,12 @@ import #(packageConfig.mapperPackage).#(table.buildMapperClassName());
|
||||
import #(packageConfig.servicePackage).#(table.buildServiceClassName());
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* #(table.getRemarks()) 服务层实现。
|
||||
*
|
||||
* @author #(javadocConfig.getAuthor())
|
||||
* @since #(javadocConfig.getSince())
|
||||
*/
|
||||
@Service
|
||||
public class #(table.buildServiceImplClassName()) extends #(serviceImplConfig.buildSuperClassName())<#(table.buildMapperClassName()), #(table.buildEntityClassName())> implements #(table.buildServiceClassName()) {
|
||||
|
||||
|
||||
@ -3,7 +3,12 @@ package #(packageConfig.tableDefPackage);
|
||||
import com.mybatisflex.core.query.QueryColumn;
|
||||
import com.mybatisflex.core.table.TableDef;
|
||||
|
||||
|
||||
/**
|
||||
* #(table.getRemarks()) 表定义层。
|
||||
*
|
||||
* @author #(javadocConfig.getAuthor())
|
||||
* @since #(javadocConfig.getSince())
|
||||
*/
|
||||
public class #(table.buildTableDefClassName()) extends TableDef {
|
||||
|
||||
public static final #(table.buildTableDefClassName()) #(table.name) = new #(table.buildTableDefClassName())("#(table.name)");
|
||||
|
||||
@ -22,6 +22,8 @@ import com.mybatisflex.codegen.config.TableConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public class GeneratorTest {
|
||||
|
||||
|
||||
@ -95,6 +97,14 @@ public class GeneratorTest {
|
||||
|
||||
GlobalConfig globalConfig = new GlobalConfig();
|
||||
|
||||
//用户信息表,用于存放用户信息。 -> 用户信息
|
||||
Function<String, String> format = (e) -> e.split(",")[0].replace("表", "");
|
||||
|
||||
//设置注解生成配置
|
||||
globalConfig.getJavadocConfig()
|
||||
.setAuthor("王帅")
|
||||
.setTableRemarkFormat(format);
|
||||
|
||||
//设置生成文件目录和根包
|
||||
globalConfig.getPackageConfig()
|
||||
.setSourceDir(System.getProperty("user.dir") + "/src/test/java")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user