com.querydsl.core.types.Order Java Examples

The following examples show how to use com.querydsl.core.types.Order. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SysAdvertiseService.java    From ZTuoExchange_framework with MIT License 6 votes vote down vote up
/**
 * 条件查询对象 pageNo pageSize 同时传时分页
 *
 * @param predicateList
 * @param pageNo
 * @param pageSize
 * @return
 */
@Transactional(readOnly = true)
public PageResult<SysAdvertise> query(List<Predicate> predicateList, Integer pageNo, Integer pageSize) {
    List<SysAdvertise> list;
    JPAQuery<SysAdvertise> jpaQuery = queryFactory.selectFrom(sysAdvertise);
    if (predicateList != null) {
        jpaQuery.where(predicateList.toArray(new Predicate[predicateList.size()]));
    }
    if (pageNo != null && pageSize != null) {
        list = jpaQuery.orderBy(new OrderSpecifier<>(Order.DESC, sysAdvertise.createTime))
                .offset((pageNo - 1) * pageSize).limit(pageSize).fetch();
    } else {
        list = jpaQuery.orderBy(new OrderSpecifier<>(Order.DESC, sysAdvertise.createTime)).fetch();
    }
    return new PageResult<>(list, jpaQuery.fetchCount());
}
 
Example #2
Source File: PageModel.java    From ZTuoExchange_framework with MIT License 5 votes vote down vote up
public List<Order> toOrders(List<Sort.Direction> list){
    List<Order> orders = new ArrayList<>();
    for(Sort.Direction direction:list){
        orders.add(directoryToOrder(direction));
    }
    return orders ;
}
 
Example #3
Source File: QuerydslUtil.java    From eds-starter6-jpa with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
public static OrderSpecifier[] createOrderSpecifiers(
		ExtDirectStoreReadRequest request, Class<?> clazz,
		EntityPathBase<?> entityPathBase, Map<String, String> mapGuiColumn2Dbfield,
		Set<String> sortIgnoreProperties) {

	List<OrderSpecifier> orders;

	if (!request.getSorters().isEmpty()) {
		orders = new ArrayList<>();
		PathBuilder<?> entityPath = new PathBuilder<>(clazz,
				entityPathBase.getMetadata());
		for (SortInfo sortInfo : request.getSorters()) {

			if (!sortIgnoreProperties.contains(sortInfo.getProperty())) {
				Order order;
				if (sortInfo.getDirection() == SortDirection.ASCENDING) {
					order = Order.ASC;
				}
				else {
					order = Order.DESC;
				}

				String property = mapGuiColumn2Dbfield.get(sortInfo.getProperty());
				if (property == null) {
					property = sortInfo.getProperty();
				}

				orders.add(new OrderSpecifier(order, entityPath.get(property)));
			}
		}

	}
	else {
		orders = Collections.emptyList();
	}

	return orders.toArray(new OrderSpecifier[orders.size()]);
}
 
Example #4
Source File: QuerydslQueryBackend.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
@Override
public OrderSpecifier<?> newSort(Expression<?> expr, Direction dir) {
	if (dir == Direction.ASC) {
		return new OrderSpecifier(Order.ASC, expr);
	}
	else {
		return new OrderSpecifier(Order.DESC, expr);
	}
}
 
Example #5
Source File: QuerydslQueryBackend.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
@Override
public OrderSpecifier<?> newSort(Expression<?> expr, Direction dir) {
	if (dir == Direction.ASC) {
		return new OrderSpecifier(Order.ASC, expr);
	}
	else {
		return new OrderSpecifier(Order.DESC, expr);
	}
}
 
