freemarker特殊字符用noparse转义#{}

自动生成mapper的时候遇到特殊字符需要转义,特此记录一下

如:

<#list mapperDataList as data>
    #{it.${data.columnName}},

这样会报错

Caused by: freemarker.core.ParseException: Syntax error in template "mapper.ftl" in line 12, column 6:
You can't use "${" here as you are already in FreeMarker-expression-mode. Thus, instead of ${myExpression}, just write myExpression. (${...} is only needed where otherwise static text is expected, i.e, outside FreeMarker tags and ${...}-s.)
    at freemarker.core.TokenMgrError.toParseException(TokenMgrError.java:247)
    at freemarker.template.Template.(Template.java:215)
    at freemarker.cache.TemplateCache.loadTemplate(TemplateCache.java:495)
    at freemarker.cache.TemplateCache.getTemplate(TemplateCache.java:409)
    at freemarker.cache.TemplateCache.getTemplate(TemplateCache.java:261)
    at freemarker.template.Configuration.getTemplate(Configuration.java:1786)
    at freemarker.template.Configuration.getTemplate(Configuration.java:1646)
    at com.code.generator.CodeGenerator.genMapperSql(CodeGenerator.java:292)
    ... 2 more

应该写成

<#list mapperDataList as data>
    <#noparse>#{it.${data.columnName}<#noparse>},

这样就能正确生成了。

<#noparse>的作用:

FreeMarker 不会在这个指令体中间寻找FTL标签, 插值和其他特殊的字符序列,除了noparse的结束标记。

 

你可能感兴趣的:(开发经验,报错处理)