Velocity作为替换模板是在有独特优势,最近小学了一下,感觉很不错,下载资源包目前最新为1.6.3的,除了velocity1.6.3.jar之外,lib目录下的所有jar都需要添加到项目中去
public class VelocityHandle {
/**
* @param clazz
* @return 插入实体的insert sql命令
* @throws Exception
*/
private static String sqlBuilder() {
try {
Velocity.init();
String sqlCmd = "insert into $table ($columns) values ($params)";
VelocityContext ctx = new VelocityContext();
ctx.put("table", "target");
ctx.put("params", "aa");
ctx.put("columns", "xxx");
StringWriter writer = new StringWriter();
Velocity.evaluate(ctx, writer, "mystring", sqlCmd);
String str = writer.toString();
writer.close();
return str;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args){
System.out.println(sqlBuilder());
}
}