Example #6
Source File: UserService.java    From codeway_service with GNU General Public License v3.0 5 votes vote down vote up
public QueryResults<User> findByCondition(User user, QueryVO queryVO) {

        QUser qUser = QUser.user;
        com.querydsl.core.types.Predicate predicate = null;
        OrderSpecifier<?> sortedColumn = QuerydslUtil.getSortedColumn(Order.DESC, qUser);
        if (StringUtils.isNotEmpty(user.getUserName())) {
            predicate = ExpressionUtils.and(predicate, qUser.userName.like(user.getUserName()));
        }
        if (user.getStatus() != null) {
            predicate = ExpressionUtils.and(predicate, qUser.status.eq(user.getStatus()));
        }
        if (StringUtils.isNotEmpty(user.getId())) {
            predicate = ExpressionUtils.and(predicate, qUser.status.eq(user.getStatus()));
        }
        if (StringUtils.isNotEmpty(user.getUserName())) {
            predicate = ExpressionUtils.and(predicate, qUser.status.eq(user.getStatus()));
        }
        if (StringUtils.isNotEmpty(user.getAccount())) {
            predicate = ExpressionUtils.and(predicate, qUser.account.eq(user.getAccount()));
        }
        if (StringUtils.isNotEmpty(user.getPhone())) {
            predicate = ExpressionUtils.and(predicate, qUser.phone.eq(user.getPhone()));
        }
        if (StringUtils.isNotEmpty(queryVO.getFieldSort())) {
            sortedColumn = QuerydslUtil.getSortedColumn(Order.DESC, qUser, queryVO.getFieldSort());
        }
        QueryResults<User> queryResults = jpaQueryFactory
                .selectFrom(qUser)
                .where(predicate)
                .offset(queryVO.getPageNum())
                .limit(queryVO.getPageSize())
                .orderBy(sortedColumn)
                .fetchResults();
        return queryResults;
    }
 
Example #7
Source File: RoleService.java    From codeway_service with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 条件查询角色
 *
 * @param role : Role
 * @return IPage<Role>
 */
public QueryResults<Role> findRuleByCondition(Role role, QueryVO queryVO) {

    QRole qRole = QRole.role;
    com.querydsl.core.types.Predicate predicate = null;
    OrderSpecifier<?> sortedColumn = QuerydslUtil.getSortedColumn(Order.DESC, qRole);
    if (StringUtils.isNotEmpty(role.getRoleName())) {
        predicate = ExpressionUtils.and(predicate, qRole.roleName.like(role.getRoleName()));
    }
    if (StringUtils.isNotEmpty(queryVO.getFieldSort())) {
        sortedColumn = QuerydslUtil.getSortedColumn(Order.DESC, qRole, queryVO.getFieldSort());
    }
    QueryResults<Role> queryResults = jpaQueryFactory
            .selectFrom(qRole)
            .where(predicate)
            .offset(queryVO.getPageNum())
            .limit(queryVO.getPageSize())
            .orderBy(sortedColumn)
            .fetchResults();
    return queryResults;
}
 
Example #8
Source File: SysHelpService.java    From ZTuoExchange_framework with MIT License 5 votes vote down vote up
public Page<SysHelp> findByCate(int pageNo,int pageSize,String cate){
    Sort sort = new Sort(new Sort.Order(Sort.Direction.DESC, "sort"));
    Pageable pageable = new PageRequest(pageNo - 1, pageSize, sort);
    Specification specification = new Specification() {
        List<javax.persistence.criteria.Predicate> predicates = new ArrayList<>();

        @Override
        public javax.persistence.criteria.Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
            predicates.add(criteriaBuilder.equal(root.get("SysHelpClassification"),cate));
            predicates.add(criteriaBuilder.equal(root.get("isTop"),"0"));
            return criteriaBuilder.and(predicates.toArray(new javax.persistence.criteria.Predicate[predicates.size()]));
        }
    };
    return sysHelpDao.findAll(specification,pageable);
}
 
Example #9
Source File: SysHelpService.java    From ZTuoExchange_framework with MIT License 5 votes vote down vote up
/**
 * 根据分类分页查询
 * @param pageNo
 * @param pageSize
 * @param cate
 * @return
 */
public Page<SysHelp> findByCondition(int pageNo,int pageSize,SysHelpClassification cate){
    Sort sort = new Sort(new Sort.Order(Sort.Direction.DESC, "sort"));
    Pageable pageable = new PageRequest(pageNo - 1, pageSize, sort);
    Specification specification = new Specification() {
        List<javax.persistence.criteria.Predicate> predicates = new ArrayList<>();

        @Override
        public javax.persistence.criteria.Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
            predicates.add(criteriaBuilder.equal(root.get("sysHelpClassification"),cate));
            return criteriaBuilder.and(predicates.toArray(new javax.persistence.criteria.Predicate[predicates.size()]));
        }
    };
    return sysHelpDao.findAll(specification,pageable);
}
 
Example #10
Source File: SysAdvertiseService.java    From ZTuoExchange_framework with MIT License 5 votes vote down vote up
public List<SysAdvertise> findAll(List<Predicate> predicateList) {
    List<SysAdvertise> list;
    JPAQuery<SysAdvertise> jpaQuery = queryFactory.selectFrom(sysAdvertise);
    if (predicateList != null) {
        jpaQuery.where(predicateList.toArray(new Predicate[predicateList.size()]));
    }
    list = jpaQuery.orderBy(new OrderSpecifier<>(Order.DESC, sysAdvertise.createTime)).fetch();
    return list;
}
 
