How to use OrderBy with findAll in Spring Data

前端 未结 7 1028
无人共我
无人共我 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:33

    public interface StudentDAO extends JpaRepository {
        public List findAllByOrderByIdAsc();
    }
    

    The code above should work. I'm using something similar:

    public List findTop10ByOrderByLevelDesc();
    

    It returns 10 rows with the highest level.

    IMPORTANT: Since I've been told that it's easy to miss the key point of this answer, here's a little clarification:

    findAllByOrderByIdAsc(); // don't miss "by"
           ^
    

提交回复
热议问题