首页 > 代码库 > Hibernate 组合查询

Hibernate 组合查询

 

 1 public List<Student> findByCondition(Student stu) { 2     StringBuilder sb=new StringBuilder("from Student where 1=1"); 3     List<Object> params=new ArrayList<Object>();   //参数 4     String sid=stu.getSid(); 5     if(sid!=null&&!sid.trim().isEmpty()){ 6         sb.append(" and sid like ?"); 7         params.add("%"+sid+"%"); 8     } 9     10     SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");11     Date  birthday=stu.getBirthday();12     if(birthday!=null){13         sb.append(" and birthday = ?");14         params.add(birthday);15     }16     17     String sql=sb.toString();18     Query q = getCurrentSession().createQuery(sql);19     if (params != null && !params.isEmpty()) {20         for (int i=0;i<params.size();i++) {21             q.setParameter(i, params.get(i));22         }23     }24     return q.list();25     26     }

 

Hibernate 组合查询