Example #11
Source File: SysAdvertiseService.java    From ZTuoExchange_framework with MIT License 5 votes vote down vote up
/**
 * 条件查询对象 pageNo pageSize 同时传时分页
 *
 * @param predicateList
 * @param pageNo
 * @param pageSize
 * @return
 */
@Transactional(readOnly = true)
public PageResult<SysAdvertise> query(List<Predicate> predicateList, Integer pageNo, Integer pageSize) {
    List<SysAdvertise> list;
    JPAQuery<SysAdvertise> jpaQuery = queryFactory.selectFrom(sysAdvertise);
    if (predicateList != null) {
        jpaQuery.where(predicateList.toArray(new Predicate[predicateList.size()]));
    }
    if (pageNo != null && pageSize != null) {
        list = jpaQuery.orderBy(new OrderSpecifier<>(Order.DESC, sysAdvertise.createTime))
                .offset((pageNo - 1) * pageSize).limit(pageSize).fetch();
    } else {
        list = jpaQuery.orderBy(new OrderSpecifier<>(Order.DESC, sysAdvertise.createTime)).fetch();
    }
    return new PageResult<>(list, jpaQuery.fetchCount());
}
 
Example #12
Source File: RoleService.java    From codeway_service with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 条件查询角色
 *
 * @param role : Role
 * @return IPage<Role>
 */
public QueryResults<Role> findRuleByCondition(Role role, QueryVO queryVO) {

    QRole qRole = QRole.role;
    com.querydsl.core.types.Predicate predicate = null;
    OrderSpecifier<?> sortedColumn = QuerydslUtil.getSortedColumn(Order.DESC, qRole);
    if (StringUtils.isNotEmpty(role.getRoleName())) {
        predicate = ExpressionUtils.and(predicate, qRole.roleName.like(role.getRoleName()));
    }
    if (StringUtils.isNotEmpty(queryVO.getFieldSort())) {
        sortedColumn = QuerydslUtil.getSortedColumn(Order.DESC, qRole, queryVO.getFieldSort());
    }
    QueryResults<Role> queryResults = jpaQueryFactory
            .selectFrom(qRole)
            .where(predicate)
            .offset(queryVO.getPageNum())
            .limit(queryVO.getPageSize())
            .orderBy(sortedColumn)
            .fetchResults();
    return queryResults;
}
 
Example #13
Source File: PageModel.java    From ZTuoExchange_framework with MIT License 5 votes vote down vote up
private Sort getSort() {
    List<Sort.Order> orders = null;
    setSort();
    if(direction.size() == property.size()) {
        orders = new ArrayList<>();
        int length = direction.size();
        for (int i = 0; i < length; i++) {
            orders.add(new Sort.Order(direction.get(i), property.get(i)));
        }
    }
    return new Sort(orders);
}
 
Example #14
Source File: SysHelpService.java    From ZTuoExchange_framework with MIT License 5 votes vote down vote up
public Page<SysHelp> findByCate(int pageNo,int pageSize,String cate){
    Sort sort = new Sort(new Sort.Order(Sort.Direction.DESC, "sort"));
    Pageable pageable = new PageRequest(pageNo - 1, pageSize, sort);
    Specification specification = new Specification() {
        List<javax.persistence.criteria.Predicate> predicates = new ArrayList<>();

        @Override
        public javax.persistence.criteria.Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
            predicates.add(criteriaBuilder.equal(root.get("SysHelpClassification"),cate));
            predicates.add(criteriaBuilder.equal(root.get("isTop"),"0"));
            return criteriaBuilder.and(predicates.toArray(new javax.persistence.criteria.Predicate[predicates.size()]));
        }
    };
    return sysHelpDao.findAll(specification,pageable);
}
 
Example #15
Source File: SysHelpService.java    From ZTuoExchange_framework with MIT License 5 votes vote down vote up
/**
 * 根据分类分页查询
 * @param pageNo
 * @param pageSize
 * @param cate
 * @return
 */
public Page<SysHelp> findByCondition(int pageNo,int pageSize,SysHelpClassification cate){
    Sort sort = new Sort(new Sort.Order(Sort.Direction.DESC, "sort"));
    Pageable pageable = new PageRequest(pageNo - 1, pageSize, sort);
    Specification specification = new Specification() {
        List<javax.persistence.criteria.Predicate> predicates = new ArrayList<>();

        @Override
        public javax.persistence.criteria.Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
            predicates.add(criteriaBuilder.equal(root.get("sysHelpClassification"),cate));
            return criteriaBuilder.and(predicates.toArray(new javax.persistence.criteria.Predicate[predicates.size()]));
        }
    };
    return sysHelpDao.findAll(specification,pageable);
}
 
