StringTemplate 模板引擎Java

StringTemplate是一种基于java的模板引擎库,类似于velocity,FreeMarker。可以用于生成源代码、web页面、电子邮件等多种样式的文本。选择StringTemplate的原因是因为相较于其他的模板引擎,他的功能更加强大。

使用

1、使用maven添加依赖或从http://www.stringtemplate.org下载
<dependency>
  <groupId>org.antlr</groupId>
  <artifactId>ST4</artifactId>
  <version>4.0.8</version>
  <scope>compile</scope>
</dependency>
2、demo
2.1示例:
import org.stringtemplate.v4.ST;
...

       ST hello = new ST("Hello, <name>");
       hello.add("name", "World");
       System.out.println(hello.render());

       或者
        
     ST hello = new ST("Hello, <model.name>");
     hello.add("model", new TemplateModel());
     System.out.println(hello.render());


输出:

Hello, World
2.2 条件表达式

ST支持条件表达式,简单实例如下:

       // 第二个和第三个参数用于定义表达式的头尾字符
       ST hello = new ST("Hello, $if(name)$$name$$endif$", '$', '$');
       hello.add("name", "risk");
       System.out.println(hello.render());

输出:

Hello, risk

2.3 模板组

StringTemplate的一个强大的功能是模板功能,模板功能有点类似函数的使用方式,模板定义如下:

templateName(args, agrs, ...) ::= "模板内容"

上述模板方式支持单行内容,这里展示一个简单的示例:

        STGroup stg = new STGroupString("sqlTemplate(columns,condition) ::= \"select <columns> from table where 1=1 <if(condition)>and <condition><endif> \"");
        ST sqlST = stg.getInstanceOf("sqlTemplate");
        sqlST.add("columns","order_id");
        sqlST.add("condition", "dt='2017-04-04'");
        System.out.print(sqlST.render());

输出:

from select order_id from table where 1=1 and dt='2017-04-04' 

2.4 对于模板定义,同时支持如下两种方式:

  • 1、模板内容为多行
templateName(args, agrs, ...) ::= <<
模板内容
模板内容
>>
  • 2、模板内容多行,且忽略换行符和缩进
templateName(args, agrs, ...) ::= <%
模板内容
模板内容
%>

因此可以将上例中的sql写成如下方式更好:

        STGroup stg = new STGroupString("sqlTemplate(columns,condition) ::= <<select <columns> \n" +
                "from table \n" +
                "where 1=1 <if(condition)>and <condition><endif> >>");
        ST sqlST = stg.getInstanceOf("sqlTemplate");
        sqlST.add("columns","order_id");
        sqlST.add("condition", "dt='2017-04-04'");
        System.out.print(sqlST.render());

输出:

select order_id 
from table 
where 1=1 and dt='2017-04-04' 

当模板过于复杂时,以硬编码的方式将模板写在代码中,实在很麻烦,因此将模板内容写入到文件dataExtarctSql.stg中,内容如下:

sqlTemplate(columns,condition)
::= <<select <columns;separator=",">
from table
where 1=1 <if(condition)>and <condition><endif>
>>

这里将<columns> 修改为 <columns;separator=","> 这样可以在列名中插入多个列名,并以","分割。
java代码如下:

        STGroup stg = new STGroupFile("dataExtractSql.stg");
        ST sqlST = stg.getInstanceOf("sqlTemplate");

        List<String> columnList = new LinkedList<String>();
        columnList.add("order_id");
        columnList.add("price");
        columnList.add("phone");
        sqlST.add("columns", columnList);
        sqlST.add("condition", "dt='2017-04-04'");
        System.out.print(sqlST.render());

输出:

select order_id,price,phone
from table
where 1=1 and dt='2017-04-04'
2.5模板组的简单使用

当一个模板比较复杂时,可以拆分成多个模板,以模板组的方式使用更加方便。
模板文件dataExtarctSql.stg如下:

/**模板外注释sql模板*/
sqlTemplate(columns,condition,joinKey,tableName,childColumns,childJoinKey,childTableName,childCdtion)
::= <<
<! 模板内注释 !>
select <columns;separator=",">,<childColumns:{item|t2.<item>};separator=",">
from <tableName> as t1 left join (<childSqlTemplate(childColumns, childCdtion)>) as t2 on t1.<joinKey>=t2.<childJoinKey>
where 1=1 <if(condition)>and <condition><endif>
>>

/**模板外注释sql子模板*/

childSqlTemplate(childColumns, childCdtion)
::= <<
select <childColumns;separator=",">
from <childTableName>
where 1=1 <if(childCdtion)>and <childCdtion><endif>
>>

模板中有两个模板函数sqlTemplate、childSqlTemplate,childSqlTemplate作为一个子模板被sqlTemplate调用。sqlTemplate中select 后面的列名分为两部分,一部从数组变量columns中获取,并以“,”进行分割,另一部分从数组childColumns中获取,使用“,”分割的同时,也为每个列名增加了“t2.”的前缀,即子查询的别名。from部分中将childSqlTemplate函数模板作为一个子查询进行join。这样获得了一个比较复杂的sql语句。
java代码如下:

        STGroup stg = new STGroupFile("dataExtractSql.stg");
        ST sqlST = stg.getInstanceOf("sqlTemplate");

        List<String> columnList = new LinkedList<String>();
        columnList.add("order_id");
        columnList.add("price");
        columnList.add("phone");
        columnList.add("user");

        sqlST.add("columns", columnList);
        sqlST.add("condition", "dt='2017-04-04'");
        sqlST.add("joinKey", "user");
        sqlST.add("tableName", "orderTable");

        List<String> childColumnList = new LinkedList<String>();
        childColumnList.add("user");
        childColumnList.add("userLeave");
        childColumnList.add("userLocation");
        sqlST.add("childColumns", childColumnList);
        sqlST.add("childJoinKey", "user");
        sqlST.add("childTableName", "userTable");

        String result = sqlST.render();

        System.out.print(result);

输入内容如下:

select order_id,price,phone,user,t2.user,t2.userLeave,t2.userLocation
from orderTable as t1 left join (select user,userLeave,userLocation
from userTable
where 1=1 ) as t2 on t1.user=t2.user
where 1=1 and dt='2017-04-04'



  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值