GAP使用JDO查询

注意事项:
1、与条件使用 &&
2、过滤条件queryTemplate 不能加上排序order by语句。解决方法是使用query.setOrdering("orderDate desc");
public List<FanOrder> queryByUsername(String username,String startDate,String endDate){
		
		List<FanOrder> orders = null;
		Query query = null;
		try {
			String queryTemplate = "",filter="";
			if (!username.equals("")){
				queryTemplate = "username == \"%s\" && orderDate >= \"%s\" && orderDate <= \"%s\" ";
				filter = String.format(queryTemplate,username,startDate, endDate);
				
			}else{
				queryTemplate = "orderDate >= \"%s\" && orderDate <= \"%s\" ";
				filter = String.format(queryTemplate,startDate, endDate);
			}
			query = pm.newQuery(FanOrder.class,filter);
			query.setOrdering("orderDate desc"); 
			orders = (List<FanOrder>) query.execute();
		} catch (RuntimeException e) {
			e.printStackTrace();
		}finally { 
	        query.closeAll(); 
	    }
		return orders;
	}

你可能感兴趣的:(查询)