SpringBoot项目中:使用Spring-Data-Jpa创建Sort()对象和PageRequest()遇到问题。【解决办法】

❌错误展示:

在使用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);
    }

猜你喜欢

转载自blog.csdn.net/Zp_insist/article/details/122838806