+ * ${table.comment!} 前端控制器 + *
+ * + * @author ${author} + * @since ${date} + */ +<#if restControllerStyle> +@RestController +<#else> +@Controller +#if> +@RequestMapping("<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}#if>/<#if controllerMappingHyphenStyle>${controllerMappingHyphen}<#else>${table.entityPath}#if>") +<#if kotlin> +class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()#if> +<#else> +<#if superControllerClass??> +public class ${table.controllerName} extends ${superControllerClass} { +<#else> +public class ${table.controllerName} { +#if> + +} +#if> 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> + +<#list importEntityJavaPackages as pkg> +import ${pkg}; +#list> + +/** + *+ * ${table.comment!} + *
+ * + * @author ${author} + * @since ${date} + */ +<#list entityClassAnnotations as an> +${an.displayName} +#list> +<#if superEntityClass??> +public class ${entity} extends ${superEntityClass}<#if activeRecord><${entity}>#if> { +<#elseif activeRecord> +public class ${entity} extends Model<${entity}> { +<#elseif entitySerialVersionUID> +public class ${entity} implements Serializable { +<#else> +public class ${entity} { +#if> +<#if entitySerialVersionUID> + + <#if entitySerialAnnotation> + @Serial + #if> + private static final long serialVersionUID = 1L; +#if> +<#-- ---------- BEGIN 字段循环遍历 ----------> +<#list table.fields as field> + <#if field.keyFlag> + <#assign keyPropertyName="${field.propertyName}"/> + #if> + + <#if field.comment!?length gt 0> + <#if entityFieldUseJavaDoc> + /** + * ${field.comment} + */ + #if> + #if> + <#list field.annotationAttributesList as an> + ${an.displayName} + #list> + private ${field.propertyType} ${field.propertyName}; +#list> +<#------------ END 字段循环遍历 ----------> +<#if !entityLombokModel> + <#list table.fields as field> + <#if field.propertyType == "boolean"> + <#assign getprefix="is"/> + <#else> + <#assign getprefix="get"/> + #if> + + 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}) { + #if> + this.${field.propertyName} = ${field.propertyName}; + <#if chainModel> + return this; + #if> + } + #list> +#if> +<#if entityColumnConstant> + <#list table.fields as field> + + public static final String ${field.name?upper_case} = "${field.name}"; + #list> +#if> +<#if activeRecord> + + @Override + public Serializable pkVal() { + <#if keyPropertyName??> + return this.${keyPropertyName}; + <#else> + return null; + #if> + } +#if> +<#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} + + #if> + #list> + "}"; + } +#if> +} 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}; +#list> +<#if importMapperJavaPackages?size !=0> + + <#list importMapperJavaPackages as pkg> +import ${pkg}; + #list> +#if> + +/** + *+ * ${table.comment!} Mapper 接口 + *
+ * + * @author ${author} + * @since ${date} + */ +<#if mapperAnnotationClass??> +@${mapperAnnotationClass.simpleName} +#if> +<#if kotlin> +interface ${table.mapperName} : ${superMapperClass}<${entity}> { +<#else> +public interface ${table.mapperName} extends ${superMapperClass}<${entity}> { +#if> + +<#list mapperMethodList as m> + /** + * generate by ${m.indexName} + * + <#list m.tableFieldList as f> + * @param ${f.propertyName} ${f.comment} + #list> + */ + ${m.method} +#list> +} 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 @@ + + ++ * ${table.comment!} 服务类 + *
+ * + * @author ${author} + * @since ${date} + */ +<#if kotlin> +interface ${table.serviceName} : ${superServiceClass}<${entity}> +<#else> +public interface ${table.serviceName} extends ${superServiceClass}<${entity}> { + +} +#if> 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}; +#if> +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}#if> { + +} +<#else> +public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}><#if generateService> implements ${table.serviceName}#if> { + +} +#if> 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(); + } +}