From 4fa5f8958dcd99812c0465ec674ef9d9759883e6 Mon Sep 17 00:00:00 2001 From: chenmin Date: Tue, 24 Mar 2020 15:46:58 +0800 Subject: [PATCH 01/11] =?UTF-8?q?=E4=BF=AE=E6=94=B9entity=E7=9A=84?= =?UTF-8?q?=E6=B3=A8=E8=A7=A3=E5=8C=85=E8=B7=AF=E5=BE=84,=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0lombok,=E4=BF=AE=E6=94=B9Controller=E4=B8=BARestContro?= =?UTF-8?q?ller?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/baomidou/config/StrategyConfig.java | 5 +- .../resources/template/controller.java.vm | 36 ++--- src/main/resources/template/entity.java.vm | 143 +++++++++--------- 3 files changed, 96 insertions(+), 88 deletions(-) diff --git a/src/main/java/com/baomidou/config/StrategyConfig.java b/src/main/java/com/baomidou/config/StrategyConfig.java index ec6cf4f..d8c0eba 100644 --- a/src/main/java/com/baomidou/config/StrategyConfig.java +++ b/src/main/java/com/baomidou/config/StrategyConfig.java @@ -80,8 +80,9 @@ public class StrategyConfig { } public NamingStrategy getFieldNaming() { - if (fieldNaming == null) - return naming; + if (fieldNaming == null) { + return naming; + } return fieldNaming; } diff --git a/src/main/resources/template/controller.java.vm b/src/main/resources/template/controller.java.vm index b92da73..ca4aee2 100644 --- a/src/main/resources/template/controller.java.vm +++ b/src/main/resources/template/controller.java.vm @@ -1,18 +1,18 @@ -package ${package.Controller}; - -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; - -/** - *

- * ${table.comment} 前端控制器 - *

- * - * @author ${author} - * @since ${date} - */ -@Controller -@RequestMapping("#if(${package.ModuleName})/${package.ModuleName}#end/${table.entityName.toLowerCase()}") -public class ${table.controllerName} { - -} +package ${package.Controller}; + +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.RequestMapping; + +/** + *

+ * ${table.comment} 前端控制器 + *

+ * + * @author ${author} + * @since ${date} + */ +@RestController +@RequestMapping("#if(${package.ModuleName})/${package.ModuleName}#end/${table.entityName.toLowerCase()}") +public class ${table.controllerName} { + +} diff --git a/src/main/resources/template/entity.java.vm b/src/main/resources/template/entity.java.vm index 16299e4..5ceba67 100644 --- a/src/main/resources/template/entity.java.vm +++ b/src/main/resources/template/entity.java.vm @@ -1,68 +1,75 @@ -package ${package.Entity}; - -#if(${activeRecord}) -import com.baomidou.mybatisplus.activerecord.Model; -#end -#if(${idGenType}!="ID_WORKER") -import com.baomidou.mybatisplus.enums.IdType; -import com.baomidou.mybatisplus.annotations.TableId; -#end -import com.baomidou.mybatisplus.annotations.TableField; -#if(${addTabeName}) -import com.baomidou.mybatisplus.annotations.TableName; -#end -import java.io.Serializable; -#if(${table.hasDate}) -import java.util.Date; -#end - -/** - *

- * ${table.comment} - *

- * - * @author ${author} - * @since ${date} - */ -#if(${addTabeName}) -@TableName("${table.name}") -#end -#if(${activeRecord}) -public class ${entity} extends Model<${entity}> { -#else -public class ${entity} implements Serializable { -#end - -private static final long serialVersionUID = 1L; - -#foreach($field in ${table.fields}) -/** - * ${field.comment} - */ - #if(${field.keyFlag} && ${idGenType}!="ID_WORKER") - @TableId(type = IdType.${idGenType}) - #end - #if($field.convert && ${field.name.toLowerCase()} != ${field.propertyName.toLowerCase()}) - @TableField(value="${field.name}") - #end -private ${field.propertyType} ${field.propertyName}; - -#end - -#foreach($field in ${table.fields}) - #if(${field.propertyName.equals("Boolean")}) - #set($getprefix="is") - #else - #set($getprefix="get") - #end - -public ${field.propertyType} ${getprefix}${field.capitalName}() { - return ${field.propertyName}; - } - -public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) { - this.${field.propertyName} = ${field.propertyName}; - } -#end - - } +package ${package.Entity}; + +#if(${activeRecord}) +import com.baomidou.mybatisplus.activerecord.Model; +#end +#if(${idGenType}!="ID_WORKER") +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +#end +import com.baomidou.mybatisplus.annotation.TableField; +#if(${addTabeName}) +import com.baomidou.mybatisplus.annotation.TableName; +#end + +import lombok.Data; +import lombok.experimental.Accessors; + +import java.io.Serializable; +#if(${table.hasDate}) +import java.util.Date; +#end + +/** + *

+ * ${table.comment} + *

