首页 文章

如何在Spring Data中使用OrderBy和findAll

提问于
浏览
177

我正在使用spring数据,我的DAO看起来像

public interface StudentDAO extends JpaRepository<StudentEntity, Integer> {
    public findAllOrderByIdAsc();   // I want to use some thing like this
}

在上面的代码中,注释行显示了我的意图 . Spring Data可以提供内置功能,使用这种方法通过ASC / DESC查找某些列的所有记录顺序吗?

5 回答

  • 426
    public interface StudentDAO extends JpaRepository<StudentEntity, Integer> {
        public List<StudentEntity> findAllByOrderByIdAsc();
    }
    

    上面的代码应该有效 . 我正在使用类似的东西:

    public List<Pilot> findTop10ByOrderByLevelDesc();
    

    它返回10行,具有最高级别 .

    IMPORTANT: 由于我很容易错过这个答案的关键点,这里有一点澄清:

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

    AFAIK,我不认为使用直接方法命名查询是可行的 . 但是,您可以使用Sort类使用内置排序机制 . 存储库有一个findAll(Sort)方法,您可以将 Sort 的实例传递给 . 例如:

    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");
        }
    }
    
  • 1

    请看这里:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods

    在以下部分:“表2.3 . 方法名称中支持的关键字”

    我认为它完全符合您的要求,并且您所说的相同查询应该有效...

  • 45

    是的,您可以使用Spring Data中的查询方法进行排序 .

    例如:使用id字段的值按升序或降序排列 .

    码:

    public interface StudentDAO extends JpaRepository<StudentEntity, Integer> {
        public findAllByOrderByIdAsc();   
    }
    

    替代方案:

    @Repository
    public class StudentServiceImpl implements StudentService {
        @Autowired
        private StudentDAO studentDao;
    
        @Override
        public List<Student> findAll() {
            return studentDao.findAll(orderByIdAsc());
        }
    private Sort orderByIdAsc() {
        return new Sort(Sort.Direction.ASC, "id")
                    .and(new Sort(Sort.Direction.ASC, "name"));
    }
    }
    

    Spring数据排序:Sorting

  • 0

    我尝试在这个例子中向您展示个性化OrderBy排序的完整示例

    import java.util.List;
     import org.springframework.data.domain.Page;
     import org.springframework.data.domain.Sort;
     import org.springframework.data.jpa.repository.*;
     import org.springframework.data.repository.query.Param;
     import org.springframework.stereotype.Repository;
     import org.springframework.data.domain.Sort;
     /**
     * Spring Data  repository for the User entity.
     */
     @SuppressWarnings("unused")
     @Repository
     public interface UserRepository extends JpaRepository<User, Long> {
     List <User> findAllWithCustomOrderBy(Sort sort);
     }
    

    您将使用此示例:一种动态构建对象实例Sort的方法:

    import org.springframework.data.domain.Sort;
    public class SampleOrderBySpring{
     Sort dynamicOrderBySort = createSort();
         public static void main( String[] args )
         {
           System.out.println("default sort \"firstName\",\"name\",\"age\",\"size\" ");
           Sort defaultSort = createStaticSort();
           System.out.println(userRepository.findAllWithCustomOrderBy(defaultSort ));
    
    
           String[] orderBySortedArray = {"name", "firstName"};
           System.out.println("default sort ,\"name\",\"firstName\" ");
           Sort dynamicSort = createDynamicSort(orderBySortedArray );
           System.out.println(userRepository.findAllWithCustomOrderBy(dynamicSort ));
          }
          public Sort createDynamicSort(String[] arrayOrdre) {
            return  Sort.by(arrayOrdre);
            }
    
       public Sort createStaticSort() {
            String[] arrayOrdre  ={"firstName","name","age","size");
            return  Sort.by(arrayOrdre);
            }
    }
    

相关问题