多条件拼接SQL的查询中,用ssh,DAO中List productList = this.getHibernateTemplate().find(sql);

怎样动态给SQL赋值呢
2024-11-01 19:23:52
推荐回答(1个)
回答1:

竟然你都是多条件拼接了,那么你把拼好的sql传进去不就行了

String id = map.get("id")==null?null:(String)map.get("id");
String name = map.get("name")==null?null:(String)map.get("name");
String post = map.get("post")==null?null:(String)map.get("post");
String certtype = map.get("certtype")==null?null:(String)map.get("certtype");
String status = map.get("status")==null?null:(String)map.get("status");
String dept = map.get("dept")==null?null:(String)map.get("dept");
String sqlwhere = "";
if(id!=null){
sqlwhere +=" AND ID LIKE '%"+id+"%' ";
}
if(name!=null){
sqlwhere +=" AND NAME LIKE '%"+name+"%' ";
}
if(post!=null){
sqlwhere +=" AND POST = '"+post+"' ";
}
if(certtype!=null){
sqlwhere +=" AND CERTTYPE = '"+certtype+"' ";
}
if(status!=null){
sqlwhere +=" AND STATUS = '"+status+"' ";
}
if(dept!=null){
sqlwhere +=" AND DEPT = '"+dept+"' ";
}

拼的烂点像我这样行不??