简体   繁体   English

如何使用带有空变量后缀的 StrSubstitutor ?

[英]how can I use StrSubstitutor with and empty variable suffix?

I am trying to use Apache Commons Lang's StrSubstitutor to replace variables in a string marked only using prefixes, eg like named parameters markjed with : in an SQL query.我正在尝试使用 Apache Commons Lang 的StrSubstitutor来替换仅使用前缀标记的字符串中的变量,例如在 SQL 查询中用:标记的命名参数。

Here's the code snippet I am using, which doesn't work.这是我正在使用的代码片段,它不起作用。

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

Any idea how to do it?知道怎么做吗?

I am trying to implement a custom StrMatcher but am still unsuccessful.我正在尝试实现自定义StrMatcher但仍然没有成功。 Any one has done it before and can share some experience?有没有人做过,可以分享一些经验?

After a deep delving into the code of StrSubstitutor I realized it cannot be done using the StrSubstitutor / StrMatcher API.在深入研究StrSubstitutor的代码后,我意识到它无法使用StrSubstitutor / StrMatcher API 来完成。 Since Spring's NAmedParameterJdbcTemplate does not expose the sql with replaced parameter values, I had to implement my own sql parameter substitutor.由于 Spring 的NAmedParameterJdbcTemplate不公开具有替换参数值的 sql,因此我必须实现自己的 sql 参数替换器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM