2016-11-06 2 views
1

접두어 만 사용하여 표시된 문자열의 변수를 대체하기 위해 Apache Commons Lang의 StrSubstitutor을 사용하려고합니다. SQL 쿼리에서 :이라는 이름의 매개 변수를 markjed와 같이 사용합니다.빈 변수 접미어와 함께 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를 사용하여 구현할 수 없다는 것을 깨달았습니다. 스프링의 NAmedParameterJdbcTemplate은 대체 된 매개 변수 값으로 sql을 노출시키지 않으므로 내 자신의 sql 매개 변수 대체자를 구현해야했습니다.

관련 문제