How to use OrderBy with findAll in Spring Data

前端 未结 7 991
无人共我
无人共我 2020-12-02 04:25

I am using spring data and my DAO looks like

public interface StudentDAO extends JpaRepository {
    public findAllOrderByIdAsc         


        
相关标签:
7条回答
  • 2020-12-02 04:55

    AFAIK, I don't think this is possible with a direct method naming query. You can however use the built in sorting mechanism, using the Sort class. The repository has a findAll(Sort) method that you can pass an instance of Sort to. For example:

    import org.springframework.data.domain.Sort;
    
    @Repository
    public class StudentServiceImpl implements StudentService {
        @Autowired
        private StudentDAO studentDao;
    
        @Override
        public List<Student> findAll() {
            return studentDao.findAll(sortByIdAsc());
        }
    
        private Sort sortByIdAsc() {
            return new Sort(Sort.Direction.ASC, "id");
        }
    } 
    
    0 讨论(0)
提交回复
热议问题