关键是这个get方法,现在只是能够支持简单的键值对:
@SuppressWarnings("unchecked")
@Override
public T getByElse(Map<String, Object> params) {
if(params.size() == 0) {
return null;
} else {
Iterator<Entry<String, Object>> iterator = params.entrySet().iterator();
String sql = "from " + clazz.getSimpleName() + " where ";
while (iterator.hasNext()) {
Entry<String, Object> entry = iterator.next();
sql += entry.getKey() + " = :" + entry.getKey();
if(iterator.hasNext()) {
sql += " and ";
}
}
Query query = sessionFactory.getCurrentSession().createQuery(sql);
iterator = params.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, Object> entry = iterator.next();
query.setParameter(entry.getKey(), entry.getValue());
}
try {
return (T)query.uniqueResult();
} catch (Exception e) {
return null;
}
}
}
我想让这个getByElse方法也能够支持模糊查询,但水平有限,希望高手指点
慕田峪4524236
相关分类