Java 一种类似弹簧的工具';s namedParameters用于常规字符串处理?
我想用值替换字符串中的命名参数,就像使用Python的%运算符时一样。 Spring支持与NamedParameterUtils非常相似的功能,但出于某种原因,它与Jdbc紧密耦合,并用问号替换这些命名参数 所以,如果我想做Java 一种类似弹簧的工具';s namedParameters用于常规字符串处理?,java,spring,string,velocity,Java,Spring,String,Velocity,我想用值替换字符串中的命名参数,就像使用Python的%运算符时一样。 Spring支持与NamedParameterUtils非常相似的功能,但出于某种原因,它与Jdbc紧密耦合,并用问号替换这些命名参数 所以,如果我想做”:a是:b“%{”a:“Java”,“b:“Beauty”}我必须使用像Velocity这样的重炮吗?还是我错过了一些简单的东西 更新:使用Dmitry的代码编写我自己的简单版本: public static String formatSqlWithNamedParams(
”:a是:b“%{”a:“Java”,“b:“Beauty”}
我必须使用像Velocity这样的重炮吗?还是我错过了一些简单的东西
更新:使用Dmitry的代码编写我自己的简单版本:
public static String formatSqlWithNamedParams(String sqlTemplate, Map<String, Object> params){
StringWriter writer = new StringWriter();
Pattern var = Pattern.compile("\\$\\{\\s*([\\w\\[\\]\\(\\)\\.]+)\\s*\\}");
Matcher match = var.matcher(sqlTemplate);
int offset = 0;
while (match.find()) {
writer.write(sqlTemplate.substring(offset, match.start()));
String key = match.group(1);
Object val = params.get(key);
writer.write(val.toString());
offset = match.end();
}
writer.write(sqlTemplate.substring(offset));
return writer.toString();
}
公共静态字符串格式SqlWithNamedParams(字符串sqlTemplate,映射参数){
StringWriter编写器=新的StringWriter();
Pattern var=Pattern.compile(\\$\{\\s*([\\w\\\[\]\(\\)\\.]+)\\s*\});
Matcher match=var.Matcher(sqlTemplate);
整数偏移=0;
while(match.find()){
write(sqlTemplate.substring(offset,match.start());
字符串键=匹配。组(1);
Object val=params.get(key);
writer.write(val.toString());
offset=match.end();
}
writer.write(sqlTemplate.substring(offset));
返回writer.toString();
}
编写所需的代码大约需要50行
下面是我不久前写的一个“平凡模板”的摘录,它做了一些非常类似的事情:
private final Pattern var = Pattern
.compile("\\{\\s*([\\w\\[\\]\\(\\)\\.]+)\\s*\\}");
private final PropertyUtilsBean bean = new PropertyUtilsBean();
private final Object data;
public TrivialTemplate(Object data) {
this.data = data;
}
public void process(BufferedReader reader, PrintWriter writer) {
String line = null;
try {
while ((line = reader.readLine()) != null) {
Matcher match = var.matcher(line);
int offset = 0;
while (match.find()) {
writer.write(line.substring(offset, match.start()));
String key = match.group(1);
if (!isBlank(key)) {
Object val = bean.getNestedProperty(data, key
.toLowerCase());
writer.write(val != null ? val.toString() : "{null}");
} else {
writer.write("{null}");
}
offset = match.end();
}
writer.write(line.substring(offset));
writer.println();
}
} catch (Throwable t) {
throw new RuntimeException("template error", t);
}
}
我使用BeanUtils来支持任意数据对象,但您也可以轻松地将其限制为映射。编写所需的代码大约需要50行 下面是我不久前写的一个“平凡模板”的摘录,它做了一些非常类似的事情:
private final Pattern var = Pattern
.compile("\\{\\s*([\\w\\[\\]\\(\\)\\.]+)\\s*\\}");
private final PropertyUtilsBean bean = new PropertyUtilsBean();
private final Object data;
public TrivialTemplate(Object data) {
this.data = data;
}
public void process(BufferedReader reader, PrintWriter writer) {
String line = null;
try {
while ((line = reader.readLine()) != null) {
Matcher match = var.matcher(line);
int offset = 0;
while (match.find()) {
writer.write(line.substring(offset, match.start()));
String key = match.group(1);
if (!isBlank(key)) {
Object val = bean.getNestedProperty(data, key
.toLowerCase());
writer.write(val != null ? val.toString() : "{null}");
} else {
writer.write("{null}");
}
offset = match.end();
}
writer.write(line.substring(offset));
writer.println();
}
} catch (Throwable t) {
throw new RuntimeException("template error", t);
}
}
我使用BeanUtils来支持任意数据对象,但您也可以很容易地将其限制为映射。您特别需要命名参数,
printf
-style或{0}是{1}
不会剪切它吗?不会,因为我正在构造一些相当大的字符串,{0}
样式占位符在一个包含许多参数的长字符串中变得非常混乱和混乱。您特别需要命名参数,printf
-style或{0}is{1}
不会剪切它吗?不会,因为我正在构造一些相当大的字符串,{0}
样式占位符在包含许多参数的长字符串中变得非常混乱和混乱。