From 0b6ee12e9ec1cd03f4cadbd2e2392a36587699f3 Mon Sep 17 00:00:00 2001 From: SerLiunx <17689543@qq.com> Date: Sat, 9 Aug 2025 00:22:00 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=BC=95=E5=85=A5=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=94=9F=E6=88=90.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 16 +++ .../resources/templates/controller.java.ftl | 37 ++++++ src/main/resources/templates/entity.java.ftl | 113 ++++++++++++++++++ src/main/resources/templates/mapper.java.ftl | 40 +++++++ src/main/resources/templates/mapper.xml.ftl | 39 ++++++ src/main/resources/templates/service.java.ftl | 20 ++++ .../resources/templates/serviceImpl.java.ftl | 28 +++++ .../templates/以上为代码生成的模板文件 | 0 .../java/generator/SCloudCodeGenerator.java | 59 +++++++++ 9 files changed, 352 insertions(+) create mode 100644 src/main/resources/templates/controller.java.ftl create mode 100644 src/main/resources/templates/entity.java.ftl create mode 100644 src/main/resources/templates/mapper.java.ftl create mode 100644 src/main/resources/templates/mapper.xml.ftl create mode 100644 src/main/resources/templates/service.java.ftl create mode 100644 src/main/resources/templates/serviceImpl.java.ftl create mode 100644 src/main/resources/templates/以上为代码生成的模板文件 create mode 100644 src/test/java/generator/SCloudCodeGenerator.java diff --git a/pom.xml b/pom.xml index 2a6a793..44c3e15 100644 --- a/pom.xml +++ b/pom.xml @@ -20,6 +20,8 @@ 9.4.0 3.5.12 + + 2.3.34 @@ -57,6 +59,20 @@ + + + com.baomidou + mybatis-plus-generator + ${mybatis-plus.version} + + + + + org.freemarker + freemarker + ${freemarker.version} + + org.springframework.boot spring-boot-starter-jdbc diff --git a/src/main/resources/templates/controller.java.ftl b/src/main/resources/templates/controller.java.ftl new file mode 100644 index 0000000..2067be0 --- /dev/null +++ b/src/main/resources/templates/controller.java.ftl @@ -0,0 +1,37 @@ +package ${package.Controller}; + +import org.springframework.web.bind.annotation.RequestMapping; +<#if restControllerStyle> +import org.springframework.web.bind.annotation.RestController; +<#else> +import org.springframework.stereotype.Controller; + +<#if superControllerClassPackage??> +import ${superControllerClassPackage}; + + +/** + *

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

+ * + * @author ${author} + * @since ${date} + */ +<#if restControllerStyle> +@RestController +<#else> +@Controller + +@RequestMapping("<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}/<#if controllerMappingHyphenStyle>${controllerMappingHyphen}<#else>${table.entityPath}") +<#if kotlin> +class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}() +<#else> +<#if superControllerClass??> +public class ${table.controllerName} extends ${superControllerClass} { +<#else> +public class ${table.controllerName} { + + +} + diff --git a/src/main/resources/templates/entity.java.ftl b/src/main/resources/templates/entity.java.ftl new file mode 100644 index 0000000..aa09ff1 --- /dev/null +++ b/src/main/resources/templates/entity.java.ftl @@ -0,0 +1,113 @@ +package ${package.Entity}; + +<#list importEntityFrameworkPackages as pkg> +import ${pkg}; + + +<#list importEntityJavaPackages as pkg> +import ${pkg}; + + +/** + *

+ * ${table.comment!} + *

+ * + * @author ${author} + * @since ${date} + */ +<#list entityClassAnnotations as an> +${an.displayName} + +<#if superEntityClass??> +public class ${entity} extends ${superEntityClass}<#if activeRecord><${entity}> { +<#elseif activeRecord> +public class ${entity} extends Model<${entity}> { +<#elseif entitySerialVersionUID> +public class ${entity} implements Serializable { +<#else> +public class ${entity} { + +<#if entitySerialVersionUID> + + <#if entitySerialAnnotation> + @Serial + + private static final long serialVersionUID = 1L; + +<#-- ---------- BEGIN 字段循环遍历 ----------> +<#list table.fields as field> + <#if field.keyFlag> + <#assign keyPropertyName="${field.propertyName}"/> + + + <#if field.comment!?length gt 0> + <#if entityFieldUseJavaDoc> + /** + * ${field.comment} + */ + + + <#list field.annotationAttributesList as an> + ${an.displayName} + + private ${field.propertyType} ${field.propertyName}; + +<#------------ END 字段循环遍历 ----------> +<#if !entityLombokModel> + <#list table.fields as field> + <#if field.propertyType == "boolean"> + <#assign getprefix="is"/> + <#else> + <#assign getprefix="get"/> + + + public ${field.propertyType} ${getprefix}${field.capitalName}() { + return ${field.propertyName}; + } + + <#if chainModel> + public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) { + <#else> + public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) { + + this.${field.propertyName} = ${field.propertyName}; + <#if chainModel> + return this; + + } + + +<#if entityColumnConstant> + <#list table.fields as field> + + public static final String ${field.name?upper_case} = "${field.name}"; + + +<#if activeRecord> + + @Override + public Serializable pkVal() { + <#if keyPropertyName??> + return this.${keyPropertyName}; + <#else> + return null; + + } + +<#if !entityLombokModel && entityToString> + + @Override + public String toString() { + return "${entity}{" + + <#list table.fields as field> + <#if field_index==0> + "${field.propertyName} = " + ${field.propertyName} + + <#else> + ", ${field.propertyName} = " + ${field.propertyName} + + + + "}"; + } + +} diff --git a/src/main/resources/templates/mapper.java.ftl b/src/main/resources/templates/mapper.java.ftl new file mode 100644 index 0000000..45d76e6 --- /dev/null +++ b/src/main/resources/templates/mapper.java.ftl @@ -0,0 +1,40 @@ +package ${package.Mapper}; + +<#list importMapperFrameworkPackages as pkg> +import ${pkg}; + +<#if importMapperJavaPackages?size !=0> + + <#list importMapperJavaPackages as pkg> +import ${pkg}; + + + +/** + *

