【问题标题】:how can I use StrSubstitutor with and empty variable suffix?如何使用带有空变量后缀的 StrSubstitutor?
【发布时间】:2017-03-19 18:45:42
【问题描述】:

我正在尝试使用 Apache Commons Lang 的 StrSubstitutor 替换仅使用前缀标记的字符串中的变量,例如就像在 SQL 查询中用: 标记的命名参数一样。

这是我正在使用的代码 sn-p,它不起作用。

import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang3.text.StrMatcher;
import org.apache.commons.lang3.text.StrSubstitutor;

Map<String,String> m = ImmutableMap.of("a", 1);

StrSubstitutor strSubstitutor = new StrSubstitutor(m)
        .setVariablePrefix(":")
        .setVariableSuffix("");
System.out.println(strSubstitutor.replace("select a from t where a = :a"));
// expect select a from t where a = 1

知道怎么做吗?

我正在尝试实现自定义StrMatcher,但仍然不成功。 有谁做过,可以分享一下经验吗?

【问题讨论】:

    标签: java string apache-commons-lang apache-commons-lang3


    【解决方案1】:

    在深入研究StrSubstitutor 的代码后,我意识到使用StrSubstitutor/StrMatcher API 无法做到这一点。 由于 Spring 的 NAmedParameterJdbcTemplate 没有暴露带有替换参数值的 sql,所以我不得不实现自己的 sql 参数替换器。

    【讨论】:

      猜你喜欢
      • 2019-12-19
      • 1970-01-01
      • 1970-01-01
      • 2019-06-29
      • 2016-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多