您的位置:首页 > 其它

hibernate通过判断参数动态组合Hql语句,生成基本通用查询

2015-08-11 20:15 543 查看
//     
public List find(Station entity) {
List reuslt = null;

// 字符串辅助类
StringBuffer hql = new StringBuffer("from Station where 1=1");

List vp = new ArrayList();

if (entity != null) {

//小于0的和null都不做比较
if (entity.getCzdm() != null&&entity.getCzdm()<0) {
hql.append(" and czdm=?");
vp.add(entity.getCzdm());
}

//空字符串和null都不做比较
if (entity.getCzmc() != null&&entity.getCzmc().length()>0) {
hql.append(" and czmc = ?");
vp.add(entity.getCzmc());
}

if (entity.getDj() != null) {
hql.append(" and dj=?");
vp.add(entity.getDj());
}
}

Query q = gs().createQuery(hql.toString());

for (int i = 0; i < vp.size(); i++) {
q.setParameter(i, vp.get(i));
}

reuslt = q.list();
return reuslt;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: