例如我的hql语句: from Book as book where 1=1 and book.BookName like :Name and book.Author like :Author ComplexSearchForm f=new ComplexSearchForm(); f.setName("test"); f.setAuthor("test2"); Query q = getCurrentSession().createQuery(hql); q.setProperties(f); 这样是可行的 |
|
补充一下吧:我在一个controller函数里面要这样调用:
@RequestMapping("/complexFind") public void complexFind(ComplexSearchForm searchForm,HttpServletRequest request, HttpServletResponse response) throws Exception { page=ServletRequestUtils.getIntParameter(request, "page", 1);//默认值为1 rows=ServletRequestUtils.getIntParameter(request, "rows", 0); String hql=bookService.getCompleSearchHQL(searchForm); books=bookService.find(hql,searchForm, page, rows); //省略。。。 } @Override public String getCompleSearchHQL(ComplexSearchForm searchForm) { String hql="from Book as book where 1=1 ";//前台已做验证,不能提交空的查询条件 if(searchForm.getName()!=null){ hql+="and book.BookName like :Name "; } if(searchForm.getAuthor()!=null){ if(searchForm.getName()!=null){ hql+=searchForm.getLogic1()+"book.Author like :Author "; }else{ hql+="and book.Author like :Author "; } } if(searchForm.getSeries()!=null){ if(searchForm.getAuthor()!=null){ hql+=searchForm.getLogic2()+"book.Series like :Series "; }else{ if(searchForm.getName()!=null){ hql+=searchForm.getLogic1()+"book.Series like :Series "; }else{ hql+="and book.Series like :Series "; } } } return hql; } 现在就是怎么使用setProperties方法 和通配符 % |
|
解决了。。。在ComplexSearchForm?上的set函数弄成
public void setName(String Name) { if(Name!=null){ this.Name ="%"+ Name+"%"; }else this.Name=null; // this.Name = Name; } 这样形式的就行 |