修改代码生成器模板.

This commit is contained in:
lijiahang
2023-07-12 16:14:12 +08:00
parent be2d72d483
commit d39ab1f32a
14 changed files with 689 additions and 48 deletions

View File

@@ -28,12 +28,6 @@
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- datasource -->
<dependency>
<groupId>com.orion.ops</groupId>
<artifactId>orion-ops-spring-boot-starter-datasource</artifactId>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>com.baomidou</groupId>
@@ -47,6 +41,19 @@
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
</dependency>
<!-- doc -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<scope>provided</scope>
</dependency>
<!-- mysql -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -3,6 +3,7 @@ package com.orion.ops.framework.mybatis.core.domain;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableLogic;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.apache.ibatis.type.JdbcType;
@@ -19,37 +20,28 @@ import java.util.Date;
@Data
public class BaseDO implements Serializable {
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
@Schema(description = "创建时间")
private Date createTime;
/**
* 更新时间
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
@Schema(description = "修改时间")
private Date updateTime;
/**
* 创建人
*/
@TableField(fill = FieldFill.INSERT, jdbcType = JdbcType.VARCHAR)
@Schema(description = "创建人")
private String creator;
/**
* 更新人
*/
@TableField(fill = FieldFill.INSERT_UPDATE, jdbcType = JdbcType.VARCHAR)
@Schema(description = "修改人")
private String updater;
/**
* 是否删除 0未删除 1已删除
*
* @see com.orion.ops.framework.common.constant.Const#NOT_DELETE
* @see com.orion.ops.framework.common.constant.Const#IS_DELETED
*/
@TableLogic
@Schema(description = "是否删除 0未删除 1已删除")
private Boolean deleted;
}

View File

