〇×〇站

个人博客,佛系更新

ExportUtil

  • Pluto
  • 2026-03-04 10:20:32
  • 0
  • 引入依赖:

    <dependency>
        <groupId>com.ibeetl</groupId>
        <artifactId>beetl</artifactId>
        <version>3.16.2.RELEASE</version>
    </dependency>
    

    ExportUtil.java:

    import lombok.SneakyThrows;
    import org.beetl.core.Configuration;
    import org.beetl.core.GroupTemplate;
    import org.beetl.core.Template;
    import org.beetl.core.resource.ClasspathResourceLoader;
    import java.util.Map;
    
    public class ExportUtil {
    
        @SneakyThrows
        public static String genSql(String key, Map map) {
            ClasspathResourceLoader crl = new ClasspathResourceLoader("templates/btl");
            Configuration cfg = new Configuration() {{
                setStatementStart("-- :");
                setStatementEnd(null);
                addPkg("x.y.z.constant"); // DBNameConstant类的包名
            }};
            Template t = new GroupTemplate(crl, cfg).getTemplate(key);
            t.binding(map);
            return t.render();
        }
    }
    

    resources/templates/btl/test.sql:

    SELECT
        *
    FROM
        ${@DBNameConstant.getTest()}.test
    WHERE
        1 = 1
    -- :if(isNotEmpty(ids)) {
        AND id IN (${ids})
    -- :}
    ORDER BY id DESC
    
    分类:
  • 代码
  • 标签:
  • java