java - 使用 Telosys 浏览 Velocity 模板语言中的目录
问题描述
我正在尝试在 VTL 中编写模板以使用 telosys 生成 java 类。我的起点是一个包含 json 文件(mapping1.json、mapping2.json、...)的目录(src/main/resources/templates/es)。json 文件的数量和名称未知,可能会有所不同。我必须为每个 json 文件生成几个 java 类。
我已经为我必须生成的每个 java 类编写了一个模板。但是这些模板适用于静态嵌入的 json 对象。
这是一个例子:
#set($json = {
"template":"acces_formation",
"mappings":{
"data":{
"properties":{
"MATRICULE":{
"type":"string",
"index":"not_analyzed"
},
"NOM":{
"type":"string",
"index":"not_analyzed"
},
"DATE_NAIS":{
"type":"date",
"format":"dd/MM/yyyy"
},
"SEXE":{
"type":"string",
"index":"not_analyzed"
},
"GRADE":{
"type":"string",
"index":"not_analyzed"
}
}
}
}
})
#macro(javaName $s)$s.substring(0,1).toUpperCase()$s.substring(1)#end
#macro(setter $s)set#javaName($s)#end
#macro(getter $s)get#javaName($s)#end
###############################start macro toCamelCase
#macro(toCamelCase $s)
#set($datas = $s.split("_"))
#set($name = "")
#foreach($data in $datas)
#set($data = $data.substring(0,1).toUpperCase()+$data.substring(1))
#set($name = $name+$data)
#end
$name##
#end
###############################End macro toCamelCase
###############################Start macro tab
#macro(tab $nbreTotal)
#if($nbreTotal >= 1)
#foreach($nbre in [1..$nbreTotal])
##
#end
#end
#end
###############################End macro tab
###############################Start macro javaType
#macro(javaType $f, $nbreTab)
#if($f.equals("string"))
#tab($nbreTab)String##
#elseif($f.equals("boolean"))
#tab($nbreTab)Boolean##
#elseif($f.equals("long"))
#tab($nbreTab)Long##
#elseif($f.equals("float"))
#tab($nbreTab)Float##
#elseif($f.equals("double"))
#tab($nbreTab)Double##
#elseif($f.equals("int"))
#tab($nbreTab)Integer##
#elseif($f.equals("integer"))
#tab($nbreTab)Integer##
#elseif($f.equals("date"))
#tab($nbreTab)String##
#elseif($f.equals("array"))
#tab($nbreTab)java.util.List<#javaType($f)>##
#else
#tab($nbreTab)$f##
#end
#end
###############################End macro javaType
#############################################
#set($templateName = $json.template)
#set($entity = "#toCamelCase($json.template)")
#set($entityDto = $entity+"Dto")
#set($param = "_param")
/*
Java dto for elasticSearch template $templateName
Created on $today.date ( Time $today.time )
Generator tool : $generator.name ( version $generator.version )
*/
package ${target.javaPackageFromFolder(${SRC})};
import java.util.Date;
import ${ROOT_PKG}.helper.contract.SearchParam;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
/**
DTO for elasticSearch template "$templateName"
@author Lazare yao
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder(alphabetic = true)
@getter
@Setter
@NoArgsConstructor
@tostring
public class $entityDto implements Cloneable {
#set($properties = $json.mappings.data.properties)
private String#tab(3)_id;
#foreach($key in $properties.keySet())
#set($field = $key.toLowerCase())
#tab(1)private #javaType($properties.get($key).type,0)#tab(3)$field ;
#end
private SearchParam<String>#tab(3)_id$param;
#foreach($key in $properties.keySet())
#set($field = $key.toLowerCase())
#tab(1)private SearchParam<#javaType($properties.get($key).type,0)>#tab(3)${field}$param;
#end
private String orderDirection;
private String orderField;
}
我现在需要的是:
1-浏览我的 json 目录,获取每个 json 文件并在该 json 上应用模板以创建该 json 文件的 java 类。
2-正确配置文件templates.cfg以使用 java 类名创建 java 类文件。关于这一点,我尝试从模板修改变量${BEANNAME}、${BEANNAME_UC}、${BEANNAME_LC}(由 telosys 默认给出)。但它不起作用:它们仍然是空的并且没有生成文件。这是文件templates.cfg的内容:
#---------------------------------------------------------
# Templates configuration file
# Values separated by ";"
# . value 1 : the label
# . value 2 : the file to be generated ( var allowed : ${BEANNAME}, ${BEANNAME_UC}, ${BEANNAME_LC} )
# . value 3 : the project folder where to generate ( var allowed : ${BEANNAME}, ${BEANNAME_UC}, ${BEANNAME_LC} )
# . value 4 : the template to use
# . value 5 : number of execution : "1" for "ONCE" for all entities, default is multiple executions ( executed for each entity )
#---------------------------------------------------------
# Since v 2.0 the project's variables can be used in file and folder name
#---------------------------------------------------------
Dto ; ${BEANNAME}Dto.java ; ${SRC}/${ROOT_PKG}/helper/dto ; dto.vm
CustomDto ; _${BEANNAME}Dto.java ; ${SRC}/${ROOT_PKG}/helper/dto/customize ; _dto.vm
Enum ; ${BEANNAME}Enum.java ; ${SRC}/${ROOT_PKG}/helper/enums ; enum.vm
Repository ; ${BEANNAME}Repository.java ; ${SRC}/${ROOT_PKG}/dao/repository/es ; repository.vm ;
Custom Repository ; _${BEANNAME}Repository.java ; ${SRC}/${ROOT_PKG}/dao/repository/es/customize ; _repository.vm ;
Business ; ${BEANNAME}Business.java ; ${SRC}/${ROOT_PKG}/business/ ; business.vm
Controller ; ${BEANNAME}Controller.java ; ${SRC}/${ROOT_PKG}/rest/api ; controller.vm
谢谢你帮助我!
解决方案
推荐阅读
- asp.net-core - Blazor AspNetCore WebApi 向客户端返回错误
- excel-formula - Excel查找多列,查找最新日期并从另一列返回对应值
- java - 安装 APEX 时出错:包“APEX_200200.WWV_FLOW_SECURITY”的现有状态已失效
- python - Pandas:创建具有不同频率的 pandas 日期时间序列
- scala - 使用 AWS Glue 连接到 Presto 数据库。无法通过 SSL 密钥库或证书
- string - Apache Camel 从字符串消息体中删除撇号
- shell - Selenium/Protractor 可以配置为执行 shell 脚本吗?
- python - 如何在 python -m 下载调用中设置代理?
- java - 从活动类过渡到片段类
- azure - AzCopy Sync 命令在访问密钥上失败,但在容器为私有时传递 SAS 密钥