如何在 Spring Data 中使用 OrderBy 和 findAll [英] How to use OrderBy with findAll in Spring Data

查看:30
本文介绍了如何在 Spring Data 中使用 OrderBy 和 findAll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using spring data and my DAO looks like

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

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

In above code, commented line shows my intent. Can spring Data provides inbuilt functionality to use such a method to find all records order by some column with ASC/DESC?

推荐答案

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

上面的代码应该可以工作.我正在使用类似的东西:

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

public List<Pilot> findTop10ByOrderByLevelDesc();

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

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"
       ^

这篇关于如何在 Spring Data 中使用 OrderBy 和 findAll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