编写复杂的HQL语句
2016-03-04 16:36
507 查看
之前在做项目时候需要一个功能是在页面显示总金额,但条件是要根据用户的搜索条件不同显示不同的总金额。在公司大牛指导下写了这么一长串代码,突然想起来了总结一下,免得以后自己忘掉。主要思想是利用map和String,如果map中有对应的搜索条件,就添加对应key的值。
/** * 查询当前档口下的启用的日常收/支 合计金额 * @param map 查询条件 * @param incomeType 收支类型 0:支出 1:收入 * @return */ public double getSumPayment(Map<String, Object> map, int incomeType){ // 当前档口下的启用的日常收/支 合计金额HQL StringBuffer hql = new StringBuffer("select sum(df.payment) from DailyFinace "); hql.append("df where df.account = ?1 "); hql.append("and df.delFlag = ?2 "); hql.append("and df.incomeType = ?3 "); if(map != null && map.size() > 0) { // 录单日期 if (map.containsKey("recordDate_ge") && StringUtils.isNotEmpty(map.get("recordDate_ge").toString())) { hql.append("and date_format(df.recordDate,'%Y-%m-%d') >= ?4 "); } if (map.containsKey("recordDate_le") && StringUtils.isNotEmpty(map.get("recordDate_le").toString())) { hql.append("and date_format(df.recordDate,'%Y-%m-%d') <= ?5 "); } // 业务时间 if (map.containsKey("serviceTime_ge") && StringUtils.isNotEmpty(map.get("serviceTime_ge").toString())) { hql.append("and date_format(df.serviceTime,'%Y-%m-%d') >= ?6 "); } if (map.containsKey("serviceTime_le") && StringUtils.isNotEmpty(map.get("serviceTime_le").toString())) { hql.append("and date_format(df.serviceTime,'%Y-%m-%d') <= ?7 "); } // 类型ID if (map.containsKey("typeId") && StringUtils.isNotEmpty(map.get("typeId").toString())) { hql.append("and df.typeId = ?8 "); } // 收/付款账号 if (map.containsKey("accountNo") && StringUtils.isNotEmpty(map.get("accountNo").toString())) { hql.append("and df.accountNo = ?9 "); } // 经手人 if (map.containsKey("handPerson") && StringUtils.isNotEmpty(map.get("handPerson").toString())) { hql.append("and df.handPerson.id = ?10 "); } } Query query = em.createQuery(hql.toString()); // 当前档口 query.setParameter(1, accountService.getCurrentAccount()); // 状态未删除 query.setParameter(2, Boolean.FALSE); // 收支类型 query.setParameter(3, incomeType); if(map != null && map.size() > 0) { // 录单日期 if(map.containsKey("recordDate_ge") && StringUtils.isNotEmpty(map.get("recordDate_ge").toString())){ query.setParameter(4, map.get("recordDate_ge")); } if(map.containsKey("recordDate_le") && StringUtils.isNotEmpty(map.get("recordDate_le").toString())){ query.setParameter(5, map.get("recordDate_le")); } // 业务时间 if(map.containsKey("serviceTime_ge") && StringUtils.isNotEmpty(map.get("serviceTime_ge").toString())){ query.setParameter(6, map.get("serviceTime_ge")); } if(map.containsKey("serviceTime_le") && StringUtils.isNotEmpty(map.get("serviceTime_le").toString())){ query.setParameter(7, map.get("serviceTime_le")); } // 类型ID if(map.containsKey("typeId") && StringUtils.isNotEmpty(map.get("typeId").toString())){ query.setParameter(8, map.get("typeId")); } // 收/付款账号 if(map.containsKey("accountNo") && StringUtils.isNotEmpty(map.get("accountNo").toString())){ query.setParameter(9, map.get("accountNo")); } // 经手人 if(map.containsKey("handPerson") && StringUtils.isNotEmpty(map.get("handPerson").toString())) { query.setParameter(10, map.get("handPerson")); } } // 合计值 Double sumPayment = (Double) query.getResultList().get(0); if(sumPayment == null){ sumPayment = 0.00; } return sumPayment; }
相关文章推荐
- 小笨狼漫谈多线程:GCD(一) http://www.cocoachina.com/ios/20160225/15422.html
- mybatis关联查询
- 自制操作系统学习心得(一)
- 大话设计模式-模板方法模式
- ORA-00837: Specified value of MEMORY_TARGET greater than MEMORY_MAX_TARGET
- 网络爬虫的实现
- 简单工厂和单例设计模式浅解
- [C++]lambda表达式
- FZU 2092 收集水晶
- AlwaysOn--查看可用性组的首先备份节点
- wap站获取上网用户手机型号的方法 .net的写法
- 10大常见的安全漏洞!你知道吗?
- Web前端面试小记
- 复习篇2016.3.4 16.42复习篇之一(蓝桥杯恶补)
- 加密文件系统eCrypt概述
- 操作系统--处理调度与死锁
- XAlign:用于代码对齐的Xcode插件
- 十、装饰(Decorator)模式 --结构模式(Structural Pattern)
- Java大数练习
- Lua中的面向对象[2]:使用指针