❌错误展示:
在使用Spring-Data-JPA时,创建Sort()对象和PageRequest()出现如下错误:
‘Sort(org.springframework.data.domain.Sort.Direction, java.util.List<java.lang.String>)’ has private access in ‘org.springframework.data.domain.Sort’
‘PageRequest(int, int, org.springframework.data.domain.Sort)’ has protected access in ‘org.springframework.data.domain.PageRequest’
解决办法:
springboot2.2.1(含)以上的版本Sort已经不能再实例化了,构造方法已经是私有的了!
'Sort(org.springframework.data.domain.Sort.Direction, java.util.List<java.lang.String>)' has private access in 'org.springframework.data.domain.Sort'
改用Sort.by()获得Sort对象
Sort sort= Sort.by(Sort.Direction.DESC,"xxxxxx");
改用PageRequest.of()获得Pageable 对象
Pageable pageable= PageRequest.of(0, size, sort);
@Override public List<Type> listTypeTop(Integer size) { Sort sort= Sort.by(Sort.Direction.DESC,"xxxxxxxxx"); Pageable pageable= PageRequest.of(0, size, sort); return typeDao.findTop(pageable); }