今天在编程时遇到一个小问题,本来以为直接把值放到hashMap中就可以通过freemarker语法传值到sql中,但我这次打算传的是这样的类型status in (:status)这样的,就是:status希望能传来1,2,3这样的进来,于是我就把paramMap.put("status","1,2,3"),但这样查出来的结果却是空,也就是说我没有能正确的传值过来,于是找人请教:
List<String> list = new ArrayList<String>();
// 可变个数
list.add("1");
list.add("2");
list.add("3");
Map<String,String> paramMap = new HashMap<String, String>();
StringBuffer buffer = new StringBuffer();
for(int i=1;i<=list.size();i++){
//inSQL的拼写
buffer.append(":var").append(i).append(",");
//变量名的定义
paramMap.put("var"+i, list.get(i-1));
}
buffer.deleteCharAt(buffer.length()-1);
paramMap.put("inSQL", buffer.toString());
System.out.println(paramMap);
//SQL写法
/*select OV.* from SO_ORDI_VIS_INF as OV
where OV.OMSHX IN (${inSQL})
order by OV.ROW_ID ,
*/
这样的写法就可以了,当然数据库也支持<#if inSQL ?exists></#if>
于是就有如下结果
SELECT count(1) as count from VGS_GM_ORDER a ,VGS_GM_PRODUCT b
where a.SN_PROID=b.SN_PROID and a.USERID=:userId <#if type? exists>
and b.TYPE=:type</#if><#if status ? exists> and a.STATUS IN (:status)</#if>
<#if inSQL ? exists> and a.STATUS IN (${inSQL})</#if>
<#if beginTime ? exists>and a.CREATE_TIME>:beginTime</#if>