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; }
相关文章推荐
- _11_foreach遍历__
- _10_do_while循环__
- _09_while循环__
- _08_for循环--输出9x9乘法表
- _07_Switch_判断
- sql之截取字符串函数
- 一种瀑布流效果的实现
- _06_命令行参数_if判断
- _08_for循环--输出9x9乘法表
- _07_Switch_判断
- 《公开课2》:Tiny服务及CEP深入剖析——直播QQ群:228977971
- 29-HTML-09-HTML(超链接_2)
- Java 多线程实现的两种方式
- _05_type类型转换
- 并查集详解 (转)
- _04_Type类型判断_float_double
- _03_启动程序传递参数_并显示参数
- 简单SQL语句
- _02_从键盘读入用户的输入,格式化输出hello+用户输入的信息
- _01_Hello, World!