标签:main 判断 dir derby request 排序 res tor roo
在此介绍我所用的一种方式:
第一步,引包
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
第二步,service方法代码
@Override
public Page<TurnOverRecord> findAll(Integer pageNum, Integer pageSize, TurnOverRecord turnOverRecord) {
//先按isTurnOver从小到大升序,再按turnOverTime升序
List< Order> orders=new ArrayList< Order>();
orders.add( new Order(Sort.Direction. ASC, "isTurnOver"));
orders.add( new Order(Sort.Direction. ASC, "turnOverTime"));
Pageable pageable= new PageRequest(pageNum - 1, pageSize, new Sort(orders));
Specification<TurnOverRecord> specification = new Specification<TurnOverRecord>() {
@Override
public Predicate toPredicate(Root<TurnOverRecord> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Predicate predicate = cb.conjunction();
//单属性排序
// query.orderBy(cb.asc(root.get("isTurnOver")));
// query.orderBy(cb.desc(root.get("id")));
// query.groupBy(root.get("id"));
query.distinct(true);
//判断是否有条件
predicate.getExpressions().add(cb.equal(root.get("flag"), 1));
if (turnOverRecord != null) {
if (StringUtil.isNotNull(turnOverRecord.getMemberId())
&& StringUtil.isNotNullAndZero(turnOverRecord.getMemberId().getId())
) {
predicate.getExpressions().add(cb.equal(root.get("memberId").get("id"), turnOverRecord.getMemberId().getId()));
}
if (StringUtil.isNotNull(turnOverRecord.getMemberId())
&& StringUtil.isNotNullAndBlank(turnOverRecord.getMemberId().getName())
) {
predicate.getExpressions().add(cb.like(root.get("memberId").get("name"), "%"+turnOverRecord.getMemberId().getName().trim()+"%"));
}
if (StringUtil.isNotNull(turnOverRecord.getCreator())
&& StringUtil.isNotNullAndBlank(turnOverRecord.getCreator().getName())) {
predicate.getExpressions().add(cb.like(root.get("creator").get("name"), "%"+turnOverRecord.getCreator().getName().trim() + "%"));
}
}
return predicate;
}
};
return turnOverRecordDao.findAll(specification, pageable);
}
spring data JPA 中的多属性排序---养老院管理系统涉及技术整理
标签:main 判断 dir derby request 排序 res tor roo
原文地址:http://www.cnblogs.com/meng-ma-blogs/p/7466244.html