用户管理系统——多条件搜索

来源:互联网 发布:被广电禁播的网络剧 编辑:程序博客网 时间:2024/06/06 03:37

cstm.dao

public List<Customer> query(Customer criteria) {/* * 搜索条件中包括cname,gender,cellphone,email */// TODO Auto-generated method stubtry {//1.给出sql语句前半句StringBuilder sql=new StringBuilder("select * from t_customer where 1=1");//2.判断条件,追加where子句//3.创建一个ArrayList,装载参数List<Object> params=new ArrayList<Object>();String cname=criteria.getCname();if(cname!=null && !cname.trim().isEmpty()){sql.append(" and cname like ?");params.add("%"+cname+"%");}String gender=criteria.getGender();if(gender!=null && !gender.trim().isEmpty()){sql.append(" and gender=?");params.add(gender);}String cellphone=criteria.getCellphone();if(cellphone!=null && !cellphone.trim().isEmpty()){sql.append(" and cellphone like ?");params.add("%"+cellphone+"%");}String email=criteria.getEmail();if(email!=null && !email.trim().isEmpty()){sql.append(" and email like ?");params.add("%"+email+"%");}return qr.query(sql.toString(), new BeanListHandler <Customer>(Customer.class),params.toArray());} catch (SQLException e) {// TODO Auto-generated catch blockthrow new RuntimeException(e);}}

cstm.servlet

public String query(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {Customer criteria=CommonUtils.toBean(request.getParameterMap(), Customer.class);List <Customer> list=customerService.query(criteria);request.setAttribute("cstmList",list);return "f:/list.jsp";}


0 0
原创粉丝点击