1.在spring data for jpa 中,存在一个pageable接口,pageable接口的实现类的构造方法中有个Sort参数,可以按照列属性进行排序。通过查看Sort类的构造方法,我们对Sort这个类进行一下分析,Sort类中存在以下几个构造方法:
1).public Sort(Order... orders);
2).public Sort(List<Order> orders);
3).public Sort(String... properties);
4).public Sort(Direction direction, String... properties);
5).public Sort(Direction direction, List<String> properties);
2.在上面这几种构造方法,其中Direction 是用来标识按照列属性升序还是降序排序的,properties即为列属性。
1).方法1和2性质相同,主要是Order类的用途是怎样的。
2).方法3只是输入列属性,按照默认的排序方式(ASC),因此也不能满足要求。
3).方法4和5方法由于只能够实用一种排序方向,但是列属性是一和多区别。
4.再看Order类的构造方法:
1).public Order(Direction direction, String property);
2).可以看到一个Order维护一个Direction 和一个列属性,正式我们所要的。这样我们可以做到先升序再降序的功能。
List< Order> orders=new ArrayList< Order>();
orders.add( new Order(Direction. ASC, "c"));
orders.add( new Order(Direction. DESC, "d"));
Pageable pageable= new PageRequest(pageNo, pageSize, new Sort(orders));
5.举个栗子,包含各种查询方式
public Page<Spu> findPage(int pageNo, int pageSize, SpuSearchDTO entity){
List<Sort.Order> orders = new ArrayList<>();
orders.add(new Sort.Order(Sort.Direction.DESC, "createDate"));
orders.add(new Sort.Order(Sort.Direction.ASC, "updateDate"));
Sort sort = new Sort(orders);
PageRequest pageRequest;
if (pageSize == -1) {
//不分页
pageRequest = new PageRequest(0, Integer.MAX_VALUE,sort);
} else {
//分页查询
pageRequest = new PageRequest((pageNo-1), pageSize,sort);
}
return spuRepository.findAll(where(entity), pageRequest);
}
private Specification<Spu> where(final SpuSearchDTO entity) {
return new Specification<Spu>() {
@Override
public Predicate toPredicate(Root<Spu> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> list = new ArrayList<Predicate>();
list.add(cb.equal(root.get("delFlag").as(Boolean.class), false));
if (null != entity.getSpuId()){
list.add(cb.equal(root.get("spuId").as(Long.class), entity.getSpuId()));
}
//传一个list查询
if (null != entity.getIds() && entity.getIds().size() > 0) {
CriteriaBuilder.In<Object> in = cb.in(root.get("spuId"));
for (int i = 0; i < entity.getIds().size(); i++) {
in.value(entity.getIds().get(i));
}
list.add(in);
}
}
if (null != entity.getStandardFlag()){
list.add(cb.equal(root.get("standardFlag").as(Boolean.class), entity.getStandardFlag()));
}
if (null != entity.getBrandId()){
list.add(cb.equal(root.get("brandId").as(Long.class), entity.getBrandId()));
}
//查询更新时间在此时间范围内的所有spu对象
/* if (null != entity.getCreateTimeStart){
list.add(cb.greaterThanOrEqualTo(root.get("updateDate").as(Date.class), entity.getCreateTimeStart()));
}
if (null != entity.getCreateTimeEnd()()){
list.add(cb.lessThanOrEqualTo(root.get("updateDate").as(Date.class), entity.getCreateTimeEnd()));
}*/
//查询指定日期当天的所有spu对象
if(entity.getCreateDate() != null) {
Date startDate = entity.getCreateDate();
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
calendar.set(Calendar.HOUR_OF_DAY,0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
list.add(cb.greaterThanOrEqualTo(root.get("createDate").as(Date.class), calendar.getTime()));
}
if(entity.getCreateDate() != null) {
Date endDate = entity.getCreateDate();
Calendar calendar = Calendar.getInstance();
calendar.setTime(endDate);
calendar.set(Calendar.HOUR_OF_DAY,23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
list.add(cb.lessThanOrEqualTo(root.get("createDate").as(Date.class), calendar.getTime()));
}
return query.where(list.toArray(new Predicate[list.size()])).getRestriction();
}
};
}