【ST4】Java 中的模板引擎 StringTemplate

在这里插入图片描述

1.概述

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

2.使用

使用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 \n” +
“from table \n” +
“where 1=1 <if(condition)>and >>”);
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'

————————————————
版权声明:本文为CSDN博主「天人不合一」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u011704663/article/details/79847229

猜你喜欢

转载自blog.csdn.net/qq_21383435/article/details/106459346