如何正确使用HQL进行查询?
一开始我是这样写的,将HQL和Query参数设置通过一个判断写在一起了,但是随之而来的问题就出现了,在createQuery之后增加的参数会报未定义该参数。
public List<CheckItemIntervalResult> query(Map<String,String> filterMap){List<CheckItemIntervalResult> lists = new ArrayList<CheckItemIntervalResult>();if(!filterMap.isEmpty()){System.out.println(filterMap.size());String checkItemIntervalId = filterMap.get("checkItemIntervalId");String actionResult = filterMap.get("actionResult");String actionTimeStart = filterMap.get("actionTimeStart");String actionTimeEnd = filterMap.get("actionTimeEnd");StringBuffer hql = new StringBuffer(); hql.append("from CheckItemIntervalResult where 1=1");//这里的hql不好定义,因为这里还不知道到底需要过滤哪几个条件Query query = createQuery(hql.toString());if(StringUtils.isNotBlank(checkItemIntervalId)){hql.append(" and checkItemInterval_id=:checkItemIntervalId ");query.setString("checkItemIntervalId", checkItemIntervalId);}if(StringUtils.isNotBlank(actionResult)){hql.append(" and actionResult =:actionResult");query.setString("actionResult", actionResult);}DateUtils dateUtils = new DateUtils("yyyy-MM-dd");if(StringUtils.isNotBlank(actionTimeStart) && StringUtils.isBlank(actionTimeEnd)){hql.append(" and actionTime >= :actionTimeStart");query.setDate("actionTimeStart", dateUtils.string2Date(actionTimeStart));}if(StringUtils.isBlank(actionTimeStart) && StringUtils.isNotBlank(actionTimeEnd)){hql.append(" and actionTime <= :actionTimeEnd");query.setDate("actionTimeEnd", dateUtils.string2Date(actionTimeEnd));}if(StringUtils.isNotBlank(actionTimeStart) && StringUtils.isNotBlank(actionTimeEnd)){hql.append(" and actionTime between :actionTimeStart and :actionTimeEnd");query.setDate("actionTimeStart", dateUtils.string2Date(actionTimeStart));query.setDate("actionTimeEnd", dateUtils.string2Date(actionTimeEnd));}hql.append(" order by actionTime desc");System.out.println(hql);lists=query.list();}return lists;}
public List<CheckItemIntervalResult> query(Map<String,String> filterMap){List<CheckItemIntervalResult> lists = new ArrayList<CheckItemIntervalResult>();if(!filterMap.isEmpty()){System.out.println(filterMap.size());String checkItemIntervalId = filterMap.get("checkItemIntervalId");String actionResult = filterMap.get("actionResult");String actionTimeStart = filterMap.get("actionTimeStart");String actionTimeEnd = filterMap.get("actionTimeEnd");StringBuffer hql = new StringBuffer(); hql.append("from CheckItemIntervalResult where 1=1");if(StringUtils.isNotBlank(checkItemIntervalId)){hql.append(" and checkItemInterval_id=:checkItemIntervalId ");}if(StringUtils.isNotBlank(actionResult)){hql.append(" and actionResult =:actionResult");}if(StringUtils.isNotBlank(actionTimeStart) && StringUtils.isBlank(actionTimeEnd)){hql.append(" and actionTime >= :actionTimeStart");}if(StringUtils.isBlank(actionTimeStart) && StringUtils.isNotBlank(actionTimeEnd)){hql.append(" and actionTime <= :actionTimeEnd");}if(StringUtils.isNotBlank(actionTimeStart) && StringUtils.isNotBlank(actionTimeEnd)){hql.append(" and actionTime between :actionTimeStart and :actionTimeEnd");}hql.append(" order by actionTime desc");System.out.println(hql);Query query = createQuery(hql.toString());if(StringUtils.isNotBlank(checkItemIntervalId)){query.setString("checkItemIntervalId", checkItemIntervalId);}if(StringUtils.isNotBlank(actionResult)){query.setString("actionResult", actionResult);}DateUtils dateUtils = new DateUtils("yyyy-MM-dd");if(StringUtils.isNotBlank(actionTimeStart) && StringUtils.isBlank(actionTimeEnd)){query.setDate("actionTimeStart", dateUtils.string2Date(actionTimeStart));}if(StringUtils.isBlank(actionTimeStart) && StringUtils.isNotBlank(actionTimeEnd)){query.setDate("actionTimeEnd", dateUtils.string2Date(actionTimeEnd));}if(StringUtils.isNotBlank(actionTimeStart) && StringUtils.isNotBlank(actionTimeEnd)){query.setDate("actionTimeStart", dateUtils.string2Date(actionTimeStart));query.setDate("actionTimeEnd", dateUtils.string2Date(actionTimeEnd));}lists=query.list();}return lists;}