日期:2014-05-19 浏览次数:21007 次
public List getListForSQL(Map<String,Object> map )throws Exception{
Session session = SessionFactoryUtils.getSession(hibernateTemplate.getSessionFactory(), true);
String sql =" select * from user where 1=1 ";
//map key 有 userId(必有) name(可选) password(可选) age(可选) 等..
int userId = (Integer)map.get("userId")==null ? 0 :(Integer)map.get("userId");
String name = (String )map.get("name");
String password = (String)map.get("password");
String age = (String)map.get("age");
if(userId != 0 ){
sql += " and userid = ? ";
}else{
if(map.containsKey("userid ")){
map.remove("userid ");
}
}
if(name != null && name.length() > 0){
sql += " and name = ? ";
}else{
if(map.containsKey("name ")){
map.remove("name ");
}
}
Query query = session.createSQLQuery(sql).setProperties(map);// map传递进去 参数也对, 好像是没绑定?上
List list = query.list();
if(session != null){
session.close();
}
return list;
}