Example #16
Source File: SysAdvertiseService.java    From ZTuoExchange_framework with MIT License 5 votes vote down vote up
public List<SysAdvertise> findAll(List<Predicate> predicateList) {
    List<SysAdvertise> list;
    JPAQuery<SysAdvertise> jpaQuery = queryFactory.selectFrom(sysAdvertise);
    if (predicateList != null) {
        jpaQuery.where(predicateList.toArray(new Predicate[predicateList.size()]));
    }
    list = jpaQuery.orderBy(new OrderSpecifier<>(Order.DESC, sysAdvertise.createTime)).fetch();
    return list;
}
 
Example #17
Source File: PageModel.java    From ZTuoExchange_framework with MIT License 5 votes vote down vote up
public List<Order> toOrders(List<Sort.Direction> list){
    List<Order> orders = new ArrayList<>();
    for(Sort.Direction direction:list){
        orders.add(directoryToOrder(direction));
    }
    return orders ;
}
 
Example #18
Source File: PageModel.java    From ZTuoExchange_framework with MIT License 5 votes vote down vote up
private Sort getSort() {
    List<Sort.Order> orders = null;
    setSort();
    if(direction.size() == property.size()) {
        orders = new ArrayList<>();
        int length = direction.size();
        for (int i = 0; i < length; i++) {
            orders.add(new Sort.Order(direction.get(i), property.get(i)));
        }
    }
    return new Sort(orders);
}
 
Example #19
Source File: UserService.java    From codeway_service with GNU General Public License v3.0 5 votes vote down vote up
public QueryResults<User> findByCondition(User user, QueryVO queryVO) {

        QUser qUser = QUser.user;
        com.querydsl.core.types.Predicate predicate = null;
        OrderSpecifier<?> sortedColumn = QuerydslUtil.getSortedColumn(Order.DESC, qUser);
        if (StringUtils.isNotEmpty(user.getUserName())) {
            predicate = ExpressionUtils.and(predicate, qUser.userName.like(user.getUserName()));
        }
        if (user.getStatus() != null) {
            predicate = ExpressionUtils.and(predicate, qUser.status.eq(user.getStatus()));
        }
        if (StringUtils.isNotEmpty(user.getId())) {
            predicate = ExpressionUtils.and(predicate, qUser.status.eq(user.getStatus()));
        }
        if (StringUtils.isNotEmpty(user.getUserName())) {
            predicate = ExpressionUtils.and(predicate, qUser.status.eq(user.getStatus()));
        }
        if (StringUtils.isNotEmpty(user.getAccount())) {
            predicate = ExpressionUtils.and(predicate, qUser.account.eq(user.getAccount()));
        }
        if (StringUtils.isNotEmpty(user.getPhone())) {
            predicate = ExpressionUtils.and(predicate, qUser.phone.eq(user.getPhone()));
        }
        if (StringUtils.isNotEmpty(queryVO.getFieldSort())) {
            sortedColumn = QuerydslUtil.getSortedColumn(Order.DESC, qUser, queryVO.getFieldSort());
        }
        QueryResults<User> queryResults = jpaQueryFactory
                .selectFrom(qUser)
                .where(predicate)
                .offset(queryVO.getPageNum())
                .limit(queryVO.getPageSize())
                .orderBy(sortedColumn)
                .fetchResults();
        return queryResults;
    }
 
Example #20
Source File: PageModel.java    From ZTuoExchange_framework with MIT License 4 votes vote down vote up
public Order directoryToOrder(Sort.Direction direction){
   return direction.isAscending() ? com.querydsl.core.types.Order.ASC : com.querydsl.core.types.Order.DESC;
}
 
Example #21
Source File: PageModel.java    From ZTuoExchange_framework with MIT License 4 votes vote down vote up
public Order directoryToOrder(Sort.Direction direction){
   return direction.isAscending() ? com.querydsl.core.types.Order.ASC : com.querydsl.core.types.Order.DESC;
}
 
Example #22
Source File: QuerydslUtil.java    From codeway_service with GNU General Public License v3.0 4 votes vote down vote up
public static OrderSpecifier<?> getSortedColumn(Order order, Path<?> parent) {
    return getSortedColumn(order, parent, "createAt");
}
 
