Freemarker查询SQL⽣成器package test;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
免费模板生成器import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import t.ApplicationContext;
import t.ApplicationContextAware;
import freemarker.cache.StringTemplateLoader;
plate.Configuration;
plate.Template;
plate.TemplateException;
/**
* Freemarker查询SQL⽣成器
*/
public class Freemark implements ApplicationContextAware {
/** logger */
private static final Logger logger = LoggerFactory
.getLogger(Freemark.class);
/** Spring上下⽂ */
protected ApplicationContext applicationContext;
/** freemarker configuration */
protected Configuration cfg;
/** freemarker template loader */
protected StringTemplateLoader stringTL;
public static void main(String[] args) throws Exception {
Freemark f1 = new Freemark();
String sql = "select orderdate, o.orderid, o.status, o.alcost  from omorder o where 1 = 1  "
+ "<#if orderdate_1 ??>    derdate >= '${orderdate_1}'  </#if>  "
+ "<#if orderdate_2 ??>    derdate < '${orderdate_2}'  </#if>  <#if orderid ??>  "
+ "  derid = '${orderid}'  </#if>  <#if note ??>    like '%${note}%' "
+ " </#if>    <#if orderstatus ??>    and o.status = '${orderstatus}'  </#if>";
Map<String, Object> root = new HashMap<String, Object>();
root.put("orderdate_1", "2012-01-01");
root.put("msg", "您已经完成了第⼀个FreeMarker的⽰例");
System.out.println(sql);
System.out.QueryString("12", sql, root));
}
public String getQueryString(String queryName, String queryTemplate,
Map<String, ?> conditions) throws IOException, TemplateException {
if (queryTemplate == null) {
System.out.print("查询模板不存在");
}
cfg = new Configuration();
stringTL =new StringTemplateLoader();
stringTL.putTemplate(queryName, queryTemplate);
cfg.setTemplateLoader(stringTL);
Template temp = Template(queryName);
Template temp = Template(queryName);
StringWriter queryStringWriter = new StringWriter();
temp.process(conditions, queryStringWriter);
String queryString = String();
return queryString == null ? null : im();
}
@Override
public void setApplicationContext(ApplicationContext applicationContext)  throws BeansException {
// TODO Auto-generated method stub
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。