feat: 引入代码生成.
This commit is contained in:
16
pom.xml
16
pom.xml
@@ -20,6 +20,8 @@
|
||||
<mysql-connector.version>9.4.0</mysql-connector.version>
|
||||
<!-- mybatis-plus 版本 -->
|
||||
<mybatis-plus.version>3.5.12</mybatis-plus.version>
|
||||
<!-- freemarker 版本 -->
|
||||
<freemarker.version>2.3.34</freemarker.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@@ -57,6 +59,20 @@
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<!--mybatis plus 代码生成器 -->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-generator</artifactId>
|
||||
<version>${mybatis-plus.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- freemarker 模板引擎 -->
|
||||
<dependency>
|
||||
<groupId>org.freemarker</groupId>
|
||||
<artifactId>freemarker</artifactId>
|
||||
<version>${freemarker.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||
|
||||
37
src/main/resources/templates/controller.java.ftl
Normal file
37
src/main/resources/templates/controller.java.ftl
Normal file
@@ -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>
|
||||
<#if superControllerClassPackage??>
|
||||
import ${superControllerClassPackage};
|
||||
</#if>
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* ${table.comment!} 前端控制器
|
||||
* </p>
|
||||
*
|
||||
* @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>
|
||||
113
src/main/resources/templates/entity.java.ftl
Normal file
113
src/main/resources/templates/entity.java.ftl
Normal file
@@ -0,0 +1,113 @@
|
||||
package ${package.Entity};
|
||||
|
||||
<#list importEntityFrameworkPackages as pkg>
|
||||
import ${pkg};
|
||||
</#list>
|
||||
|
||||
<#list importEntityJavaPackages as pkg>
|
||||
import ${pkg};
|
||||
</#list>
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* ${table.comment!}
|
||||
* </p>
|
||||
*
|
||||
* @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>
|
||||
}
|
||||
40
src/main/resources/templates/mapper.java.ftl
Normal file
40
src/main/resources/templates/mapper.java.ftl
Normal file
@@ -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>
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* ${table.comment!} Mapper 接口
|
||||
* </p>
|
||||
*
|
||||
* @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>
|
||||
}
|
||||
39
src/main/resources/templates/mapper.xml.ftl
Normal file
39
src/main/resources/templates/mapper.xml.ftl
Normal file
@@ -0,0 +1,39 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="${package.Mapper}.${table.mapperName}">
|
||||
|
||||
<#if enableCache>
|
||||
<!-- 开启二级缓存 -->
|
||||
<cache type="${cacheClassName}"/>
|
||||
|
||||
</#if>
|
||||
<#if baseResultMap>
|
||||
<!-- 通用查询映射结果 -->
|
||||
<resultMap id="BaseResultMap" type="${package.Entity}.${entity}">
|
||||
<#list table.fields as field>
|
||||
<#if field.keyFlag><#--生成主键排在第一位-->
|
||||
<id column="${field.name}" property="${field.propertyName}" />
|
||||
</#if>
|
||||
</#list>
|
||||
<#list table.commonFields as field><#--生成公共字段 -->
|
||||
<result column="${field.name}" property="${field.propertyName}" />
|
||||
</#list>
|
||||
<#list table.fields as field>
|
||||
<#if !field.keyFlag><#--生成普通字段 -->
|
||||
<result column="${field.name}" property="${field.propertyName}" />
|
||||
</#if>
|
||||
</#list>
|
||||
</resultMap>
|
||||
|
||||
</#if>
|
||||
<#if baseColumnList>
|
||||
<!-- 通用查询结果列 -->
|
||||
<sql id="Base_Column_List">
|
||||
<#list table.commonFields as field>
|
||||
${field.columnName},
|
||||
</#list>
|
||||
${table.fieldNames}
|
||||
</sql>
|
||||
|
||||
</#if>
|
||||
</mapper>
|
||||
20
src/main/resources/templates/service.java.ftl
Normal file
20
src/main/resources/templates/service.java.ftl
Normal file
@@ -0,0 +1,20 @@
|
||||
package ${package.Service};
|
||||
|
||||
import ${package.Entity}.${entity};
|
||||
import ${superServiceClassPackage};
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* ${table.comment!} 服务类
|
||||
* </p>
|
||||
*
|
||||
* @author ${author}
|
||||
* @since ${date}
|
||||
*/
|
||||
<#if kotlin>
|
||||
interface ${table.serviceName} : ${superServiceClass}<${entity}>
|
||||
<#else>
|
||||
public interface ${table.serviceName} extends ${superServiceClass}<${entity}> {
|
||||
|
||||
}
|
||||
</#if>
|
||||
28
src/main/resources/templates/serviceImpl.java.ftl
Normal file
28
src/main/resources/templates/serviceImpl.java.ftl
Normal file
@@ -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;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* ${table.comment!} 服务实现类
|
||||
* </p>
|
||||
*
|
||||
* @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>
|
||||
0
src/main/resources/templates/以上为代码生成的模板文件
Normal file
0
src/main/resources/templates/以上为代码生成的模板文件
Normal file
59
src/test/java/generator/SCloudCodeGenerator.java
Normal file
59
src/test/java/generator/SCloudCodeGenerator.java
Normal file
@@ -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 <a href="mailto:root@serliunx.com">SerLiunx</a>
|
||||
* @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("<a href=\"mailto:root@serliunx.com\">SerLiunx</a>") // 设置作者
|
||||
.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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user