方法一:
YourObject findFirstBy[field]OrderBy[field]Asc();
YourObject findTopBy[field]OrderBy[field]Desc();
:
findTopByClassIdAndStudentNameOrderByUpdatedTimeDesc(classId,studentName);
方法二:使用原生的sql语句
@Query(value =“your sql str limit 1", nativeQuery = true)
Object _queryById();
:
@Repository
public interface JpaStudentRepository extends PagingAndSortingRepository {
@Query(value =
"SELECT * FROM student WHERE student_id=:studentId ORDER BY update_time DESC LIMIT 1",nativeQuery = true)
DbObject _queryStudent(Long studentId);
#需要注意的是,返回的类型必须是中的DbObject,这两种类型一致。
}
其他方式参考:https://www.baeldung.com/jpa-limit-query-results