1. 程式人生 > >DetachedCriteria多條件查詢or,disjunction

DetachedCriteria多條件查詢or,disjunction

public Page getCoordByPage(Page queryHandler, TCoordinate conditions) {
		DetachedCriteria dc = DetachedCriteria.forClass(TCoordinate.class);
		if (conditions.getTAxis() != null) {
			dc.add(Restrictions.eq("TAxis", conditions.getTAxis()));
		}
		
		if(conditions.getCoordinateName()!=null){
			dc.add(Restrictions.like("coordinateName", conditions.getCoordinateName(),MatchMode.ANYWHERE).ignoreCase());
		}

		if(conditions.getParentCoordinate().getId()!=null){
			dc.add(Restrictions.eq("parentCoordinate.id", conditions.getParentCoordinate().getId()));
		}

		dc.addOrder(Order.desc("addDate"));
		Page page = this.coordinateDao.getPageByCriteria(queryHandler, dc);
		return page;
	}

兩個條件之間or的用法【Restrictions.or(lhs, rhs)】:

dc.add(Restrictions.or(Restrictions.like("country", comm.getCountry(),MatchMode.ANYWHERE).ignoreCase(), 
					Restrictions.like("pinyin", comm.getCountry(),MatchMode.ANYWHERE).ignoreCase()));


超過兩個,比如三個條件之間的or【Disjunction disjunction = Restrictions.disjunction();】:

if(!ValidateUtils.isEmpty(comm.getCountry())){
			Disjunction disjunction = Restrictions.disjunction();
			
			disjunction.add(Restrictions.like("country", comm.getCountry(),MatchMode.ANYWHERE).ignoreCase());
			disjunction.add(Restrictions.like("pinyin", comm.getCountry(),MatchMode.ANYWHERE).ignoreCase());
			disjunction.add(Restrictions.like("jianpin", comm.getCountry(),MatchMode.ANYWHERE).ignoreCase());
			
			dc.add(disjunction);
		}


瞬即漲姿勢!