+ * + * @author ${author} + * @since ${date} + */ +@Data +@Accessors(chain = true) +#if(${addTabeName}) +@TableName("${table.name}") +#end +#if(${activeRecord}) +public class ${entity} extends Model<${entity}> { +#else +public class ${entity} implements Serializable{ +#end + +private static final long serialVersionUID=1L; + +#foreach($field in ${table.fields}) + #if(${field.comment.equals("")}) + + #else + /** + * ${field.comment} + */ + #end + #if(${field.keyFlag} && ${idGenType}!="ID_WORKER") + @TableId(type = IdType.${idGenType}) + #end + #if($field.convert && ${field.name.toLowerCase()} != ${field.propertyName.toLowerCase()}) + @TableField(value = "${field.name}") + #end +private ${field.propertyType} ${field.propertyName}; +#end + +###foreach($field in ${table.fields}) +## #if(${field.propertyName.equals("Boolean")}) +## #set($getprefix="is") +## #else +## #set($getprefix="get") +## #end +## +##public ${field.propertyType} ${getprefix}${field.capitalName}(){ +## return ${field.propertyName}; +## } +##public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) { +## this.${field.propertyName} = ${field.propertyName}; +## } +###end +} -- Gitee From 356367504f121fed98fa9842ba6e645e1e0c2876 Mon Sep 17 00:00:00 2001 From: huazhuTech_chenmin Date: Sat, 18 Apr 2020 18:45:30 +0800 Subject: [PATCH 02/11] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Swagger=E6=B3=A8?= =?UTF-8?q?=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../resources/template/controller.java.vm | 2 ++ src/main/resources/template/entity.java.vm | 19 +++++-------------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/src/main/resources/template/controller.java.vm b/src/main/resources/template/controller.java.vm index ca4aee2..6dda0a1 100644 --- a/src/main/resources/template/controller.java.vm +++ b/src/main/resources/template/controller.java.vm @@ -1,5 +1,6 @@ package ${package.Controller}; +import io.swagger.annotations.Api; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RequestMapping; @@ -11,6 +12,7 @@ import org.springframework.web.bind.annotation.RequestMapping; * @author ${author} * @since ${date} */ +@Api("${table.comment}") @RestController @RequestMapping("#if(${package.ModuleName})/${package.ModuleName}#end/${table.entityName.toLowerCase()}") public class ${table.controllerName} { diff --git a/src/main/resources/template/entity.java.vm b/src/main/resources/template/entity.java.vm index 5ceba67..190277a 100644 --- a/src/main/resources/template/entity.java.vm +++ b/src/main/resources/template/entity.java.vm @@ -12,6 +12,9 @@ import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableName; #end +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + import lombok.Data; import lombok.experimental.Accessors; @@ -30,6 +33,7 @@ import java.util.Date; */ @Data @Accessors(chain = true) +@ApiModel(description = "${table.comment}") #if(${addTabeName}) @TableName("${table.name}") #end @@ -53,23 +57,10 @@ private static final long serialVersionUID=1L; @TableId(type = IdType.${idGenType}) #end #if($field.convert && ${field.name.toLowerCase()} != ${field.propertyName.toLowerCase()}) +@ApiModelProperty(name = "${field.comment}") @TableField(value = "${field.name}") #end private ${field.propertyType} ${field.propertyName}; #end -###foreach($field in ${table.fields}) -## #if(${field.propertyName.equals("Boolean")}) -## #set($getprefix="is") -## #else -## #set($getprefix="get") -## #end -## -##public ${field.propertyType} ${getprefix}${field.capitalName}(){ -## return ${field.propertyName}; -## } -##public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) { -## this.${field.propertyName} = ${field.propertyName}; -## } -###end } -- Gitee From 210378ae5f34f2636baddee7c6f6f093b97d4d9f Mon Sep 17 00:00:00 2001 From: huazhuTech_chenmin Date: Tue, 21 Apr 2020 00:08:54 +0800 Subject: [PATCH 03/11] =?UTF-8?q?=E9=80=82=E9=85=8D=E6=9C=80=E6=96=B0?= =?UTF-8?q?=E7=9A=84MyBatis-Plus=E4=B8=BB=E9=94=AE=E7=94=9F=E6=88=90?= =?UTF-8?q?=E7=AD=96=E7=95=A5,=E4=BF=AE=E6=94=B9mapper.xml=E7=94=9F?= =?UTF-8?q?=E6=88=90=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/baomidou/config/builder/ConfigBuilder.java | 12 ++++++++---- .../java/com/baomidou/config/rules/IdStrategy.java | 2 +- src/main/resources/template/entity.java.vm | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/baomidou/config/builder/ConfigBuilder.java b/src/main/java/com/baomidou/config/builder/ConfigBuilder.java index f0d27cd..e126114 100644 --- a/src/main/java/com/baomidou/config/builder/ConfigBuilder.java +++ b/src/main/java/com/baomidou/config/builder/ConfigBuilder.java @@ -178,7 +178,7 @@ public class ConfigBuilder { packageInfo.put(ConstVal.MODULENAME, config.getModuleName()); packageInfo.put(ConstVal.ENTITY, joinPackage(config.getParent(), config.getEntity())); packageInfo.put(ConstVal.MAPPER, joinPackage(config.getParent(), config.getMapper())); - packageInfo.put(ConstVal.XML, joinPackage(config.getParent(), config.getXml())); + packageInfo.put(ConstVal.XML, joinPackage("", config.getXml())); packageInfo.put(ConstVal.SERIVCE, joinPackage(config.getParent(), config.getService())); packageInfo.put(ConstVal.SERVICEIMPL, joinPackage(config.getParent(), config.getServiceImpl())); packageInfo.put(ConstVal.CONTROLLER, joinPackage(config.getParent(), config.getController())); @@ -186,7 +186,7 @@ public class ConfigBuilder { pathInfo = new HashMap(); pathInfo.put(ConstVal.ENTITY_PATH, joinPath(outputDir, packageInfo.get(ConstVal.ENTITY))); pathInfo.put(ConstVal.MAPPER_PATH, joinPath(outputDir, packageInfo.get(ConstVal.MAPPER))); - pathInfo.put(ConstVal.XML_PATH, joinPath(outputDir, packageInfo.get(ConstVal.XML))); + pathInfo.put(ConstVal.XML_PATH, packageInfo.get(ConstVal.XML)); pathInfo.put(ConstVal.SERIVCE_PATH, joinPath(outputDir, packageInfo.get(ConstVal.SERIVCE))); pathInfo.put(ConstVal.SERVICEIMPL_PATH, joinPath(outputDir, packageInfo.get(ConstVal.SERVICEIMPL))); pathInfo.put(ConstVal.CONTROLLER_PATH, joinPath(outputDir, packageInfo.get(ConstVal.CONTROLLER))); @@ -244,8 +244,12 @@ public class ConfigBuilder { idType = IdStrategy.auto.getValue(); } else if (config.getIdGenType() == IdStrategy.input) { idType = IdStrategy.input.getValue(); - } else if (config.getIdGenType() == IdStrategy.uuid) { - idType = IdStrategy.uuid.getValue(); + } else if (config.getIdGenType() == IdStrategy.none) { + idType = IdStrategy.none.getValue(); + } else if (config.getIdGenType() == IdStrategy.assign_id) { + idType = IdStrategy.assign_id.getValue(); + }else if (config.getIdGenType() == IdStrategy.assign_uuid) { + idType = IdStrategy.assign_uuid.getValue(); } else { idType = IdStrategy.id_worker.getValue(); } diff --git a/src/main/java/com/baomidou/config/rules/IdStrategy.java b/src/main/java/com/baomidou/config/rules/IdStrategy.java index 5bdc41e..27d8e4c 100644 --- a/src/main/java/com/baomidou/config/rules/IdStrategy.java +++ b/src/main/java/com/baomidou/config/rules/IdStrategy.java @@ -7,7 +7,7 @@ package com.baomidou.config.rules; * @since 2016/8/30 */ public enum IdStrategy { - auto("AUTO"), id_worker("ID_WORKER"), uuid("UUID"), input("INPUT"); + auto("AUTO"), none("NONE"), assign_id("ASSIGN_ID"), assign_uuid("ASSIGN_UUID"),id_worker("ID_WORKER"), uuid("UUID"), input("INPUT"); private String value; diff --git a/src/main/resources/template/entity.java.vm b/src/main/resources/template/entity.java.vm index 190277a..e9b9f0a 100644 --- a/src/main/resources/template/entity.java.vm +++ b/src/main/resources/template/entity.java.vm @@ -57,7 +57,7 @@ private static final long serialVersionUID=1L; @TableId(type = IdType.${idGenType}) #end #if($field.convert && ${field.name.toLowerCase()} != ${field.propertyName.toLowerCase()}) -@ApiModelProperty(name = "${field.comment}") + @ApiModelProperty(name = "${field.comment}") @TableField(value = "${field.name}") #end private ${field.propertyType} ${field.propertyName}; -- Gitee From 9d6b3f18f2c46682edc6730009b28530c92713d7 Mon Sep 17 00:00:00 2001 From: huazhuTech_chenmin Date: Tue, 21 Apr 2020 00:11:31 +0800 Subject: [PATCH 04/11] =?UTF-8?q?=E4=BF=AE=E6=94=B9Swagger=E6=B3=A8?= =?UTF-8?q?=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/template/entity.java.vm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/template/entity.java.vm b/src/main/resources/template/entity.java.vm index e9b9f0a..3644fb2 100644 --- a/src/main/resources/template/entity.java.vm +++ b/src/main/resources/template/entity.java.vm @@ -56,8 +56,8 @@ private static final long serialVersionUID=1L; #if(${field.keyFlag} && ${idGenType}!="ID_WORKER") @TableId(type = IdType.${idGenType}) #end +@ApiModelProperty(name = "${field.comment}") #if($field.convert && ${field.name.toLowerCase()} != ${field.propertyName.toLowerCase()}) - @ApiModelProperty(name = "${field.comment}") @TableField(value = "${field.name}") #end private ${field.propertyType} ${field.propertyName}; -- Gitee From f6bf4bacde97cf5f5390dfdd8050295088a3a9de Mon Sep 17 00:00:00 2001 From: huazhuTech_chenmin Date: Wed, 29 Apr 2020 13:58:26 +0800 Subject: [PATCH 05/11] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dinclude=E5=92=8Cexclude?= =?UTF-8?q?=E7=94=9F=E6=95=88=EF=BC=8C=E4=B8=94=E4=B8=A4=E8=80=85=E5=8F=AF?= =?UTF-8?q?=E5=90=8C=E6=97=B6=E5=AD=98=E5=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/builder/ConfigBuilder.java | 51 +++++++------------ 1 file changed, 18 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/baomidou/config/builder/ConfigBuilder.java b/src/main/java/com/baomidou/config/builder/ConfigBuilder.java index e126114..6b20aa2 100644 --- a/src/main/java/com/baomidou/config/builder/ConfigBuilder.java +++ b/src/main/java/com/baomidou/config/builder/ConfigBuilder.java @@ -5,12 +5,8 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; +import java.util.stream.Collectors; import org.apache.commons.lang.StringUtils; @@ -285,41 +281,37 @@ public class ConfigBuilder { private List getTablesInfo(StrategyConfig config) { boolean isInclude = (null != config.getInclude() && config.getInclude().length > 0); boolean isExclude = (null != config.getExclude() && config.getExclude().length > 0); - if (isInclude && isExclude) { - throw new RuntimeException(" 标签中 只能配置一项!"); - } + List includes = Arrays.asList(config.getInclude()); + List excludes = Arrays.asList(config.getExclude()); List tableList = new ArrayList(); - Set notExistTables = new HashSet(); NamingStrategy strategy = config.getNaming(); NamingStrategy fieldStrategy = config.getFieldNaming(); + String tablePrefix = config.getTablePrefix(); PreparedStatement pstate = null; try { pstate = connection.prepareStatement(querySQL.getTableCommentsSql()); ResultSet results = pstate.executeQuery(); while (results.next()) { String tableName = results.getString(querySQL.getTableName()); + if (tablePrefix != null && !tableName.toUpperCase().startsWith(tablePrefix.toUpperCase())) { + continue; + } if (StringUtils.isNotBlank(tableName)) { String tableComment = results.getString(querySQL.getTableComment()); TableInfo tableInfo = new TableInfo(); if (isInclude) { - for (String includeTab : config.getInclude()) { - if (includeTab.equalsIgnoreCase(tableName)) { - tableInfo.setName(tableName); - tableInfo.setComment(tableComment); - } else { - notExistTables.add(includeTab); - } + if (includes.contains(tableName)) { + tableInfo.setName(tableName); + tableInfo.setComment(tableComment); } - } else if (isExclude) { - for (String excludeTab : config.getExclude()) { - if (!excludeTab.equalsIgnoreCase(tableName)) { - tableInfo.setName(tableName); - tableInfo.setComment(tableComment); - } else { - notExistTables.add(excludeTab); - } + } + if (isExclude) { + if (!excludes.contains(tableName)) { + tableInfo.setName(tableName); + tableInfo.setComment(tableComment); } - } else { + } + if (!isInclude || !isExclude) { tableInfo.setName(tableName); tableInfo.setComment(tableComment); } @@ -332,13 +324,6 @@ public class ConfigBuilder { System.err.println("当前数据库为空!!!"); } } - // 将已经存在的表移除 - for (TableInfo tabInfo : tableList) { - notExistTables.remove(tabInfo.getName()); - } - if (notExistTables.size() > 0) { - System.err.println("表 " + notExistTables + " 在数据库中不存在!!!"); - } } catch (SQLException e) { e.printStackTrace(); } finally { -- Gitee From 4f9604a22bdf4fac069e293b1a405adacae319d7 Mon Sep 17 00:00:00 2001 From: huazhuTech_chenmin Date: Mon, 8 Jun 2020 23:02:21 +0800 Subject: [PATCH 06/11] =?UTF-8?q?=E5=A2=9E=E5=8A=A0naming=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=EF=BC=9Aremove=5Funderline(=E5=8E=BB=E6=8E=89?= =?UTF-8?q?=E7=AC=AC=E4=B8=80=E4=B8=AA=E4=B8=8B=E5=88=92=E7=BA=BF=E4=B9=8B?= =?UTF-8?q?=E5=89=8D=E5=86=85=E5=AE=B9=EF=BC=8C=E5=90=8E=E9=9D=A2=E5=8E=9F?= =?UTF-8?q?=E6=A0=B7=E8=BE=93=E5=87=BA),remove=5Funderline=5Fand=5Fcamel(?= =?UTF-8?q?=E5=8E=BB=E6=8E=89=E7=AC=AC=E4=B8=80=E4=B8=AA=E4=B8=8B=E5=88=92?= =?UTF-8?q?=E7=BA=BF=E4=B9=8B=E5=89=8D=E5=86=85=E5=AE=B9=EF=BC=8C=E5=90=8E?= =?UTF-8?q?=E9=9D=A2=E8=BD=AC=E9=A9=BC=E5=B3=B0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/baomidou/config/StrategyConfig.java | 1 + .../config/builder/ConfigBuilder.java | 27 ++++++++++++------- .../baomidou/config/rules/NamingStrategy.java | 24 ++++++++++++++--- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/baomidou/config/StrategyConfig.java b/src/main/java/com/baomidou/config/StrategyConfig.java index d8c0eba..dfc1b6b 100644 --- a/src/main/java/com/baomidou/config/StrategyConfig.java +++ b/src/main/java/com/baomidou/config/StrategyConfig.java @@ -19,6 +19,7 @@ public class StrategyConfig { @Parameter(defaultValue = "nochange") private NamingStrategy naming; + @Parameter(defaultValue = "underline_to_camel") private NamingStrategy fieldNaming; /** diff --git a/src/main/java/com/baomidou/config/builder/ConfigBuilder.java b/src/main/java/com/baomidou/config/builder/ConfigBuilder.java index 6b20aa2..9aa2b28 100644 --- a/src/main/java/com/baomidou/config/builder/ConfigBuilder.java +++ b/src/main/java/com/baomidou/config/builder/ConfigBuilder.java @@ -6,7 +6,6 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.*; -import java.util.stream.Collectors; import org.apache.commons.lang.StringUtils; @@ -450,14 +449,24 @@ public class ConfigBuilder { */ private String processName(String name, NamingStrategy strategy, String tablePrefix) { String propertyName = ""; - if (strategy == NamingStrategy.remove_prefix_and_camel) { - propertyName = NamingStrategy.removePrefixAndCamel(name, tablePrefix); - } else if (strategy == NamingStrategy.underline_to_camel) { - propertyName = NamingStrategy.underlineToCamel(name); - } else if (strategy == NamingStrategy.remove_prefix) { - propertyName = NamingStrategy.removePrefix(name, tablePrefix); - } else { - propertyName = name; + switch (strategy) { + case remove_prefix_and_camel: + propertyName = NamingStrategy.removePrefixAndCamel(name, tablePrefix); + break; + case underline_to_camel: + propertyName = NamingStrategy.underlineToCamel(name); + break; + case remove_prefix: + propertyName = NamingStrategy.removePrefix(name, tablePrefix); + break; + case remove_underline: + propertyName = NamingStrategy.removeUnderline(name); + break; + case remove_underline_and_camel: + propertyName = NamingStrategy.removeUnderlineAndCamel(name); + break; + default: + propertyName = name; } return propertyName; } diff --git a/src/main/java/com/baomidou/config/rules/NamingStrategy.java b/src/main/java/com/baomidou/config/rules/NamingStrategy.java index ac90cfe..a9bae0c 100644 --- a/src/main/java/com/baomidou/config/rules/NamingStrategy.java +++ b/src/main/java/com/baomidou/config/rules/NamingStrategy.java @@ -25,7 +25,15 @@ public enum NamingStrategy { /** * 去掉前缀并且转驼峰 */ - remove_prefix_and_camel; + remove_prefix_and_camel, + /** + * 去掉第一个下划线之前内容,后面原样输出 + */ + remove_underline, + /** + * 去掉第一个下划线之前内容,后面转驼峰 + */ + remove_underline_and_camel; public static String underlineToCamel(String name) { // 快速检查 @@ -54,12 +62,12 @@ public enum NamingStrategy { } /** - * 去掉下划线前缀 + * 去掉第一个下划线之前内容,后面原样输出 * * @param name * @return */ - public static String removePrefix(String name) { + public static String removeUnderline(String name) { if (StringUtils.isBlank(name)) { return ""; } @@ -69,6 +77,16 @@ public enum NamingStrategy { } return name.substring(idx + 1); } + + /** + * 去掉第一个下划线之前内容,后面转驼峰 + * + * @param name + * @return + */ + public static String removeUnderlineAndCamel(String name) { + return underlineToCamel(removeUnderline(name)); + } /** * 去掉指定的前缀 -- Gitee From 323865779798d8a5537f1b53eaccce7e37c1dbfd Mon Sep 17 00:00:00 2001 From: huazhuTech_chenmin Date: Sun, 14 Jun 2020 22:45:39 +0800 Subject: [PATCH 07/11] =?UTF-8?q?=E8=B0=83=E6=95=B4controller=E5=92=8Centi?= =?UTF-8?q?ty=E7=9A=84swagger=E6=B3=A8=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/template/controller.java.vm | 2 +- src/main/resources/template/entity.java.vm | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/resources/template/controller.java.vm b/src/main/resources/template/controller.java.vm index 6dda0a1..f3394e1 100644 --- a/src/main/resources/template/controller.java.vm +++ b/src/main/resources/template/controller.java.vm @@ -12,7 +12,7 @@ import org.springframework.web.bind.annotation.RequestMapping; * @author ${author} * @since ${date} */ -@Api("${table.comment}") +@Api(tags = "${table.comment}") @RestController @RequestMapping("#if(${package.ModuleName})/${package.ModuleName}#end/${table.entityName.toLowerCase()}") public class ${table.controllerName} { diff --git a/src/main/resources/template/entity.java.vm b/src/main/resources/template/entity.java.vm index 3644fb2..7e46767 100644 --- a/src/main/resources/template/entity.java.vm +++ b/src/main/resources/template/entity.java.vm @@ -33,7 +33,7 @@ import java.util.Date; */ @Data @Accessors(chain = true) -@ApiModel(description = "${table.comment}") +@ApiModel("${table.comment}") #if(${addTabeName}) @TableName("${table.name}") #end @@ -56,7 +56,7 @@ private static final long serialVersionUID=1L; #if(${field.keyFlag} && ${idGenType}!="ID_WORKER") @TableId(type = IdType.${idGenType}) #end -@ApiModelProperty(name = "${field.comment}") +@ApiModelProperty("${field.comment}") #if($field.convert && ${field.name.toLowerCase()} != ${field.propertyName.toLowerCase()}) @TableField(value = "${field.name}") #end -- Gitee From 37fb84fd965ff47aa9645c647a5fa36b307eae56 Mon Sep 17 00:00:00 2001 From: artislong <15221499301@163.com> Date: Fri, 28 Aug 2020 16:51:46 +0800 Subject: [PATCH 08/11] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dinclude=E5=92=8Cexclude?= =?UTF-8?q?=E5=90=8C=E6=97=B6=E5=AD=98=E5=9C=A8=E7=9A=84BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/builder/ConfigBuilder.java | 35 ++++++++----------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/baomidou/config/builder/ConfigBuilder.java b/src/main/java/com/baomidou/config/builder/ConfigBuilder.java index 9aa2b28..11797ab 100644 --- a/src/main/java/com/baomidou/config/builder/ConfigBuilder.java +++ b/src/main/java/com/baomidou/config/builder/ConfigBuilder.java @@ -1,25 +1,20 @@ package com.baomidou.config.builder; -import java.io.File; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.*; - -import org.apache.commons.lang.StringUtils; - -import com.baomidou.config.ConstVal; -import com.baomidou.config.DataSourceConfig; -import com.baomidou.config.PackageConfig; -import com.baomidou.config.StrategyConfig; -import com.baomidou.config.TemplateConfig; +import com.baomidou.config.*; import com.baomidou.config.po.TableField; import com.baomidou.config.po.TableInfo; import com.baomidou.config.rules.DbType; import com.baomidou.config.rules.IdStrategy; import com.baomidou.config.rules.NamingStrategy; import com.baomidou.config.rules.QuerySQL; +import org.apache.commons.lang.StringUtils; + +import java.io.File; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.*; /** * 配置汇总 传递给文件生成工具 @@ -303,17 +298,15 @@ public class ConfigBuilder { tableInfo.setName(tableName); tableInfo.setComment(tableComment); } + } else { + tableInfo.setName(tableName); + tableInfo.setComment(tableComment); } if (isExclude) { - if (!excludes.contains(tableName)) { - tableInfo.setName(tableName); - tableInfo.setComment(tableComment); + if (excludes.contains(tableName)) { + continue; } } - if (!isInclude || !isExclude) { - tableInfo.setName(tableName); - tableInfo.setComment(tableComment); - } if (StringUtils.isNotBlank(tableInfo.getName())) { List fieldList = getListFields(tableInfo.getName(), fieldStrategy); tableInfo.setFields(fieldList); -- Gitee From 24acdb09711b1b3aab9830f37ff70e5ef4814193 Mon Sep 17 00:00:00 2001 From: artislong <15221499301@163.com> Date: Wed, 28 Apr 2021 11:48:50 +0800 Subject: [PATCH 09/11] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dinclude=E5=92=8Cexclude?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E6=9C=AA=E9=85=8D=E7=BD=AE=E6=97=B6=E7=9A=84?= =?UTF-8?q?bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/baomidou/config/builder/ConfigBuilder.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/baomidou/config/builder/ConfigBuilder.java b/src/main/java/com/baomidou/config/builder/ConfigBuilder.java index 11797ab..ba18efe 100644 --- a/src/main/java/com/baomidou/config/builder/ConfigBuilder.java +++ b/src/main/java/com/baomidou/config/builder/ConfigBuilder.java @@ -275,8 +275,8 @@ public class ConfigBuilder { private List getTablesInfo(StrategyConfig config) { boolean isInclude = (null != config.getInclude() && config.getInclude().length > 0); boolean isExclude = (null != config.getExclude() && config.getExclude().length > 0); - List includes = Arrays.asList(config.getInclude()); - List excludes = Arrays.asList(config.getExclude()); + List includes = Arrays.asList(Optional.ofNullable(config.getInclude()).orElse(new String[0])); + List excludes = Arrays.asList(Optional.ofNullable(config.getExclude()).orElse(new String[0])); List tableList = new ArrayList(); NamingStrategy strategy = config.getNaming(); NamingStrategy fieldStrategy = config.getFieldNaming(); -- Gitee From 235786807a1761962385778ae8ac125e14e47d0f Mon Sep 17 00:00:00 2001 From: artislong <15221499301@163.com> Date: Wed, 28 Apr 2021 15:30:14 +0800 Subject: [PATCH 10/11] =?UTF-8?q?=E5=A2=9E=E5=8A=A0swagger=E5=BC=80?= =?UTF-8?q?=E5=85=B3=EF=BC=8C=E4=BF=AE=E5=A4=8DBigDecimal=E4=B8=8D?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=AF=BC=E5=8C=85=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 ++- .../com/baomidou/AbstractGenerateMojo.java | 10 +++++ src/main/java/com/baomidou/GenerateMojo.java | 44 +++++++++++-------- .../com/baomidou/config/PackageConfig.java | 9 ++-- .../resources/template/controller.java.vm | 6 +++ src/main/resources/template/entity.java.vm | 18 ++++++-- 6 files changed, 67 insertions(+), 26 deletions(-) diff --git a/pom.xml b/pom.xml index c84899e..6ebd0b7 100644 --- a/pom.xml +++ b/pom.xml @@ -35,6 +35,10 @@ UTF-8 F4B46FB9 + UTF-8 + 8 + 8 + 8 @@ -86,7 +90,7 @@ org.apache.maven.plugins maven-plugin-plugin - 3.2 + 3.6.0 mp true diff --git a/src/main/java/com/baomidou/AbstractGenerateMojo.java b/src/main/java/com/baomidou/AbstractGenerateMojo.java index 8c1e2d3..3e1ee24 100644 --- a/src/main/java/com/baomidou/AbstractGenerateMojo.java +++ b/src/main/java/com/baomidou/AbstractGenerateMojo.java @@ -77,6 +77,12 @@ public abstract class AbstractGenerateMojo extends AbstractMojo { @Parameter(defaultValue = "true") private boolean activeRecord; + /** + * 开启 ActiveRecord 模式 + */ + @Parameter(defaultValue = "false") + private boolean enableSwagger; + protected ConfigBuilder config; /** @@ -117,4 +123,8 @@ public abstract class AbstractGenerateMojo extends AbstractMojo { return activeRecord; } + public boolean isEnableSwagger() { + return enableSwagger; + } + } diff --git a/src/main/java/com/baomidou/GenerateMojo.java b/src/main/java/com/baomidou/GenerateMojo.java index fdba0fa..a1982f4 100644 --- a/src/main/java/com/baomidou/GenerateMojo.java +++ b/src/main/java/com/baomidou/GenerateMojo.java @@ -1,17 +1,11 @@ package com.baomidou; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Properties; - +import com.baomidou.config.ConstVal; +import com.baomidou.config.TemplateConfig; +import com.baomidou.config.builder.ConfigBuilder; +import com.baomidou.config.po.TableField; +import com.baomidou.config.po.TableInfo; +import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.StringUtils; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; @@ -21,10 +15,10 @@ import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; import org.apache.velocity.app.VelocityEngine; -import com.baomidou.config.ConstVal; -import com.baomidou.config.TemplateConfig; -import com.baomidou.config.builder.ConfigBuilder; -import com.baomidou.config.po.TableInfo; +import java.io.*; +import java.text.SimpleDateFormat; +import java.util.*; +import java.util.stream.Collectors; /** * 生成文件 @@ -45,6 +39,7 @@ public class GenerateMojo extends AbstractGenerateMojo { */ private Map outputFiles; + @Override public void execute() throws MojoExecutionException, MojoFailureException { log.info("==========================准备生成文件...=========================="); // 初始化配置 @@ -89,7 +84,7 @@ public class GenerateMojo extends AbstractGenerateMojo { private Map analyzeData(ConfigBuilder config) { List tableList = config.getTableInfoList(); Map packageInfo = config.getPackageInfo(); - Map ctxData = new HashMap(); + Map ctxData = new HashMap<>(); String superEntityClass = getSuperClassName(config.getSuperEntityClass()); String superMapperClass = getSuperClassName(config.getSuperMapperClass()); String superServiceClass = getSuperClassName(config.getSuperServiceClass()); @@ -99,10 +94,19 @@ public class GenerateMojo extends AbstractGenerateMojo { for (TableInfo tableInfo : tableList) { VelocityContext ctx = new VelocityContext(); + List tableFields = tableInfo.getFields().stream() + .filter(tableField -> "BigDecimal".equalsIgnoreCase(tableField.getPropertyType())) + .collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(tableFields)) { + ctx.put("havBigDecimal", true); + } else { + ctx.put("havBigDecimal", false); + } + ctx.put("package", packageInfo); ctx.put("table", tableInfo); ctx.put("entity", tableInfo.getEntityName()); - ctx.put("addTabeName", !tableInfo.getEntityName().toLowerCase().equals(tableInfo.getName().toLowerCase())); + ctx.put("addTabeName", !tableInfo.getEntityName().equalsIgnoreCase(tableInfo.getName())); ctx.put("idGenType", config.getIdType()); ctx.put("superEntityClassPackage", config.getSuperEntityClass()); ctx.put("superEntityClass", superEntityClass); @@ -117,6 +121,7 @@ public class GenerateMojo extends AbstractGenerateMojo { ctx.put("enableCache", isEnableCache()); ctx.put("author", getAuthor()); ctx.put("activeRecord", isActiveRecord()); + ctx.put("enableSwagger", isEnableSwagger()); ctx.put("date", date); ctxData.put(tableInfo.getEntityName(), ctx); } @@ -130,8 +135,9 @@ public class GenerateMojo extends AbstractGenerateMojo { * @return */ private String getSuperClassName(String classPath) { - if (StringUtils.isBlank(classPath)) + if (StringUtils.isBlank(classPath)) { return null; + } return classPath.substring(classPath.lastIndexOf(".") + 1); } diff --git a/src/main/java/com/baomidou/config/PackageConfig.java b/src/main/java/com/baomidou/config/PackageConfig.java index fbd18d8..5f3cc35 100644 --- a/src/main/java/com/baomidou/config/PackageConfig.java +++ b/src/main/java/com/baomidou/config/PackageConfig.java @@ -58,8 +58,9 @@ public class PackageConfig { private String controller; public String getParent() { - if(moduleName != null && !"".equals(moduleName.trim())) - return parent + "." + moduleName; + if(moduleName != null && !"".equals(moduleName.trim())) { + return parent + "." + moduleName; + } return parent; } @@ -88,7 +89,9 @@ public class PackageConfig { } public String getController() { - if(StringUtils.isBlank(controller)) return "web"; + if(StringUtils.isBlank(controller)) { + return "web"; + } return controller; } } diff --git a/src/main/resources/template/controller.java.vm b/src/main/resources/template/controller.java.vm index f3394e1..09a153e 100644 --- a/src/main/resources/template/controller.java.vm +++ b/src/main/resources/template/controller.java.vm @@ -1,6 +1,9 @@ package ${package.Controller}; +#if(${enableSwagger}) + import io.swagger.annotations.Api; +#end import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RequestMapping; @@ -12,7 +15,10 @@ import org.springframework.web.bind.annotation.RequestMapping; * @author ${author} * @since ${date} */ +#if(${enableSwagger}) + @Api(tags = "${table.comment}") +#end @RestController @RequestMapping("#if(${package.ModuleName})/${package.ModuleName}#end/${table.entityName.toLowerCase()}") public class ${table.controllerName} { diff --git a/src/main/resources/template/entity.java.vm b/src/main/resources/template/entity.java.vm index 7e46767..38aca08 100644 --- a/src/main/resources/template/entity.java.vm +++ b/src/main/resources/template/entity.java.vm @@ -12,13 +12,19 @@ import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableName; #end +#if(${enableSwagger}) + import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; +#end import lombok.Data; import lombok.experimental.Accessors; import java.io.Serializable; +#if(${havBigDecimal}) +import java.math.BigDecimal; +#end #if(${table.hasDate}) import java.util.Date; #end @@ -33,7 +39,10 @@ import java.util.Date; */ @Data @Accessors(chain = true) +#if(${enableSwagger}) + @ApiModel("${table.comment}") +#end #if(${addTabeName}) @TableName("${table.name}") #end @@ -43,7 +52,7 @@ public class ${entity} extends Model<${entity}> { public class ${entity} implements Serializable{ #end -private static final long serialVersionUID=1L; + private static final long serialVersionUID=1L; #foreach($field in ${table.fields}) #if(${field.comment.equals("")}) @@ -56,11 +65,14 @@ private static final long serialVersionUID=1L; #if(${field.keyFlag} && ${idGenType}!="ID_WORKER") @TableId(type = IdType.${idGenType}) #end -@ApiModelProperty("${field.comment}") + #if(${enableSwagger}) + + @ApiModelProperty("${field.comment}") + #end #if($field.convert && ${field.name.toLowerCase()} != ${field.propertyName.toLowerCase()}) @TableField(value = "${field.name}") #end -private ${field.propertyType} ${field.propertyName}; + private ${field.propertyType} ${field.propertyName}; #end } -- Gitee From 2282052d2b97619dffab63e8282fe5c52a62a808 Mon Sep 17 00:00:00 2001 From: artislong <15221499301@163.com> Date: Wed, 28 Apr 2021 15:45:29 +0800 Subject: [PATCH 11/11] =?UTF-8?q?=E8=B0=83=E6=95=B4entity=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=A8=A1=E6=9D=BF=EF=BC=8C=E6=A0=BC=E5=BC=8F=E5=8C=96?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/template/entity.java.vm | 28 ++++++++-------------- 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/src/main/resources/template/entity.java.vm b/src/main/resources/template/entity.java.vm index 38aca08..745986b 100644 --- a/src/main/resources/template/entity.java.vm +++ b/src/main/resources/template/entity.java.vm @@ -11,16 +11,12 @@ import com.baomidou.mybatisplus.annotation.TableField; #if(${addTabeName}) import com.baomidou.mybatisplus.annotation.TableName; #end - #if(${enableSwagger}) - import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; #end - import lombok.Data; import lombok.experimental.Accessors; - import java.io.Serializable; #if(${havBigDecimal}) import java.math.BigDecimal; @@ -55,23 +51,19 @@ public class ${entity} implements Serializable{ private static final long serialVersionUID=1L; #foreach($field in ${table.fields}) - #if(${field.comment.equals("")}) - - #else - /** - * ${field.comment} - */ - #end - #if(${field.keyFlag} && ${idGenType}!="ID_WORKER") +#if(${field.comment.equals("")}) +#else + /** ${field.comment} */ +#end +#if(${field.keyFlag} && ${idGenType}!="ID_WORKER") @TableId(type = IdType.${idGenType}) - #end - #if(${enableSwagger}) - +#end +#if(${enableSwagger}) @ApiModelProperty("${field.comment}") - #end - #if($field.convert && ${field.name.toLowerCase()} != ${field.propertyName.toLowerCase()}) +#end +#if($field.convert && ${field.name.toLowerCase()} != ${field.propertyName.toLowerCase()}) @TableField(value = "${field.name}") - #end +#end private ${field.propertyType} ${field.propertyName}; #end -- Gitee