Example #23
Source File: QuerydslUtil.java    From codeway_service with GNU General Public License v3.0 4 votes vote down vote up
public static OrderSpecifier<?> getSortedColumn(Order order, Path<?> parent) {
    return getSortedColumn(order, parent, "createAt");
}
 
Example #24
Source File: QueryDslRepositorySupportExt.java    From springlets with Apache License 2.0 4 votes vote down vote up
/**
 * Applies the given {@link Pageable} to the given {@link JPQLQuery}.
 * Allows to map the attributes to order as provided in the {@link Pageable}
 * to real entity attributes. This might be used to work with projections
 * or DTOs whose attributes don't have the same name as the entity ones.
 *
 * It allows to map to more than one entity attribute. As an example, if
 * the DTO used to create the {@link Pageable} has a fullName attribute, you
 * could map that attribute to two entity attributes: name and surname.
 * In this case, the {@link Pageable} defines an order by a fullName
 * attribute, but que query will order by name and surname instead.
 *
 * @param pageable the ordering and paging
 * @param query
 * @param attributeMapping definition of a mapping of order attribute names
 *        to real entity ones
 * @return the updated query
 */
protected JPQLQuery<T> applyPagination(Pageable pageable, JPQLQuery<T> query,
    Map<String, Path<?>[]> attributeMapping) {

  if (pageable == null) {
    return query;
  }

  Pageable mappedPageable;
  Sort sort = pageable.getSort();
  if (sort != null) {
    List<Sort.Order> mappedOrders = new ArrayList<Sort.Order>();
    for (Sort.Order order : sort) {
      if (!attributeMapping.containsKey(order.getProperty())) {
        LOG.warn(
            "The property (%1) is not included in the attributeMapping, will order "
                + "using the property as it is",
            order.getProperty());
        mappedOrders.add(order);
      } else {
        Path<?>[] paths = attributeMapping.get(order.getProperty());
        for (Path<?> path : paths) {
          Sort.Order mappedOrder =
              new Sort.Order(order.getDirection(), preparePropertyPath(path));
          mappedOrders.add(mappedOrder);
        }
      }
    }
    if (mappedOrders.isEmpty()) {
      // No properties to order by are available, so don't apply ordering and return the query
      // as it is
      return query;
    }
    mappedPageable =
        new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), new Sort(mappedOrders));
    return applyPagination(mappedPageable, query);
  } else {
    return applyPagination(pageable, query);
  }

}
 
Example #25
Source File: QueryDslRepositorySupportExt.java    From springlets with Apache License 2.0 3 votes vote down vote up
/**
 * Adds to a query an order by the entity identifier related to this repository.
 * This is useful as the default last order in queries where pagination is
 * applied, so you have always an absolute order. Otherwise, the order
 * of the results depends on the database criteria, which might change
 * even between pages, returning confusing results for the user.
 * @param query
 * @return the updated query
 */
@SuppressWarnings({"rawtypes", "unchecked"})
protected JPQLQuery<T> applyOrderById(JPQLQuery<T> query) {
  PathBuilder<Object> idPath = getEntityId();

  return query.orderBy(new OrderSpecifier(Order.ASC, idPath, NullHandling.NullsFirst));
}
 
Example #26
Source File: QuerydslUtil.java    From codeway_service with GNU General Public License v3.0 2 votes vote down vote up
/**
 * 根据字符串字段获取排序类
 *
 * @param order:Order.DESC
 * @param fieldName:排序字段
 * @return OrderSpecifier
 * @see https://stackoverflow.com/questions/39576236/dynamic-sorting-in-querydsl
 */
public static OrderSpecifier<?> getSortedColumn(Order order, Path<?> parent, String fieldName) {
    Path<Object> fieldPath = Expressions.path(Object.class, parent, fieldName);
    return new OrderSpecifier(order, fieldPath);
}
 
Example #27
Source File: QuerydslUtil.java    From codeway_service with GNU General Public License v3.0 2 votes vote down vote up
/**
 * 根据字符串字段获取排序类
 *
 * @param order:Order.DESC
 * @param fieldName:排序字段
 * @return OrderSpecifier
 * @see https://stackoverflow.com/questions/39576236/dynamic-sorting-in-querydsl
 */
public static OrderSpecifier<?> getSortedColumn(Order order, Path<?> parent, String fieldName) {
    Path<Object> fieldPath = Expressions.path(Object.class, parent, fieldName);
    return new OrderSpecifier(order, fieldPath);
}