条件追加hql查询

	public Map searchFlightChangeInfoFenye(Map paramMap);


    
	/**
	 * flightInfo search  航班信息搜索控制
	 * */
	public ActionForward flightInfoSearch(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
	    
		//得到搜索提供的参数
	
		String pageString=request.getParameter("page");
		int page=1;
		if (pageString!=null) {
			page=Integer.parseInt(pageString);
			int size=3;
			
			
			String passenger=request.getParameter("passenger"); //乘客姓名
			String Passenger=null;
			if (passenger.length()>0) {
				Passenger=passenger;
			}
			
			String flightNo=request.getParameter("flightNo"); //航班号
			String FlightNo=null;
			if (flightNo.length()>0) {
				FlightNo=flightNo;
			}
			
			String ticketNo=request.getParameter("ticketNo"); //票号
			String TicketNo=null;
			if (ticketNo.length()>0) {
				TicketNo=ticketNo;
			}
			
			String ticket_All=request.getParameter("ticket_All"); //总票
			String Ticket_All=null;
			if (ticket_All.length()>0) {
				Ticket_All=ticket_All;
			}
			
			String takeOff_time=request.getParameter("takeOff_time"); //起飞时间
			String TakeOff_time=null;
			if (takeOff_time.length()>0) {
				TakeOff_time=takeOff_time;
			}
			
			String input_time=request.getParameter("input_time"); //输入时间
			String Input_time=null;
			if (input_time.length()>0) {
				Input_time=input_time;
			}
			
			String input_time02=request.getParameter("input_time02"); //输入时间至
			String Input_time02=null;
			if (input_time02.length()>0) {
				Input_time02=input_time02;
			}
			
			Map paramsMap=new HashMap();
			paramsMap.put("page", page);
			paramsMap.put("size", size);
			paramsMap.put("Passenger", Passenger); //乘客姓名
			paramsMap.put("FlightNo", FlightNo); //航班号
			paramsMap.put("TicketNo", TicketNo); //票号
			paramsMap.put("Ticket_All", Ticket_All); //总票款
			paramsMap.put("TakeOff_time", TakeOff_time); //起飞时间
			paramsMap.put("Input_time", Input_time); //导入时间
			paramsMap.put("Input_time02", Input_time02); //导入时间至
			
			Map flightMap=iflightInfoService.flightInfoSearchFenye(paramsMap); 
			
			request.setAttribute("flightMap", flightMap);
			
		}
		/*int flag=Integer.parseInt(request.getSession().getAttribute("lim").toString());
		   System.out.println("fffffffffffffffffffffffffffff");
		   System.out.println(flag);
		   if (flag==1) {
				return new ActionForward("/flight_info_1.jsp");
			}else if (flag==2) {
				return new ActionForward("/flight_info_2.jsp");
			}else if (flag==3) {
				return new ActionForward("/flight_info_3.jsp");
			}*/
		return new ActionForward("/flight_info.jsp");
	}
	
	
	