+ * ${table.comment!} Mapper 接口 + *

+ * + * @author ${author} + * @since ${date} + */ +<#if mapperAnnotationClass??> +@${mapperAnnotationClass.simpleName} + +<#if kotlin> +interface ${table.mapperName} : ${superMapperClass}<${entity}> { +<#else> +public interface ${table.mapperName} extends ${superMapperClass}<${entity}> { + + +<#list mapperMethodList as m> + /** + * generate by ${m.indexName} + * + <#list m.tableFieldList as f> + * @param ${f.propertyName} ${f.comment} + + */ + ${m.method} + +} diff --git a/src/main/resources/templates/mapper.xml.ftl b/src/main/resources/templates/mapper.xml.ftl new file mode 100644 index 0000000..851af12 --- /dev/null +++ b/src/main/resources/templates/mapper.xml.ftl @@ -0,0 +1,39 @@ + + + + +<#if enableCache> + + + + +<#if baseResultMap> + + +<#list table.fields as field> +<#if field.keyFlag><#--生成主键排在第一位--> + + + +<#list table.commonFields as field><#--生成公共字段 --> + + +<#list table.fields as field> +<#if !field.keyFlag><#--生成普通字段 --> + + + + + + +<#if baseColumnList> + + +<#list table.commonFields as field> + ${field.columnName}, + + ${table.fieldNames} + + + + diff --git a/src/main/resources/templates/service.java.ftl b/src/main/resources/templates/service.java.ftl new file mode 100644 index 0000000..e3232f3 --- /dev/null +++ b/src/main/resources/templates/service.java.ftl @@ -0,0 +1,20 @@ +package ${package.Service}; + +import ${package.Entity}.${entity}; +import ${superServiceClassPackage}; + +/** + *

+ * ${table.comment!} 服务类 + *

+ * + * @author ${author} + * @since ${date} + */ +<#if kotlin> +interface ${table.serviceName} : ${superServiceClass}<${entity}> +<#else> +public interface ${table.serviceName} extends ${superServiceClass}<${entity}> { + +} + diff --git a/src/main/resources/templates/serviceImpl.java.ftl b/src/main/resources/templates/serviceImpl.java.ftl new file mode 100644 index 0000000..e24e816 --- /dev/null +++ b/src/main/resources/templates/serviceImpl.java.ftl @@ -0,0 +1,28 @@ +package ${package.ServiceImpl}; + +import ${package.Entity}.${entity}; +import ${package.Mapper}.${table.mapperName}; +<#if generateService> +import ${package.Service}.${table.serviceName}; + +import ${superServiceImplClassPackage}; +import org.springframework.stereotype.Service; + +/** + *

+ * ${table.comment!} 服务实现类 + *

+ * + * @author ${author} + * @since ${date} + */ +@Service +<#if kotlin> +open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>()<#if generateService>, ${table.serviceName} { + +} +<#else> +public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}><#if generateService> implements ${table.serviceName} { + +} + diff --git a/src/main/resources/templates/以上为代码生成的模板文件 b/src/main/resources/templates/以上为代码生成的模板文件 new file mode 100644 index 0000000..e69de29 diff --git a/src/test/java/generator/SCloudCodeGenerator.java b/src/test/java/generator/SCloudCodeGenerator.java new file mode 100644 index 0000000..89c4f4c --- /dev/null +++ b/src/test/java/generator/SCloudCodeGenerator.java @@ -0,0 +1,59 @@ +package generator; + +import com.baomidou.mybatisplus.generator.FastAutoGenerator; +import com.baomidou.mybatisplus.generator.config.OutputFile; +import com.baomidou.mybatisplus.generator.config.rules.DbColumnType; +import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; + +import java.sql.Types; +import java.util.Collections; + +/** + * 代码生成器 + * + * @author SerLiunx + * @since 2025/8/9 + */ +public class SCloudCodeGenerator { + + private static final String outputPath = "C:\\Users\\serliunx\\Desktop\\code-generate"; + + public static void main(String[] args) { + FastAutoGenerator.create(args[0], args[1], args[2]) + .globalConfig(builder -> { + builder.author("SerLiunx") // 设置作者 + .outputDir(outputPath); // 指定输出目录 + }) + .dataSourceConfig(builder -> + builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> { + int typeCode = metaInfo.getJdbcType().TYPE_CODE; + if (typeCode == Types.SMALLINT) { + // 自定义类型转换 + return DbColumnType.INTEGER; + } + return typeRegistry.getColumnType(metaInfo); + }) + ) + .packageConfig(builder -> + builder.parent("com.serliunx.blog") // 设置父包名 + .pathInfo(Collections.singletonMap(OutputFile.xml, outputPath)) // 设置mapperXml生成路径 + ) + .strategyConfig(builder -> + builder.addInclude("pb_article", "pb_tag", "pb_article_tag") // 设置需要生成的表名 + .entityBuilder() + .enableLombok() + .disableSerialVersionUID() + .enableTableFieldAnnotation() + .controllerBuilder() + .template("/templates/controller.java") + .enableHyphenStyle() + .enableRestStyle() + .serviceBuilder() + .formatServiceFileName("%sService") + .mapperBuilder() + .disableMapperXml() + ) + .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板 + .execute(); + } +}