1.错误写法
List<Record> list = Db.find("SELECT id FROM b_product_detail_phone WHERE productid = 123 AND id NOT IN (?)", "147,148");查询结果包括148,即参数并未全部识别147,148,仅识别了147,无法分辨是几个参数。
正确写法:
1.全部字符串拼接
List<Record> list = Db.find("SELECT id FROM b_product_detail_phone WHERE productid = 123 AND id NOT IN (147,148)");
2.jfinal模板
// List<Integer> idList = Arrays.asList(147,148); List<Integer> idList = new ArrayList<>(); idList.add(147); idList.add(148); SqlPara para = Db.getSqlPara("test.test1", Kv.by("idList", idList)); List<Record> list = Db.find(para); renderJson(list);all.sql文件内容
#namespace("test")
#include("test.sql")
#end
test.sql中内容
#sql("test1") SELECT id FROM 表名 WHERE pid= 739 AND id NOT IN ( #for(id:idList) #(for.index > 0 ? ", " : "")#(id) #end ) #end注意:模板中,#para生成占位符,而#(value)生成值
参考: