velocity整理

调试相关

不管使用什么语言,引擎,工具去开发,对于开发人员来说最主要还是要有对应的调试方法,而对于像velocity这样模版引擎的编写,没有很直接的调试方法,但是我们可以曲线救国,通过如下方式进行调试:

package com.yjk.framework.dalgen;

import lombok.extern.slf4j.Slf4j;

/**
 * @author yujiakui
 * @version 1.0
 * Email: [email protected]
 * date: 2018/10/19 13:26
 * description:
 **/
@Slf4j
public class TestLogOutput {

    public boolean print(Object object){
        log.info("----print---obj={}",object);
        System.out.println("---------XXX------");
        return true;
    }
}

将这个测试类注入到模版引擎的上下文中,然后在vm模版文件中使用如下方式进行调用:

package ${package.Mapper};

import ${package.Entity}.${entity};
import ${superMapperClassPackage};
import org.apache.ibatis.annotations.Param;


/**
 * 

* $!{table.comment} Mapper 接口 *

* * @author ${author} * @since ${date} */ #if(${kotlin}) interface ${table.mapperName} :${superMapperClass}<${entity}> #else public interface ${table.mapperName} extends ${superMapperClass}<${entity}> { #* 这里的log对应的就是上面的TestLogOutput对象,通过这种方式进行调试*# #set($log=${cfg.map.log}) #foreach($operation in ${tableOpInfo.sqlOpInfos}) #* #if($log.print($foreach.count)) #end*# #parse("templates/operation/op-${operation.templateSuffix}.vm"); #end #*#foreach($operation in ${cfg.map.sqlCfg.tableConfigs.get(${table.name}).operations}) #if(!$operation.resultMap && !$operation.resultClass) ${entity} #elseif($operation.resultMap) $operation.resultMap #elseif($operation.resultClass) $operation.resultClass #end ${operation.name}( #foreach($param in $operation.sqlParser.params) #if($velocityCount > 1) , #end String $param #end ); #end*# } #end

velocity使用场景

velocity可以用在很多场景,特别是各种报表,通过velocity可以动态加载和修改报表的输出样式等;目前用velocity是在mybatis plus中根据sql生成对应的dao层代码和service层代码,具体代码可以参见:https://github.com/lwjaiyjk/dalgenForMybatisPlus

你可能感兴趣的:(velocity整理)