51代码网ORACLEMYSQLSQL SERVER其它数据库java/jspasp/asp.netC/C++/VC++APP应用其它语言服务器应用
您现在的位置: 51代码网 >> java >> 文章正文

sql如何实现多个条件查询自由动态拼接

更新时间:2012-4-12:  来源:51代码网

假如我有5个输入框,代表5个查询条件,怎样才能实现根据用户自由输入一个或多个条件的不同而得到不同的查询结果呢??
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;
}
穿个对象进去

  • 上一篇文章:
  • 下一篇文章: 没有了
  • 赞助商链接
    推荐文章
  • 此栏目下没有推荐文章
  • {
    设为首页 | 加入收藏 | 友情链接 | 网站地图 | 联系站长 |