2

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

这是我正在使用的代码片段,它不起作用。

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但仍然不成功。有人做过,可以分享一些经验吗?

4

1 回答 1

3

在深入研究代码后,我意识到使用/ APIStrSubstitutor无法完成。由于 Spring不会使用替换的参数值公开 sql,因此我必须实现自己的 sql 参数替换器。StrSubstitutorStrMatcherNAmedParameterJdbcTemplate

于 2017-04-18T08:31:35.933 回答