2016-11-06 9 views
1

私はApache Commons LangのStrSubstitutorを使用して、プレフィックスのみを使用してマークされた文字列の変数を置き換えようとしています。 SQLクエリに:というマーク付きの名前付きパラメータがあります。変数サフィックスが空のStrSubstitutorはどのように使用できますか?

私が使用しているコードスニペットは動作しません。

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を実装しようとしていますが、まだ失敗しています。 これまで誰も経験した経験がありますか?

答えて

1

StrSubstitutorのコードを深く掘り下げた後、私はStrSubstitutor/StrMatcher APIを使って行えないことに気付きました。 SpringのNAmedParameterJdbcTemplateは、置き換えられたパラメータ値でsqlを公開しないため、独自のSQLパラメータ置換子を実装する必要がありました。

関連する問題