mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 01:28:24 +08:00
commit
6efa1f3231
@ -94,6 +94,13 @@
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.swagger</groupId>
|
||||
<artifactId>swagger-annotations</artifactId>
|
||||
<version>1.6.11</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
|
||||
@ -56,6 +56,11 @@ public class EntityConfig {
|
||||
*/
|
||||
private boolean withLombok;
|
||||
|
||||
/**
|
||||
* Entity 是否使用 Swagger 注解。
|
||||
*/
|
||||
private boolean withSwagger;
|
||||
|
||||
/**
|
||||
* 实体类数据源。
|
||||
*/
|
||||
@ -151,6 +156,21 @@ public class EntityConfig {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否启用 Swagger。
|
||||
*/
|
||||
public boolean isWithSwagger() {
|
||||
return withSwagger;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置是否启用 Swagger。
|
||||
*/
|
||||
public EntityConfig setWithSwagger(boolean withSwagger) {
|
||||
this.withSwagger = withSwagger;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取实体类数据源。
|
||||
*/
|
||||
|
||||
@ -282,6 +282,20 @@ public class GlobalConfig {
|
||||
getJavadocConfig().setTableCommentFormat(tableCommentFormat);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see JavadocConfig#getColumnCommentFormat()
|
||||
*/
|
||||
public Function<String, String> getColumnCommentFormat() {
|
||||
return getJavadocConfig().getColumnCommentFormat();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see JavadocConfig#setColumnCommentFormat(UnaryOperator)
|
||||
*/
|
||||
public void setColumnCommentFormat(UnaryOperator<String> columnCommentFormat) {
|
||||
getJavadocConfig().setColumnCommentFormat(columnCommentFormat);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see JavadocConfig#getEntityPackage()
|
||||
*/
|
||||
@ -877,6 +891,20 @@ public class GlobalConfig {
|
||||
getEntityConfig().setWithLombok(entityWithLombok);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see EntityConfig#isWithSwagger()
|
||||
*/
|
||||
public boolean isEntityWithSwagger() {
|
||||
return getEntityConfig().isWithSwagger();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see EntityConfig#setWithSwagger(boolean)
|
||||
*/
|
||||
public void setEntityWithSwagger(boolean entityWithSwagger) {
|
||||
getEntityConfig().setWithSwagger(entityWithSwagger);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see EntityConfig#getDataSource()
|
||||
*/
|
||||
|
||||
@ -16,6 +16,8 @@
|
||||
|
||||
package com.mybatisflex.codegen.config;
|
||||
|
||||
import com.mybatisflex.core.util.StringUtil;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.function.Function;
|
||||
@ -46,6 +48,11 @@ public class JavadocConfig {
|
||||
*/
|
||||
private UnaryOperator<String> tableCommentFormat = UnaryOperator.identity();
|
||||
|
||||
/**
|
||||
* 列名格式化。
|
||||
*/
|
||||
private UnaryOperator<String> columnCommentFormat = UnaryOperator.identity();
|
||||
|
||||
/**
|
||||
* Entity 包注释。
|
||||
*/
|
||||
@ -115,6 +122,9 @@ public class JavadocConfig {
|
||||
}
|
||||
|
||||
public String formatTableComment(String comment) {
|
||||
if (StringUtil.isBlank(comment)) {
|
||||
return "";
|
||||
}
|
||||
return tableCommentFormat.apply(comment);
|
||||
}
|
||||
|
||||
@ -133,6 +143,28 @@ public class JavadocConfig {
|
||||
return this;
|
||||
}
|
||||
|
||||
public String formatColumnComment(String comment) {
|
||||
if (StringUtil.isBlank(comment)) {
|
||||
return "";
|
||||
}
|
||||
return columnCommentFormat.apply(comment);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取列注释格式化。
|
||||
*/
|
||||
public Function<String, String> getColumnCommentFormat() {
|
||||
return columnCommentFormat;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置列注释格式化方案,用来生成实体类属性注释。
|
||||
*/
|
||||
public JavadocConfig setColumnCommentFormat(UnaryOperator<String> columnCommentFormat) {
|
||||
this.columnCommentFormat = columnCommentFormat;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取实体类层包注释。
|
||||
*/
|
||||
|
||||
@ -262,11 +262,7 @@ public class Column {
|
||||
annotations.append("@ColumnMask(\"").append(columnConfig.getMask()).append("\")");
|
||||
}
|
||||
|
||||
String result = annotations.toString();
|
||||
if (!result.isEmpty()) {
|
||||
result += "\n ";
|
||||
}
|
||||
return result;
|
||||
return annotations.toString();
|
||||
}
|
||||
|
||||
private void addComma(StringBuilder annotations, boolean needComma) {
|
||||
|
||||
@ -114,12 +114,17 @@ public class Table {
|
||||
}
|
||||
|
||||
public List<Column> getColumns() {
|
||||
// 生成字段排序
|
||||
columns.sort(Comparator.comparingInt((Column c) -> c.getProperty().length())
|
||||
.thenComparing(Column::getProperty));
|
||||
return columns;
|
||||
}
|
||||
|
||||
public List<Column> getSortedColumns() {
|
||||
ArrayList<Column> arrayList = new ArrayList<>(columns);
|
||||
// 生成字段排序
|
||||
arrayList.sort(Comparator.comparingInt((Column c) -> c.getProperty().length())
|
||||
.thenComparing(Column::getProperty));
|
||||
return arrayList;
|
||||
}
|
||||
|
||||
public void setColumns(List<Column> columns) {
|
||||
this.columns = columns;
|
||||
}
|
||||
|
||||
@ -1,9 +1,14 @@
|
||||
#set(withLombok = entityConfig.isWithLombok())
|
||||
#set(withSwagger = entityConfig.isWithSwagger())
|
||||
package #(packageConfig.entityPackage);
|
||||
|
||||
#for(importClass : table.buildImports())
|
||||
import #(importClass);
|
||||
#end
|
||||
#if(withSwagger)
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
#end
|
||||
#if(withLombok)
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
@ -23,12 +28,27 @@ import lombok.NoArgsConstructor;
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
#end
|
||||
#if(withSwagger)
|
||||
@ApiModel("#(table.getComment())")
|
||||
#end
|
||||
#(table.buildTableAnnotation())
|
||||
public class #(table.buildEntityClassName())#(table.buildExtends())#(table.buildImplements()) {
|
||||
#for(column : table.columns)
|
||||
|
||||
#(column.buildComment())
|
||||
#(column.buildAnnotations())private #(column.propertySimpleType) #(column.property);
|
||||
#set(comment = javadocConfig.formatColumnComment(column.comment))
|
||||
#if(isNotBlank(comment))
|
||||
/**
|
||||
* #(comment)
|
||||
*/
|
||||
#end
|
||||
#set(annotations = column.buildAnnotations())
|
||||
#if(isNotBlank(annotations))
|
||||
#(annotations)
|
||||
#end
|
||||
#if(withSwagger)
|
||||
@ApiModelProperty("#(column.comment)")
|
||||
#end
|
||||
private #(column.propertySimpleType) #(column.property);
|
||||
#end
|
||||
|
||||
#if(!withLombok)
|
||||
|
||||
@ -18,7 +18,7 @@ public class #(tableDefClassName) extends TableDef {
|
||||
*/
|
||||
public static final #(tableDefClassName) #(tableDefConfig.buildFieldName(table.buildEntityClassName() + tableDefConfig.instanceSuffix)) = new #(tableDefClassName)();
|
||||
|
||||
#for(column: table.columns)
|
||||
#for(column: table.getSortedColumns())
|
||||
#(column.buildComment())
|
||||
public final QueryColumn #(tableDefConfig.buildFieldName(column.property)) = new QueryColumn(this, "#(column.name)");
|
||||
|
||||
|
||||
@ -85,7 +85,7 @@ public class GeneratorTest {
|
||||
generator.generate();
|
||||
}
|
||||
|
||||
// @Test
|
||||
// @Test
|
||||
public void testCodeGen2() {
|
||||
//配置数据源
|
||||
HikariDataSource dataSource = new HikariDataSource();
|
||||
@ -160,11 +160,14 @@ public class GeneratorTest {
|
||||
|
||||
//用户信息表,用于存放用户信息。 -> 用户信息
|
||||
UnaryOperator<String> tableFormat = (e) -> e.split(",")[0].replace("表", "");
|
||||
//属性添加句号
|
||||
UnaryOperator<String> columnFormat = (e) -> e.concat("。");
|
||||
|
||||
//设置注解生成配置
|
||||
globalConfig.getJavadocConfig()
|
||||
.setAuthor("王帅")
|
||||
.setTableCommentFormat(tableFormat);
|
||||
.setTableCommentFormat(tableFormat)
|
||||
.setColumnCommentFormat(columnFormat);
|
||||
|
||||
//设置生成文件目录和根包
|
||||
globalConfig.getPackageConfig()
|
||||
@ -197,11 +200,14 @@ public class GeneratorTest {
|
||||
.setPropertiesNameStyle(TableDefConfig.NameStyle.LOWER_CAMEL_CASE)
|
||||
.setOverwriteEnable(true);
|
||||
|
||||
globalConfig.disableTableDef();
|
||||
|
||||
// 配置生成 entity
|
||||
globalConfig.enableEntity()
|
||||
.setOverwriteEnable(true)
|
||||
.setDataSource("ds1")
|
||||
.setWithLombok(true);
|
||||
.setWithLombok(true)
|
||||
.setWithSwagger(true);
|
||||
|
||||
// 配置生成 mapper
|
||||
globalConfig.enableMapper()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user