首页 > 解决方案 > 使用 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


谢谢你帮助我!

标签: javajsonvtltelosys

解决方案



推荐阅读