1.工具类
package com.lys.util;
import org.apache.commons.lang.text.StrSubstitutor;
import java.util.HashMap;
import java.util.Map;
public class StringTemplate {
private String template;
private Map<String, Object> valuesMap;
public StringTemplate(String template){
this.template = template;
this.valuesMap = new HashMap<>();
}
public StringTemplate build(String param, Object value){
this.valuesMap.put(param, value);
return this;
}
public String render(){
StrSubstitutor sub = new StrSubstitutor(this.valuesMap);
return sub.replace(this.template);
}
}
2. 使用方式
StringTemplate sqlTemplate = new StringTemplate("select * from student where xy=${xy} and zy=${zy} ");
String sql= sqlTemplate
.build("xy", "信息学院")
.build("zy", "软件工程")
.render();