Files
xinghe-codegen/doc/模版.md
2026-01-21 13:53:48 +08:00

8.2 KiB

stringtemplate4

设置 $

STGroup group = new STGroupString("db_template",template, '$', '$');

模版的定义

基础

classTemplate(item, module, operationList, className) ::= <<
>>

注意 <<内容>> 定义的方法

classTemplate 为 group.getInstanceOf("classTemplate"); 定义

例子:

classTemplate(item, module, operationList, className) ::= <<
package $module.packageName$;
/**
 * @author xia
 * @date 2026/1/10
 * @version 0.0.1
 */
public class $className$ {

}
>>

映射字典

// 定义一个映射字典
requestAnnotations ::= [
    "POST": "@PostMapping",
    "GET": "@GetMapping",
    "PUT": "@PutMapping",
    "DELETE": "@DeleteMapping",
    default: "@RequestMapping" 
]

Controller

requestAnnotations ::= [
    "POST": "@PostMapping",
    "GET": "@GetMapping",
    "PUT": "@PutMapping",
    "DELETE": "@DeleteMapping",
    default: "@RequestMapping" 
]

classTemplate(module,item,operationList,basics) ::= <<
package $module.packageName$.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

/**	
 * $item.itemName$ 控制器
 * @author xia
 */
@Tag(name = "$item.itemName$")
@RequiredArgsConstructor
@RestController
@RequestMapping("/$item.itemCode$")
public class $basics.itemCodeUp$Controller {

    private final $basics.itemCodeUp$Service $item.itemCode$Service;
    
$operationList:{op | 

	@OperLog
	@SecureAudit(id = $op.id$,$if(op.isGreenLight)$isGreenLight = true,$endif$ $if(!item.isTenant)$isTenant = false,$endif$
		funType = FunTypeEnum.$op.funType$, funName = "$op.funName$", funCode = "$module.moduleCode$:$item.itemCode$:$op.operationCode$")
	@Operation(summary = "$op.funName$", description = "$op.describe$")
	$requestAnnotations.(op.requestType)$("$op.url$$op.pathParamsUrl$")
	public XResult<$if(op.isPage)$Page<$basics.itemCodeUp$$op.methodNamePascalCase$Res>$else$$if(op.isResParams)$$basics.itemCodeUp$$op.methodNamePascalCase$Res$else$Void$endif$$endif$> $op.methodName$($if(op.isReqParams)$@RequestBody @Valid $basics.itemCodeUp$$op.methodNamePascalCase$Req req$endif$$op.pathParamsLong$) {
        return $item.itemCode$Service.$op.methodName$($if(op.isPathParams)$$op.pathParams$$endif$$if(op.isReqParams)$req$endif$);
    \}
	
}$
	
}
>>

请求参数

classTemplate(module,item,operationList,basics) ::= <<
package $module.apiPackageName$.request.$item.itemCode$;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;

import java.io.Serial;
import java.io.Serializable;

/**
 * $item.itemName$
 * $basics.oper.funName$ 请求参数
 * @author xia
 */
@Getter
@Setter
@Schema(description = "$basics.oper.funName$ 参数")
public class $basics.itemCodeUp$Req $if(basics.oper.isPage)$extends XhPage$endif$ implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;
	
	
	
}
>>

响应参数

classTemplate(module,item,operationList,basics) ::= <<
package $module.apiPackageName$.response.$item.itemCode$;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;

import java.io.Serial;
import java.io.Serializable;

/**
 * $item.itemName$
 * $basics.oper.funName$ 响应
 * @author xia
 */
@Getter
@Setter
@Schema(description = "$basics.oper.funName$ 响应")
public class $basics.itemCodeUp$Res implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;
	
	
	
}
>>

服务接口

classTemplate(module,item,operationList,basics) ::= <<
package $module.packageName$.service;

/**
 * $item.itemName$ 服务层接口
 * @author xia
 */
