select * from usertable where status = ? and username = ? and sex = ?
上面的SQL是经常写的,有时字段多了需要写很多and xxx = ? 这样的,有时and的条件是不固定的。
在Jfinal3.0里支持了Sql 管理与动态生成,手册中给出的例子是
select * from table where status = #(statusOK) #for(cond:condMap) and cond = #p(cond) #end
例子中的condMap应该是个字符串数组,用下面的参数测试了下结果是
JMap map = JMap.create(); map.put("status", 1); String[] cond ={"username","sex"}; map.put("condMap", cond);
Sql: select * from table where status = ? and cond = ? and cond = ? Para: [1, username, sex]
感觉手册里的例子是否不对?这样好像没什么意义。我想得到的应该是
Sql: select * from table where status = ? and username = ? and sex = ? Para: [1, zhangsan, 男]
改变了姿势再试了下
SQL模板:
Select * from table where status = #p(status) #for(cond:condMap) and #(cond) = #p(cond) #end
JAVA代码:
JMap map = JMap.create(); map.put("status", 1); map.put("username", "zhangsan"); map.put("sex", "man"); JMap condMap = JMap.create(); String[] cond ={"username","sex"}; map.put("condMap", cond);
得到的结果:
Sql: select * from table where status = ? and username = ? and sex = ? Para: [1, username, sex]
参数对了,但是值不对。看来#p好像不支持动态的参数。