Beetl模版初试

写一个Beetl的例子,现在并没有封装成方法,后续再封。

beetl:模版例子

select ${condition.columns} from ${condition.tableName}
<% if(isNotEmpty(condition.whereCondition)){%>
     where ${condition.whereCondition}
<%} %>

Java应用例子:

StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();

        Configuration cfg = null;
        try {
            cfg = Configuration.defaultConfiguration();
        } catch (IOException e) {
            e.printStackTrace();
        }
        GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);

        Template t = null;
        try {
            t = gt.getTemplate(FileUtils.readFileToString(new File("./src/main/resources/template.btl"), "UTF8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Condition condition = new Condition("menu", "id,menu_name", "a='1'");
        Map map  =Maps.newHashMap();
        map.put("condition",condition);
        t.binding(map);
        String render = t.render();
        System.out.println(render);

效果:

select id,menu_name from menu
     where a='1'

Condition结构:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Condition {

    String tableName;

    String columns;

    String whereCondition;

}

引用:

<dependency>
            <groupId>com.ibeetl</groupId>
            <artifactId>beetl</artifactId>
            <version>2.9.6</version>
        </dependency>
发布了78 篇原创文章 · 获赞 6 · 访问量 8539

猜你喜欢

转载自blog.csdn.net/MrBack/article/details/102649041