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

用户管理系统——多条件搜索_第1张图片

cstm.dao

public List query(Customer criteria) {
		/*
		 * 搜索条件中包括cname,gender,cellphone,email
		 */
		// TODO Auto-generated method stub
		try {
		//1.给出sql语句前半句
			StringBuilder sql=new StringBuilder("select * from t_customer where 1=1");
			//2.判断条件,追加where子句
			//3.创建一个ArrayList,装载参数
			List params=new ArrayList();
			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.class),params.toArray());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
		
		
	} 
  
cstm.servlet

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


你可能感兴趣的:(JAVA,web)