public Map searchFlightChangeInfoFenye(Map paramMap) {
		// TODO Auto-generated method stub
		
		// 第几页
		Integer page = (Integer) paramMap.get("page");
		System.out.println("page--="+page);
		Integer size = (Integer) paramMap.get("size");
		
		//公司名称
		String voyage_company=(String) paramMap.get("voyage_company");
		System.out.println("voyage_company:"+voyage_company);
		//操作人名称
		String handlerString=(String) paramMap.get("handler");
		
		
		//手机号码
		String receive_cellphoneString=(String) paramMap.get("receive_cellphone");
		
		//状态
		int flight_state= -1;  
		String flight_stateString=(String) paramMap.get("flight_state");
		if (flight_stateString!=null && flight_stateString.length()>0) {
			flight_state=Integer.parseInt(flight_stateString);
		}
		
		//操作时间
		System.out.println("paramMap-------------:"+paramMap.get("handle_time"));//("(String) paramMap.get("handle_time"):"+(String) paramMap.get("handle_time"));
		String handle_timeString= (String) paramMap.get("handle_time");
		System.out.println("-----------------------");
	    Date handle_time=null;
		if (handle_timeString!=null ) {
			handle_time=java.sql.Date.valueOf(handle_timeString);//handle_timeString;
			System.out.println("handle_time==="+handle_time);
		}
		
		//接受时间开始
		String accept_timeString=(String) paramMap.get("accept_time");
		Date accept_time=null;
		if (accept_timeString!=null) {
			accept_time=java.sql.Date.valueOf(accept_timeString);
			System.out.println("accept_time==="+accept_time);
		}
		
		//接受时间结束
		String accept_time02String=(String) paramMap.get("accept_time02");
		Date accept_time02=null;
		if (accept_time02String!=null) {
			accept_time02=java.sql.Date.valueOf(accept_time02String);
			System.out.println("accept_time02==="+accept_time02);
		}
		
		//拼hql ----按条件追加------------------------------------------------
		
		String hqlcount="select count(*) from TbFlightChangeInfo where 1=1 ";
		String hqlString="from TbFlightChangeInfo where 1=1 ";
        System.out.println("]]]]]]]]]]]");
        
        //定义数组接收查询参数
		List values=new ArrayList();
		
		//判断公司名称是否具备例如查询条件
		if (voyage_company!=null && voyage_company.length()>0) {
			hqlString=hqlString+" and voyageCompany like ? ";
			hqlcount=hqlcount+" and voyageCompany like ? ";
			values.add(voyage_company+"%");
		}
		//判断操作者是否具备例如查询条件
		if (handlerString !=null && handlerString.length()>0) {
			hqlString=hqlString+" and handler=?";
			hqlcount=hqlcount+" and handler=?";
			values.add(handlerString);
		}
		//判断输入手机号码是否具备例如查询条件
		if (receive_cellphoneString !=null && receive_cellphoneString.length()>0) {
			hqlcount=hqlcount+" and receiveCellphone=? ";
			hqlString=hqlString+" and receiveCellphone=? ";
			values.add(receive_cellphoneString);
		}
		//判断状态是否具备例如查询条件
		if (flight_state != -1) {
			hqlString=hqlString+" and flightState=? ";
			hqlcount=hqlcount+" and flightState=? ";
			values.add(flight_state);
		}
		//判断操作时间是否具备例如查询条件
		if (handle_time!=null) {
			hqlString=hqlString+" and handleTime=?";
			hqlcount=hqlcount+" and handleTime=?";
			values.add(handle_time);
		}
		//判断接收时间是否具备例如查询条件
		if (accept_time!=null && accept_time02!=null) {
			hqlString=hqlString+" and receiveTime between ? and ?";
			hqlcount=hqlcount+" and receiveTime between ? and ?";
			values.add(accept_time);
			values.add(accept_time02);
		}
		else if (accept_time!=null && accept_time02==null) {
			hqlString=hqlString+" and receiveTime >= ?";
			hqlcount=hqlcount+" and receiveTime >= ?";
			values.add(accept_time);
			
		}
		else if (accept_time==null && accept_time02!=null) {
			hqlString=hqlString+" and receiveTime <= ?";
			hqlcount=hqlcount+" and receiveTime <= ?";
			values.add(accept_time02);
		}
		
		System.out.println("hqlString:"+hqlString);
		//设问号 ------------------------------------------------------
		
		Query query=getSession().createQuery(hqlString);
		Query queryCount=getSession().createQuery(hqlcount);
		
		if (values.size()>0) {
			System.out.println("values.size()"+values.size());
			for (int i = 0; i < values.size(); i++) {
				queryCount.setParameter(i,values.get(i));
				query.setParameter(i,values.get(i));
				
			}
			
		}
		/*String hql2="from TbFlightChangeInfo";
		List list0= getHibernateTemplate().find(hql2);
		System.out.println("query2.uniqueResult():"+list0.size());*/
		//总条数
	//	List sumList=getHibernateTemplate().find(hql2);
	//	int sum=sumList.size();
		Object countObject =queryCount.uniqueResult();
		int sum = Integer.parseInt(countObject.toString());
		System.out.println("sum:"+sum);
		//总页数
		int count=sum%size==0?sum/size:sum/size+1;
		System.out.println("count:"+count);
		//越界检查
		if (page<1) {
			page=1;
		}
		if (page>count) {
			page=count;
		}
		
		
		//查
		query.setFirstResult((page-1)*size).setMaxResults(size);
		List list=query.list();
		
		//保存至map
		Map resultMap=new HashMap();
		resultMap.put("list",list);
		resultMap.put("page",page);
		resultMap.put("size",size);
		resultMap.put("count",count);
		resultMap.put("sum",sum);
		
		return resultMap;
	}



你可能感兴趣的:(map,分页,条件查询,条件分页)