//根据map查找实体 Map<String, Object> entrys = new HashMap<String, Object>(); if (StringUtil.isNotEmpty(declareType)) { entrys.put("type", "1"); } List<T> rlist = commonDao.findByMapProperty(T.class, entrys);
public <T> List<T> findByMapProperty(Class<T> entityClass, Map<String, Object> entrys) ;
//dao实现 public <T> List<T> findByMapProperty(Class<T> entityClass, Map<String, Object> entrys) { Criteria criteria = getSession().createCriteria(entityClass); for (String key:entrys.keySet()) { Object value = entrys.get(key); String queryWay = "eq"; if(key.indexOf(":")!=-1){ if(key.split(":").length>=2){ queryWay= key.split(":")[1]; } key = key.split(":")[0]; } if(value!=null){ if("le".equals(queryWay)){ criteria.add(Restrictions.le(key, value)); }else if("lt".equals(queryWay)){ criteria.add(Restrictions.lt(key, value)); }else if("ge".equals(queryWay)){ criteria.add(Restrictions.ge(key, value)); }else if("gt".equals(queryWay)){ criteria.add(Restrictions.gt(key, value)); }else if("like".equals(queryWay)){//like未添加%%则装值时需要自行添加 criteria.add(Restrictions.like(key, value)); }else if("ne".equals(queryWay)){ criteria.add(Restrictions.ne(key, value)); }else if("not".equals(queryWay)){ Object obj = value; if (obj instanceof Collection){ List clist =(List)obj; criteria.add(Restrictions.not(Restrictions.in(key, clist))); } }else if("in".equals(queryWay)){ Object obj = value; if (obj instanceof Collection){ List clist =(List)obj; criteria.add(Restrictions.in(key, clist)); } }else if("order".equals(queryWay)){ criteria.addOrder(Order.desc(key)); }else{ criteria.add(Restrictions.eq(key, value)); } }else{ criteria.add(Restrictions.isNull(key)); } } return (List<T>) criteria.list(); }