//value[i]为第i个查询条件propertyName[i]的值 (本方法已通过测试)
/*多条件查询,查询条件的值为空时自动除去该条件
* rigor为true时采用精确查询
*/
public List searchByPropertys(String model,String[]propertyName,Object[] value,int page,boolean rigor){
StringBuffer sqlBuffer = new StringBuffer();
String ralation=" like ";
if(rigor){
ralation=" = ";
}
sqlBuffer.append("from "+model+" as model\n");
int len=propertyName.length;
List list=new ArrayList();
boolean first=true;
for(int i=0;i<len;i++){
if(value[i]!=null){
if(first){
sqlBuffer.append(" where "+ "model."+ propertyName[i] + ralation+" ?\n");
list.add(value[i]);
first=false;
}else{
sqlBuffer.append(" and "+ "model."+ propertyName[i] +ralation+ " ?\n");
list.add(value[i]);
}
}
}
try {
Session session=getSession();
Query queryObject = session.createQuery(sqlBuffer.toString());
for(int i=0;i<list.size();i++){
if(rigor){
queryObject.setParameter(i, list.get(i));
}else{
queryObject.setParameter(i, "%"+list.get(i)+"%");
}
}
list=queryObject.list();
closeSession(session);
return list;
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}