假如我有5个输入框,代表5个查询条件,怎样才能实现根据用户自由输入一个或多个条件的不同而得到不同的查询结果呢??

解决方案 »

  1.   

    /**
     * @param服务分页查询
     * @author 徐江为
     */
    public List<CstService> getCstServiceByPage(int page, int everypage,
    CstService ser, String d1, String d2, String status) {
    String hql = "select s,c.custName from CstService s,CstCustomer c where s.cstCustomer.custNo=c.custNo and s.svrStatus='"
    + status + "'";
    if (ser.getSvrCustName() != null && !ser.getSvrCustName().equals("")) {
    hql += " and s.svrCustName like '%" + ser.getSvrCustName() + "%'";
    }
    if (ser.getSvrTitle() != null && !ser.getSvrTitle().equals("")) {
    hql += " and s.svrTitle='" + ser.getSvrTitle() + "'";
    }
    if (ser.getSvrType() != null && !ser.getSvrType().equals("")) {
    if (ser.getSvrType().equals("全部")) {
    hql = hql;
    } else {
    hql += " and s.svrType='" + ser.getSvrType() + "'";
    } }
    if (!d1.equals("") && d1 != null && !d2.equals("") && d2 != null) {
    hql += " and s.svrCreateDate between to_date('" + d1
    + "','yyyy-mm-dd') and to_date('" + d2 + "','yyyy-mm-dd')";
    }
    List<Object> list = HibernateBaseDao.bypage(hql, page, everypage);
    Iterator<Object> iter = list.iterator();
    List<CstService> list2 = new ArrayList<CstService>();
    while (iter.hasNext()) {
    Object[] obj = (Object[]) iter.next();
    CstService service = (CstService) obj[0];
    String custName = obj[1].toString();
    CstCustomer c = new CstCustomer();
    c.setCustName(custName);
    service.setCstCustomer(c);
    list2.add(service);
    }
    return list2;
    }
    穿个对象进去
      

  2.   

    问的是:根据不同的输入条件,来实现按不同条件查询,动态的在sql拼接查询条件
      

  3.   

    +1 这个是根据条件查询的呀!
    String hql = "select s,c.custName from CstService s,CstCustomer c where s.cstCustomer.custNo=c.custNo and s.svrStatus='"
    + status + "'";
    if (ser.getSvrCustName() != null && !ser.getSvrCustName().equals("")) {
    hql += " and s.svrCustName like '%" + ser.getSvrCustName() + "%'";
    }
    if (ser.getSvrTitle() != null && !ser.getSvrTitle().equals("")) {
    hql += " and s.svrTitle='" + ser.getSvrTitle() + "'";
    }
    if (ser.getSvrType() != null && !ser.getSvrType().equals("")) {
    if (ser.getSvrType().equals("全部")) {
    hql = hql;
    } else {
    hql += " and s.svrType='" + ser.getSvrType() + "'";
    }}
    if (!d1.equals("") && d1 != null && !d2.equals("") && d2 != null) {
    hql += " and s.svrCreateDate between to_date('" + d1
    + "','yyyy-mm-dd') and to_date('" + d2 + "','yyyy-mm-dd')";
    }不过这样要注意注入。
      

  4.   

    1楼的代码,拼接会有严重的安全隐患!建议使用占位符吧,然后定义一个list来保存条件参数(有位置顺序)。在hibernate的回调函数中再读取参数。
      

  5.   

    String sql = "select * from table where 1=1";
    if(name != ""){
    sql+=" and name="+name;
    }
    if(pass != ""){
    sql += " and pass="+pass;
    }
    ....执行查询语句。。
      

  6.   

    首先你的DAO查找的操作要这样写:
    public List findObjectByWhere(String sql_param,List param){
       StringBuffer sql = new StringBuffer("select * from tablet_name where 1=1 ");
      ........
    .........}  if(sql_param!=null && !sql_param.equals("")){
         sql.append(sql_param);
      }
     if(param!=null && param.size()!=0){
       //下面就是设置?号参数啦
      ....
     }拼接的地方就这样行啦
    先定义一个StringBuffer和一个ListStringBuffer buff = new StringBuffer();
    List list = new ArrayList();
    if(name!=null && !name.equals("")){
       buff.append(" and name like ? ");
       list.add("%"+name+"%");//这个是模糊查询
    }
    if(age!=null && !age.equals("")){
       buff.append(" and age=? ");
       list.add(age);
    }
    ...
    ...
    ...
    之后就调用DAO的方法  findObjectByWhere(buff.toString(),list);
    这样就行啦,不知我说明白了没有
      

  7.   

    11楼正解,1楼的那个思路是对的,不过查询条件要是有转移字符比如单引号就会出现问题。hibernate最好是采用11楼的动态设置参数,可以避免转义字符的问题及sql注入安全问题
      

  8.   

    ,11楼的那个list大可不不必,可以去掉
      

  9.   

    lz 问的可是一到面试的笔试题呀。我当时没写好,结果被pass了。
    写法可以参考 《精通Hibernate》-- 11.5.1 动态查询 p332 或者
    StringBuffer hql = new StringBuffer ("select from User u where 1=1");
    if (user.getTitle() != null && !user.getTitle().equals("")) {
    hql.append(" and u.title like '%" + user.getTitle() + "%'");
    }
    if (user.getAge() != null && !user.getAge().equals("")) {
    hql.append(" and u.age ='" + user.getAge() + "'");
    }
    return getSession().createQuery(hql.toString());
      

  10.   

    可以写一个sqlcondition model类 给它三个属性 第一个是 name(对象数据库的字段名) 第二个是value(对象要查询的条件值) operate(对应查询操作符:like = >等)在写个类 在select 、、 from tbl 加上这个类处理sqlcondtion model类后返回的where字符串 加在select语句的后面即可!
      

  11.   


    好像没看懂我提的问题,要的是实现sql或hql的动态查询