Freemarker动态替换JDBC的URL中的占位符,可以给默认值



import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;


public class FreemarkerUtil {


    final static Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);


    static {
        cfg.setLocalizedLookup(false);
        cfg.setDefaultEncoding("UTF_8");
        cfg.setTemplateLoader(new SimpleStringTemplateLoader());
    }


    public static String render(String templateContent, Map<String, Object> param){
        try {
            if (!StringUtils.hasText(templateContent)){
                return "";
            }
            Template template = cfg.getTemplate(templateContent);
            String result = FreeMarkerTemplateUtils.processTemplateIntoString(template, param);
            return result;
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    public static void main(String[] args) {

        String urlFormat = "jdbc:postgresql://${host}:${port}/${dbName}?currentSchema=<#if schemaName??>${schemaName}<#else>public</#if>&useSSL=false&stringtype=unspecified";
        Map<String, Object> param=new HashMap<String, Object>(){{
            put("host","localhost");
            put("port","7300");
            put("dbName","dbtest");
            put("schemaName","test");

        }};

        String connectStr = FreemarkerUtil.render(urlFormat, param);
        System.out.println(connectStr);
        //jdbc:postgresql://localhost:7300/dbtest?currentSchema=test&useSSL=false&stringtype=unspecified
    }

}

在pom.xml中引入依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>

Java中的命名参数动态替换通常使用的是字符串格式化方法,例如使用String.format()方法。具体步骤如下: 1. 定义字符串模板,其中需要被替换的部分用占位符表示,例如"%s"表示一个字符串占位符,"%d"表示一个整数占位符。 2. 使用字符串格式化方法将占位符替换成实际的参数值,例如使用String.format()方法,传入参数值和字符串模板即可得到替换后的字符串。 示例代码如下: ```java String name = "Tom"; int age = 18; String message = String.format("My name is %s, I'm %d years old.", name, age); System.out.println(message); // 输出:My name is Tom, I'm 18 years old. ``` Java属性中的动态占位符替换通常使用的是模板引擎,例如使用Velocity、Freemarker等。具体步骤如下: 1. 定义模板文件,其中需要被替换的部分用占位符表示,例如"$name"表示一个名称占位符,"$age"表示一个年龄占位符。 2. 加载模板文件,并将模板文件中的占位符替换成具体的参数值,例如使用Velocity引擎,通过设置参数值,将模板文件中的占位符替换成具体的值。 示例代码如下(使用Velocity引擎): ```java // 定义模板文件 String template = "My name is $name, I'm $age years old."; // 创建Velocity引擎 VelocityEngine engine = new VelocityEngine(); engine.init(); // 创建Velocity上下文 VelocityContext context = new VelocityContext(); // 设置参数值 context.put("name", "Tom"); context.put("age", 18); // 加载模板文件并渲染 StringWriter writer = new StringWriter(); engine.evaluate(context, writer, "", template); String message = writer.toString(); System.out.println(message); // 输出:My name is Tom, I'm 18 years old. ``` 注意:在使用模板引擎时,需要先了解具体的模板语法和使用方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值