网页中有4个文本框,数据类型是String,java.sql.Date,BigDecimal,输入几个条件就按几个条件复合查询。在hibernate中怎么构成语句啊,要不得写N个方法啊,就是有没有一个或两个方法就可以实现的,关键是HQL语句上怎么写?请各位高手帮忙,谢谢。

解决方案 »

  1.   

    我写过的一个组合查询,你可以参考一下,页面上有几个文本框,可发任意输入几个进行查询,不输入查询所有。public List query(QueryPlanForm queryPlanForm){
    String hql="from TPlan where 1=1 ";
    if(queryPlanForm.getPname()!= null && !queryPlanForm.getPname().equals("")){
    hql += " and name like '%"+queryPlanForm.getPname().trim()+"%' ";
    }
    if(queryPlanForm.getTaskId() != null && !queryPlanForm.getTaskId().equals("")){
    int taskId = Integer.parseInt(queryPlanForm.getTaskId());
    hql += " and taskId = "+ taskId;
    }
    if(queryPlanForm.getStart1() != null && !queryPlanForm.getStart1().equals("")&&queryPlanForm.getStart2() != null && !queryPlanForm.getStart2().equals("")){
    Date start1 = StringToDate.stringToDate(queryPlanForm.getStart1());
    Date start2 = StringToDate.stringToDate(queryPlanForm.getStart2());
    hql += " and start between '"+start1 +"' and '"+ start2+"'";
    }

    if(queryPlanForm.getEnd1() != null && !queryPlanForm.getEnd1().equals("")&&queryPlanForm.getEnd2() != null && !queryPlanForm.getEnd2().equals("")){
    Date end1 = StringToDate.stringToDate(queryPlanForm.getEnd1());
    Date end2 = StringToDate.stringToDate(queryPlanForm.getEnd2());
    hql += " and end between '" +end1+"' and '"+ end2+"'";
    }

    if(queryPlanForm.getFeedback() != null && !queryPlanForm.getFeedback().equals("")){
    hql += " and feedback = '"+queryPlanForm.getFeedback()+"' ";
    }

    return helper.executeQuery(hql);
    }