在此介绍我所用的一种方式:
第一步,引包
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); }
时间: 2024-11-05 23:34:55