From 6787403cf952c3fae73a4ad8ef34fd425e43a970 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 23 Aug 2023 08:24:59 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E9=93=BE=E5=BC=8F?= =?UTF-8?q?=E4=B8=8E=20Builder=20=E6=9E=84=E5=BB=BA=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/config/ColumnConfig.java | 276 ++++++++++++------ .../codegen/config/TableConfig.java | 154 +++++++--- .../mybatisflex/codegen/entity/Column.java | 6 +- 3 files changed, 306 insertions(+), 130 deletions(-) diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ColumnConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ColumnConfig.java index dfc1f7a7..4933eea0 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ColumnConfig.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ColumnConfig.java @@ -24,8 +24,11 @@ import java.io.Serializable; /** * 表字段的单独设置。 */ +@SuppressWarnings({"rawtypes", "UnusedReturnValue", "unused"}) public class ColumnConfig implements Serializable { + private static final long serialVersionUID = -1511605303951623381L; + /** * 字段名称。 */ @@ -56,6 +59,11 @@ public class ColumnConfig implements Serializable { */ private Boolean version; + /** + * 是否是租户 ID。 + */ + private Boolean tenantId; + /** * 配置的 jdbcType。 */ @@ -82,7 +90,7 @@ public class ColumnConfig implements Serializable { /** * 脱敏方式。 */ - private String mask; + private String maskType; /** * 字段是否为主键。 @@ -104,139 +112,235 @@ public class ColumnConfig implements Serializable { */ private Boolean keyBefore; - /** - * 是否是租户 ID。 - */ - private Boolean tenantId; - - public String getColumnName() { - return columnName; + public static ColumnConfig create() { + return new ColumnConfig(); } - public void setColumnName(String columnName) { + public String getColumnName() { + return this.columnName; + } + + public ColumnConfig setColumnName(String columnName) { this.columnName = columnName; + return this; } public String getOnInsertValue() { - return onInsertValue; + return this.onInsertValue; } - public void setOnInsertValue(String onInsertValue) { + public ColumnConfig setOnInsertValue(String onInsertValue) { this.onInsertValue = onInsertValue; + return this; } public String getOnUpdateValue() { - return onUpdateValue; + return this.onUpdateValue; } - public void setOnUpdateValue(String onUpdateValue) { + public ColumnConfig setOnUpdateValue(String onUpdateValue) { this.onUpdateValue = onUpdateValue; + return this; } public Boolean getLarge() { - return isLarge; + return this.isLarge; } - public void setLarge(Boolean large) { - isLarge = large; + public ColumnConfig setLarge(Boolean large) { + this.isLarge = large; + return this; } public Boolean getLogicDelete() { - return isLogicDelete; + return this.isLogicDelete; } - public void setLogicDelete(Boolean logicDelete) { - isLogicDelete = logicDelete; + public ColumnConfig setLogicDelete(Boolean logicDelete) { + this.isLogicDelete = logicDelete; + return this; } public Boolean getVersion() { - return version; + return this.version; } - public void setVersion(Boolean version) { + public ColumnConfig setVersion(Boolean version) { this.version = version; - } - - public JdbcType getJdbcType() { - return jdbcType; - } - - public void setJdbcType(JdbcType jdbcType) { - this.jdbcType = jdbcType; - } - - public Class getTypeHandler() { - return typeHandler; - } - - public void setTypeHandler(Class typeHandler) { - this.typeHandler = typeHandler; - } - - public String getMask() { - return mask; - } - - public void setMask(String mask) { - this.mask = mask; - } - - public boolean isPrimaryKey() { - return isPrimaryKey; - } - - public void setPrimaryKey(boolean primaryKey) { - isPrimaryKey = primaryKey; - } - - public KeyType getKeyType() { - return keyType; - } - - public void setKeyType(KeyType keyType) { - this.keyType = keyType; - } - - public String getKeyValue() { - return keyValue; - } - - public void setKeyValue(String keyValue) { - this.keyValue = keyValue; - } - - public Boolean getKeyBefore() { - return keyBefore; - } - - public void setKeyBefore(Boolean keyBefore) { - this.keyBefore = keyBefore; + return this; } public Boolean getTenantId() { - return tenantId; + return this.tenantId; } - public void setTenantId(Boolean tenantId) { + public ColumnConfig setTenantId(Boolean tenantId) { this.tenantId = tenantId; + return this; + } + + public JdbcType getJdbcType() { + return this.jdbcType; + } + + public ColumnConfig setJdbcType(JdbcType jdbcType) { + this.jdbcType = jdbcType; + return this; } public String getPropertyType() { - return propertyType; + return this.propertyType; } - /** - * 原始类型直接写类型名称,例:int/long/float/double/boolean,对象类型请写对应类的全限定名,例:java.lang.String - */ - public void setPropertyType(String propertyType) { + public ColumnConfig setPropertyType(String propertyType) { this.propertyType = propertyType; + return this; } public String getPropertyDefaultValue() { - return propertyDefaultValue; + return this.propertyDefaultValue; } - public void setPropertyDefaultValue(String propertyDefaultValue) { + public ColumnConfig setPropertyDefaultValue(String propertyDefaultValue) { this.propertyDefaultValue = propertyDefaultValue; + return this; } + + public Class getTypeHandler() { + return this.typeHandler; + } + + public ColumnConfig setTypeHandler(Class typeHandler) { + this.typeHandler = typeHandler; + return this; + } + + public String getMaskType() { + return this.maskType; + } + + public ColumnConfig setMaskType(String maskType) { + this.maskType = maskType; + return this; + } + + public boolean isPrimaryKey() { + return this.isPrimaryKey; + } + + public ColumnConfig setPrimaryKey(boolean primaryKey) { + this.isPrimaryKey = primaryKey; + return this; + } + + public KeyType getKeyType() { + return this.keyType; + } + + public ColumnConfig setKeyType(KeyType keyType) { + this.keyType = keyType; + return this; + } + + public String getKeyValue() { + return this.keyValue; + } + + public ColumnConfig setKeyValue(String keyValue) { + this.keyValue = keyValue; + return this; + } + + public Boolean getKeyBefore() { + return this.keyBefore; + } + + public ColumnConfig setKeyBefore(Boolean keyBefore) { + this.keyBefore = keyBefore; + return this; + } + + public static final class Builder { + + private final ColumnConfig columnConfig; + + private Builder() { + this.columnConfig = new ColumnConfig(); + } + + public static Builder builder() { + return new Builder(); + } + + public Builder columnName(String columnName) { + this.columnConfig.setColumnName(columnName); + return this; + } + + public Builder onInsertValue(String onInsertValue) { + this.columnConfig.setOnInsertValue(onInsertValue); + return this; + } + + public Builder onUpdateValue(String onUpdateValue) { + this.columnConfig.setOnUpdateValue(onUpdateValue); + return this; + } + + public Builder version(Boolean version) { + this.columnConfig.setVersion(version); + return this; + } + + public Builder tenantId(Boolean tenantId) { + this.columnConfig.setTenantId(tenantId); + return this; + } + + public Builder jdbcType(JdbcType jdbcType) { + this.columnConfig.setJdbcType(jdbcType); + return this; + } + + public Builder propertyType(String propertyType) { + this.columnConfig.setPropertyType(propertyType); + return this; + } + + public Builder propertyDefaultValue(String propertyDefaultValue) { + this.columnConfig.setPropertyDefaultValue(propertyDefaultValue); + return this; + } + + public Builder typeHandler(Class typeHandler) { + this.columnConfig.setTypeHandler(typeHandler); + return this; + } + + public Builder maskType(String maskType) { + this.columnConfig.setMaskType(maskType); + return this; + } + + public Builder keyType(KeyType keyType) { + this.columnConfig.setKeyType(keyType); + return this; + } + + public Builder keyValue(String keyValue) { + this.columnConfig.setKeyValue(keyValue); + return this; + } + + public Builder keyBefore(Boolean keyBefore) { + this.columnConfig.setKeyBefore(keyBefore); + return this; + } + + public ColumnConfig build() { + return this.columnConfig; + } + + } + } diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TableConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TableConfig.java index 707d5ad5..4b60465f 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TableConfig.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TableConfig.java @@ -25,18 +25,19 @@ import java.util.Map; /** * 表的单独设置。 */ +@SuppressWarnings({"unused", "UnusedReturnValue"}) public class TableConfig { - /** - * 表名。 - */ - private String tableName; - /** * 数据库的 schema(模式)。 */ private String schema; + /** + * 表名。 + */ + private String tableName; + /** * 默认为 驼峰属性 转换为 下划线字段。 */ @@ -57,89 +58,160 @@ public class TableConfig { */ private Class setListenerClass; - /** - * 对应列的配置。 - */ - private Map columnConfigMap; - /** * 是否开启 Mapper 生成。 */ private Boolean mapperGenerateEnable = Boolean.TRUE; - public String getTableName() { - return tableName; - } + /** + * 对应列的配置。 + */ + private Map columnConfigMap; - public void setTableName(String tableName) { - this.tableName = tableName; + public static TableConfig create() { + return new TableConfig(); } public String getSchema() { - return schema; + return this.schema; } - public void setSchema(String schema) { + public TableConfig setSchema(String schema) { this.schema = schema; + return this; + } + + public String getTableName() { + return this.tableName; + } + + public TableConfig setTableName(String tableName) { + this.tableName = tableName; + return this; } public Boolean getCamelToUnderline() { - return camelToUnderline; + return this.camelToUnderline; } - public void setCamelToUnderline(Boolean camelToUnderline) { + public TableConfig setCamelToUnderline(Boolean camelToUnderline) { this.camelToUnderline = camelToUnderline; + return this; } public Class getInsertListenerClass() { - return insertListenerClass; + return this.insertListenerClass; } - public void setInsertListenerClass(Class insertListenerClass) { + public TableConfig setInsertListenerClass(Class insertListenerClass) { this.insertListenerClass = insertListenerClass; + return this; } public Class getUpdateListenerClass() { - return updateListenerClass; + return this.updateListenerClass; } - public void setUpdateListenerClass(Class updateListenerClass) { + public TableConfig setUpdateListenerClass(Class updateListenerClass) { this.updateListenerClass = updateListenerClass; + return this; } public Class getSetListenerClass() { - return setListenerClass; + return this.setListenerClass; } - public void setSetListenerClass(Class setListenerClass) { + public TableConfig setSetListenerClass(Class setListenerClass) { this.setListenerClass = setListenerClass; - } - - public Map getColumnConfigMap() { - return columnConfigMap; - } - - public void setColumnConfigMap(Map columnConfigMap) { - this.columnConfigMap = columnConfigMap; + return this; } public Boolean getMapperGenerateEnable() { - return mapperGenerateEnable; + return this.mapperGenerateEnable; } - public void setMapperGenerateEnable(Boolean mapperGenerateEnable) { + public TableConfig setMapperGenerateEnable(Boolean mapperGenerateEnable) { this.mapperGenerateEnable = mapperGenerateEnable; + return this; } - public void addColumnConfig(ColumnConfig columnConfig) { - if (columnConfigMap == null) { - columnConfigMap = new HashMap<>(); + public Map getColumnConfigMap() { + return this.columnConfigMap; + } + + public TableConfig setColumnConfigMap(Map columnConfigMap) { + this.columnConfigMap = columnConfigMap; + return this; + } + + public TableConfig addColumnConfig(ColumnConfig columnConfig) { + if (this.columnConfigMap == null) { + this.columnConfigMap = new HashMap<>(); } - columnConfigMap.put(columnConfig.getColumnName(), columnConfig); + this.columnConfigMap.put(columnConfig.getColumnName(), columnConfig); + return this; } - public ColumnConfig getColumnConfig(String columnName) { - return columnConfigMap == null ? null : columnConfigMap.get(columnName); + protected ColumnConfig getColumnConfig(String columnName) { + return this.columnConfigMap == null ? null : this.columnConfigMap.get(columnName); + } + + public static final class Builder { + + private final TableConfig tableConfig; + + private Builder() { + this.tableConfig = new TableConfig(); + } + + public static Builder builder() { + return new Builder(); + } + + public Builder schema(String schema) { + this.tableConfig.setSchema(schema); + return this; + } + + public Builder tableName(String tableName) { + this.tableConfig.setTableName(tableName); + return this; + } + + public Builder camelToUnderline(Boolean camelToUnderline) { + this.tableConfig.setCamelToUnderline(camelToUnderline); + return this; + } + + public Builder insertListenerClass(Class insertListenerClass) { + this.tableConfig.setInsertListenerClass(insertListenerClass); + return this; + } + + public Builder updateListenerClass(Class updateListenerClass) { + this.tableConfig.setUpdateListenerClass(updateListenerClass); + return this; + } + + public Builder setListenerClass(Class setListenerClass) { + this.tableConfig.setSetListenerClass(setListenerClass); + return this; + } + + public Builder mapperGenerateEnable(Boolean mapperGenerateEnable) { + this.tableConfig.setMapperGenerateEnable(mapperGenerateEnable); + return this; + } + + public Builder columnConfig(ColumnConfig columnConfigMap) { + this.tableConfig.addColumnConfig(columnConfigMap); + return this; + } + + public TableConfig build() { + return this.tableConfig; + } + } } diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java index e44136b2..7129d518 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java @@ -295,8 +295,8 @@ public class Column { } //@ColumnMask 注解 - if (columnConfig.getMask() != null) { - annotations.append("@ColumnMask(\"").append(columnConfig.getMask()).append("\")"); + if (columnConfig.getMaskType() != null) { + annotations.append("@ColumnMask(\"").append(columnConfig.getMaskType()).append("\")"); } return annotations.toString(); @@ -324,7 +324,7 @@ public class Column { if (columnConfig.getPropertyType() != null) { addImportClass(importClasses, columnConfig.getPropertyType()); } - if (columnConfig.getMask() != null) { + if (columnConfig.getMaskType() != null) { addImportClass(importClasses, ColumnMask.class.getName()); }