public interface $basics.itemCodeUp$Service {

$operationList:{op | 

    /**
     * $op.funName$
     * $op.describe$
     */
    XResult<$if(op.isPage)$Page<$basics.itemCodeUp$$op.methodNamePascalCase$Res>$else$$if(op.isResParams)$$basics.itemCodeUp$$op.methodNamePascalCase$Res$else$Void$endif$$endif$> $op.methodName$($if(op.isReqParams)$$basics.itemCodeUp$$op.methodNamePascalCase$Req req$endif$$if(op.isPathParams)$$op.pathParamsLongReq$$endif$);
	
}$

}
>>

服务接口实现

classTemplate(module,item,operationList,basics) ::= <<
package $module.packageName$.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import $module.packageName$.service.$basics.itemCodeUp$Service;

/**
 * $item.itemName$ 服务层接口
 * @author xia
 */
@Service
@RequiredArgsConstructor
public class $basics.itemCodeUp$ServiceImpl implements $basics.itemCodeUp$Service{

$operationList:{op | 

    /**
     * $op.funName$
     * $op.describe$
     */
	@Override
    public XResult<$if(op.isPage)$Page<$basics.itemCodeUp$$op.methodNamePascalCase$Res>$else$$if(op.isResParams)$$basics.itemCodeUp$$op.methodNamePascalCase$Res$else$Void$endif$$endif$> $op.methodName$($if(op.isReqParams)$$basics.itemCodeUp$$op.methodNamePascalCase$Req req$endif$$if(op.isPathParams)$$op.pathParamsLongReq$$endif$){
        // TODO $op.funName$ 实现
		return null;
	\}
	
}$

}
>>

模块数据

group dbXml;

moduleTemplate(module) ::= <<
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
        http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.24.xsd">

    <changeSet id="insert-ms-fun-module-data" author="xinghe">
        <comment>初始化模块数据</comment>
	$module:{op | 
		<insert tableName="ms_fun_module">
			<column name="id" value="$op.id$"/>
			<column name="module_name" value="$op.moduleCode$"/>
		</insert>
	}$
    </changeSet>

</databaseChangeLog>
>>

itemTemplate(item) ::= <<
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
        http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.24.xsd">

    <changeSet id="insert-ms-fun-item-data" author="xinghe">
        <comment>初始化功能数据</comment>
	$item:{op | 
		<insert tableName="ms_fun_item">
			<column name="id" value="$op.id$"/>
			<column name="module_id" value="$op.moduleId$"/>
			<column name="item_name" value="$op.itemName$"/>
			<column name="is_tenant" value="$op.isTenant$"/>
			<column name="describe" value="$op.describe$"/>
		</insert>
	}$
    </changeSet>

</databaseChangeLog>
>>


funTypeAnnotations ::= [
    "ADD": "0",
    "DELETE": "1",
    "EDIT": "2",
    "QUERY": "3",
    "UPLOAD": "4",
    "DOWNLOAD": "5",
    "IMPORT": "6",
    "EXPORT": "7",
    "PRINT": "8",
    "REGISTER": "9",
    "LOGIN": "10",
    "LOGOUT": "11",
    "SMS": "12",
    "EMAIL": "13",
    "WECHAT": "14",
    "OTHER": "99"
    default: "-1" 
]
operationTemplate(operation) ::= <<
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
        http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.24.xsd">

    <changeSet id="insert-ms-fun-operation-data" author="xinghe">
        <comment>初始化操作数据</comment>
	$operation:{op | 
		<insert tableName="ms_fun_item">
			<column name="id" value="$op.id$"/>
			<column name="module_id" value="$op.moduleId$"/>
			<column name="item_id" value="$op.itemId$"/>
			<column name="is_green_light" value="$op.isGreenLight$"/>
			<column name="fun_code" value="$op.moduleCode$:$op.itemCode$:$op.operationCode$"/>
			<column name="fun_name" value="$op.funName$"/>
			<column name="fun_type" value="$funTypeAnnotations.(op.funType)$"/>
			<column name="usable_config" value="$op.usableConfigJson$"/>
			<column name="field_cofnig" value="$op.fieldConfigJson$"/>
			<column name="sort_order" value="$op.sortOrder$"/>
			<column name="describe" value="$op.describe$"/>
		</insert>
	}$
    </changeSet>

</databaseChangeLog>
>>