@@ -2,10 +2,8 @@ package com.orion.ops.framework.mybatis.core.generator;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.builder.CustomFile;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
@@ -21,7 +19,11 @@ import org.apache.ibatis.annotations.Mapper;
import java.io.File;
/**
* 代码生成器
*
* @author Jiahang Li
* @version 1.0.0
* @since 2022/4/20 10:33
*/
public class CodeGenerator {
@@ -29,9 +31,9 @@ public class CodeGenerator {
String outputDir = "D:/MP/";
String author = Const.ORION_AUTHOR;
// 表名
String[] tables = {"user_info"};
String[] tables = {"test_table"};
// 模块
String module = "user";
String module = "infra";
// 连接
File yamlFile = new File("orion-ops-launch/src/main/resources/application-dev.yaml");
YmlExt yaml = YmlExt.load(yamlFile);
@@ -56,30 +58,83 @@ public class CodeGenerator {
String password,
String[] tables,
String module) {
// 获取全局配置
GlobalConfig globalConfig = getGlobalConfig(outputDir, author);
// 数据源配置
DataSourceConfig dataSourceConfig = getDataSourceConfig(url, username, password);
// 策略配置
StrategyConfig strategyConfig = getStrategyConfig(tables);
// 包名配置
PackageConfig packageConfig = getPackageConfig(module);
// 模板配置
TemplateConfig templateConfig = getTemplateConfig();
// 注入配置
InjectionConfig injectionConfig = getInjectionConfig();
// 整合配置
AutoGenerator ag = new AutoGenerator(dataSourceConfig)
// 整合全局配置
.global(globalConfig)
// 整合表名配置
.strategy(strategyConfig)
// 整合包名配置
.packageInfo(packageConfig)
// 整合模板配置
.template(templateConfig)
// 整合注入配置
.injection(injectionConfig);
// 执行
ag.execute();
}
/**
* 获取全局配置
*
* @param outputDir 输出地址
* @param author 作者
* @return config
*/
private static GlobalConfig getGlobalConfig(String outputDir, String author) {
// 全局配置
GlobalConfig gbConfig = new GlobalConfig.Builder()
// 设置作者
.author(author)
// 生成路径
.outputDir(outputDir)
// 生成 swagger 注解
.enableSwagger()
// 生成 spring doc 注解
.enableSpringdoc()
// date类型
.dateType(DateType.ONLY_DATE)
// 注释时间
.commentDate("yyyy-MM-dd")
.commentDate("yyyy-M-d HH:mm")
// 构建
.build();
return gbConfig;
}
// 数据源配置
/**
* 获取数据源配置
*
* @param url url
* @param username username
* @param password password
* @return 数据源配置
*/
private static DataSourceConfig getDataSourceConfig(String url, String username, String password) {
DataSourceConfig dsConfig = new DataSourceConfig.Builder(url, username, password)
// 转换器
.typeConvert(new MySqlTypeConvert() {
@Override
public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
if (fieldType.toLowerCase().contains("bit")) {
return DbColumnType.INTEGER;
}
if (fieldType.toLowerCase().contains("tinyint")) {
return DbColumnType.INTEGER;
}
@@ -90,8 +145,16 @@ public class CodeGenerator {
.dbQuery(new MySqlQuery())
// 构建
.build();
return dsConfig;
}
// 策略配置
/**
* 获取策略配置
*
* @param tables 生成的表名
* @return 策略配置
*/
private static StrategyConfig getStrategyConfig(String[] tables) {
StrategyConfig stConfig = new StrategyConfig.Builder()
// 生成的表
.addInclude(tables)
@@ -131,13 +194,41 @@ public class CodeGenerator {
.formatXmlFileName("%sMapper")
// 覆盖 mapper 文件
.enableFileOverride()
// controller 配置
.controllerBuilder()
// controller 文件名称
.formatFileName("%sController")
// 脊柱命名法
.enableHyphenStyle()
// @RestController
.enableRestStyle()
// 覆盖 controller 文件
.enableFileOverride()
// service 配置
.serviceBuilder()
// 覆盖 service 文件
.enableFileOverride()
// service 名称
.formatServiceFileName("%sService")
// service impl 名称
.formatServiceImplFileName("%sServiceImpl")
// 构建
.build();
return stConfig;
}
// 包名策略配置
/**
* 获取包名配置
*
* @param module 模块
* @return 包名配置
*/
private static PackageConfig getPackageConfig(String module) {
PackageConfig pkConfig = new PackageConfig.Builder()
// 声明父包
.parent("com.orion.ops.module." + module)
.parent("com.orion.ops.module")
// 模块名称
.moduleName(module)
// 实体类的包
.entity("entity.domain")
// 映射接口的包
@@ -152,23 +243,77 @@ public class CodeGenerator {
.controller("controller")
// 构建
.build();
return pkConfig;
}
// 整合配置
AutoGenerator ag = new AutoGenerator(dsConfig)
// 整合全局配置
.global(gbConfig)
// 整合表名配置
.strategy(stConfig)
// 整合包名策略
.packageInfo(pkConfig)
// TODO 自定义convert文件 request VO
// .injection()
// TODO 自定义模板以及convert文件
// .template()
;
/**
* 获取模板配置
*
* @return 模板配置
*/
private static TemplateConfig getTemplateConfig() {
TemplateConfig tplConfig = new TemplateConfig.Builder()
.controller("/templates/orion-controller.java.vm")
.entity("/templates/orion-domain.java.vm")
.service("/templates/orion-service.java.vm")
.serviceImpl("/templates/orion-service-impl.java.vm")
.mapper("/templates/orion-dao.java.vm")
.xml("/templates/orion-mapper.xml")
.build();
return tplConfig;
}
// 执行
ag.execute();
/**
* 获取注入配置
*
* @return 注入配置
*/
private static InjectionConfig getInjectionConfig() {
// vo 文件
CustomFile voFile = new CustomFile.Builder()
.enableFileOverride()
.templatePath("/templates/orion-vo.java.vm")
.fileName("VO.java")
.packageName("vo")
.build();
// dto 文件
CustomFile dtoFile = new CustomFile.Builder()
.enableFileOverride()
.templatePath("/templates/orion-dto.java.vm")
.fileName("DTO.java")
.packageName("dto")
.build();
// request 文件
CustomFile requestFile = new CustomFile.Builder()
.enableFileOverride()
.templatePath("/templates/orion-request.java.vm")
.fileName("Request.java")
.packageName("request")
.build();
// convert 文件
CustomFile convertFile = new CustomFile.Builder()
.enableFileOverride()
.templatePath("/templates/orion-convert.java.vm")
.fileName("Convert.java")
.packageName("convert")
.build();
// 注入配置
InjectionConfig injection = new InjectionConfig.Builder()
// vo 文件
.customFile(voFile)
// dto 文件
.customFile(dtoFile)
// request 文件
.customFile(requestFile)
// convert 文件
.customFile(convertFile)
// 构建
.build();
return injection;
}
}

View File

@@ -2,6 +2,7 @@ package com.orion.ops.framework.mybatis.core.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.orion.ops.framework.mybatis.core.query.CacheQuery;
import com.orion.ops.framework.mybatis.core.query.DataQuery;
import java.util.Collection;
@@ -97,4 +98,13 @@ public interface IMapper<T> extends BaseMapper<T> {
return DataQuery.of(this);
}
/**
* 获取 CacheQuery 对象
*
* @return CacheQuery
*/
default CacheQuery<T> cache() {
return CacheQuery.of(this);
}
}

View File

@@ -0,0 +1,46 @@
package ${package.Controller};
import com.orion.ops.framework.common.annotation.RestWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
#if(${restControllerStyle})
import org.springframework.web.bind.annotation.RestController;
#else
import org.springframework.stereotype.Controller;
#end
#if(${superControllerClassPackage})
import ${superControllerClassPackage};
#end
import lombok.extern.slf4j.Slf4j;
/**
* $!{table.comment} api
*
* @author ${author}
* @version 1.0.0
* @since ${date}
*/
@Tag(name = "${package.ModuleName} - $!{table.comment}服务")
@Slf4j
@Validated
@RestWrapper
#if(${restControllerStyle})
@RestController
#else
@Controller
#end
@RequestMapping("#if(${package.ModuleName})/${package.ModuleName}#end/#if(${controllerMappingHyphenStyle})${controllerMappingHyphen}#else${table.entityPath}#end")
#if(${kotlin})
class ${table.controllerName}#if(${superControllerClass}) : ${superControllerClass}()#end
#else
#if(${superControllerClass})
public class ${table.controllerName} extends ${superControllerClass} {
#else
public class ${table.controllerName} {
#end
}
#end

View File

@@ -0,0 +1,41 @@
package ${package.Entity};
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
/**
* $!{table.comment} 转换器
*
* @author ${author}
* @version 1.0.0
* @since ${date}
*/
@Mapper
@SuppressWarnings("ALL")
public interface ${entity}Convert {
${entity}Convert MAPPER = Mappers.getMapper(${entity}Convert.class);
${entity} toDomain(${entity}Request request);
${entity}DTO toDto(${entity}Request request);
${entity}VO toVo(${entity} domain);
${entity}DTO toDto(${entity} domain);
List<${entity}VO> toVoListForDomain(List<${entity}> domain);
List<${entity}DTO> toDtoListForDomain(List<${entity}> domain);
${entity}VO toVo(${entity}DTO dto);
${entity} toDto(${entity}DTO dto);
List<${entity}VO> toVoListForDto(List<${entity}DTO> dto);
List<${entity}> toDoListForDto(List<${entity}DTO> dto);
}

View File

@@ -0,0 +1,24 @@
package ${package.Mapper};
import ${package.Entity}.${entity};
import ${superMapperClassPackage};
#if(${mapperAnnotationClass})
import ${mapperAnnotationClass.name};
#end
/**
* $!{table.comment} Mapper 接口
*
* @author ${author}
* @since ${date}
*/
#if(${mapperAnnotationClass})
@${mapperAnnotationClass.simpleName}
#end
#if(${kotlin})
interface ${table.mapperName} : ${superMapperClass}<${entity}>
#else
public interface ${table.mapperName} extends ${superMapperClass}<${entity}> {
}
#end

View File

@@ -0,0 +1,164 @@
package ${package.Entity};
#foreach($pkg in ${table.importPackages})
import ${pkg};
#end
#if(${springdoc})
import io.swagger.v3.oas.annotations.media.Schema;
#elseif(${swagger})
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
#end
#if(${entityLombokModel})
import lombok.Data;
import lombok.EqualsAndHashCode;
#if(${chainModel})
import lombok.experimental.Accessors;
#end
#end
/**
* $!{table.comment}
*
* @author ${author}
* @version 1.0.0
* @since ${date}
*/
#if(${entityLombokModel})
@Data
@EqualsAndHashCode(callSuper = true)
#if(${chainModel})
@Accessors(chain = true)
#end
#end
#if(${table.convert})
@TableName("${schemaName}${table.name}")
#end
#if(${springdoc})
@Schema(name = "${entity}", description = "$!{table.comment}")
#elseif(${swagger})
@ApiModel(value = "${entity}对象", description = "$!{table.comment}")
#end
#if(${superEntityClass})
public class ${entity} extends ${superEntityClass}#if(${activeRecord})<${entity}>#end {
#elseif(${activeRecord})
public class ${entity} extends Model<${entity}> {
#elseif(${entitySerialVersionUID})
public class ${entity} implements Serializable{
#else
public class ${entity} {
#end
#if(${entitySerialVersionUID})
private static final long serialVersionUID=1L;
#end
## ---------- BEGIN 字段循环遍历 ----------
#foreach($field in ${table.fields})
#if(${field.keyFlag})
#set($keyPropertyName=${field.propertyName})
#end
#if("$!field.comment" != "")
#if(${springdoc})
@Schema(description = "${field.comment}")
#elseif(${swagger})
@ApiModelProperty("${field.comment}")
#else
/**
* ${field.comment}
*/
#end
#end
#if(${field.keyFlag})
## 主键
#if(${field.keyIdentityFlag})
@TableId(value = "${field.annotationColumnName}", type = IdType.AUTO)
#elseif(!$null.isNull(${idType}) && "$!idType" != "")
@TableId(value = "${field.annotationColumnName}", type = IdType.${idType})
#elseif(${field.convert})
@TableId("${field.annotationColumnName}")
#end
## 普通字段
#elseif(${field.fill})
## ----- 存在字段填充设置 -----
#if(${field.convert})
@TableField(value = "${field.annotationColumnName}", fill = FieldFill.${field.fill})
#else
@TableField(fill = FieldFill.${field.fill})
#end
#elseif(${field.convert})
@TableField("${field.annotationColumnName}")
#end
## 乐观锁注解
#if(${field.versionField})
@Version
#end
## 逻辑删除注解
#if(${field.logicDeleteField})
@TableLogic
#end
private ${field.propertyType} ${field.propertyName};
#if(!$foreach.hasNext)
#end
#end
## ---------- END 字段循环遍历 ----------
#if(!${entityLombokModel})
#foreach($field in ${table.fields})
#if(${field.propertyType.equals("boolean")})
#set($getprefix="is")
#else
#set($getprefix="get")
#end
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}) {
#end
this.${field.propertyName} = ${field.propertyName};
#if(${chainModel})
return this;
#end
}
#end
## --foreach end---
#end
## --end of #if(!${entityLombokModel})--
#if(${entityColumnConstant})
#foreach($field in ${table.fields})
public static final String ${field.name.toUpperCase()} ="${field.name}";
#end
#end
#if(${activeRecord})
@Override
public Serializable pkVal(){
#if(${keyPropertyName})
return this.${keyPropertyName};
#else
return null;
#end
}
#end
#if(!${entityLombokModel})
@Override
public String toString() {
return "${entity}{" +
#foreach($field in ${table.fields})
#if($!{foreach.index}==0)
"${field.propertyName} = " + ${field.propertyName} +
#else
", ${field.propertyName} = " + ${field.propertyName} +
#end
#end
"}";
}
#end
}

View File

@@ -0,0 +1,47 @@
package ${package.Entity};
#foreach($pkg in ${table.importPackages})
import ${pkg};
#end
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.util.Date;
/**
* $!{table.comment}
*
* @author ${author}
* @version 1.0.0
* @since ${date}
*/
@Data
@Schema(name = "${entity}DTO", description = "$!{table.comment}")
public class ${entity}DTO implements Serializable {
private static final long serialVersionUID = 1L;
## ---------- BEGIN 字段循环遍历 ----------
#foreach($field in ${table.fields})
#if(${field.keyFlag})
#set($keyPropertyName=${field.propertyName})
#end
#if("$!field.comment" != "")
@Schema(description = "${field.comment}")
#end
private ${field.propertyType} ${field.propertyName};
#end
@Schema(description = "创建时间")
private Date createTime;
@Schema(description = "修改时间")
private Date updateTime;
@Schema(description = "创建人")
private String creator;
@Schema(description = "修改人")
private String updater;
}

View 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}"/>
#end
#if(${baseResultMap})
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="${package.Entity}.${entity}">
#foreach($field in ${table.fields})
#if(${field.keyFlag})##生成主键排在第一位
<id column="${field.name}" property="${field.propertyName}"/>
#end
#end
#foreach($field in ${table.commonFields})##生成公共字段
<result column="${field.name}" property="${field.propertyName}"/>
#end
#foreach($field in ${table.fields})
#if(!${field.keyFlag})##生成普通字段
<result column="${field.name}" property="${field.propertyName}"/>
#end
#end
</resultMap>
#end
#if(${baseColumnList})
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
#foreach($field in ${table.commonFields})
${field.columnName},
#end
${table.fieldNames}
</sql>
#end
</mapper>

View File

@@ -0,0 +1,33 @@
package ${package.Entity};
#foreach($pkg in ${table.importPackages})
import ${pkg};
#end
import com.orion.lang.define.wrapper.PageRequest;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* $!{table.comment}
*
* @author ${author}
* @version 1.0.0
* @since ${date}
*/
@Data
@EqualsAndHashCode(callSuper = true)
@Schema(name = "${entity}Request", description = "$!{table.comment}")
public class ${entity}Request extends PageRequest {
#foreach($field in ${table.fields})
#if(${field.keyFlag})
#set($keyPropertyName=${field.propertyName})
#end
#if("$!field.comment" != "")
@Schema(description = "${field.comment}")
#end
private ${field.propertyType} ${field.propertyName};
#end
}

View File

@@ -0,0 +1,28 @@
package ${package.ServiceImpl};
import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
#if(${table.serviceInterface})
import ${package.Service}.${table.serviceName};
#end
import ${superServiceImplClassPackage};
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
* $!{table.comment} 服务实现类
*
* @author ${author}
* @since ${date}
*/
@Slf4j
@Service
#if(${kotlin})
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>()#if(${table.serviceInterface}), ${table.serviceName}#end {
}
#else
public class ${table.serviceImplName} implements ${table.serviceName} {
}
#end

View File

@@ -0,0 +1,18 @@
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} {
}
#end

View File

@@ -0,0 +1,47 @@
package ${package.Entity};
#foreach($pkg in ${table.importPackages})
import ${pkg};
#end
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.util.Date;
/**
* $!{table.comment} VO
*
* @author ${author}
* @version 1.0.0
* @since ${date}
*/
@Data
@Schema(name = "${entity}VO", description = "$!{table.comment}")
public class ${entity}VO implements Serializable {
private static final long serialVersionUID = 1L;
## ---------- BEGIN 字段循环遍历 ----------
#foreach($field in ${table.fields})
#if(${field.keyFlag})
#set($keyPropertyName=${field.propertyName})
#end
#if("$!field.comment" != "")
@Schema(description = "${field.comment}")
#end
private ${field.propertyType} ${field.propertyName};
#end
@Schema(description = "创建时间")
private Date createTime;
@Schema(description = "修改时间")
private Date updateTime;
@Schema(description = "创建人")
private String creator;
@Schema(description = "修改人")
private String updater;
}