我使用spring数据,我的DAO看起来像
public interface StudentDAO extends JpaRepository<StudentEntity, Integer> { public findAllOrderByIdAsc(); // I want to use some thing like this }
请查看“表3。方法名称中支持的关键字”中的Spring Data JPA -参考文档,第5.3节。查询方法,特别是5.3.2. 创建查询部分(截至2019-05-03的链接)。
我认为它确实有你需要的,和你说的一样的查询应该工作…
AFAIK,我认为这对于直接的方法命名查询是不可能的。然而,你可以使用内置的排序机制,使用Sort类。存储库有一个findAll(Sort)方法,你可以将Sort的实例传递给它。例如:
Sort
findAll(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"); } }
public interface StudentDAO extends JpaRepository<StudentEntity, Integer> { public List<StudentEntity> findAllByOrderByIdAsc(); }
上面的代码应该可以工作。我用的是类似的东西:
public List<Pilot> findTop10ByOrderByLevelDesc();
它返回最高级别的10行。
< >强重要: 因为我被告知很容易错过这个答案的关键点,这里有一点澄清:
findAllByOrderByIdAsc(); // don't miss "by" ^
是的,你可以在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数据排序:排序
在这个示例中,我试图向您展示一个完整的示例,以个性化您的orderderby排序
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); } }
简单的方法:
repository.findAll(Sort.by(Sort.Direction.DESC, "colName"));
来源:https://www.baeldung.com/spring-data-sorting
结合以上所有答案,你可以用BaseEntity编写可重用的代码:
@Data @NoArgsConstructor @MappedSuperclass public abstract class BaseEntity { @Transient public static final Sort SORT_BY_CREATED_AT_DESC = Sort.by(Sort.Direction.DESC, "createdAt"); @Id private Long id; private LocalDateTime createdAt; private LocalDateTime updatedAt; @PrePersist void prePersist() { this.createdAt = LocalDateTime.now(); } @PreUpdate void preUpdate() { this.updatedAt = LocalDateTime.now(); } }
DAO对象重载findAll方法——基本上,仍然使用findAll()
findAll()
public interface StudentDAO extends CrudRepository<StudentEntity, Long> { Iterable<StudentEntity> findAll(Sort sort); }
StudentEntity扩展了包含可重复字段的BaseEntity(也许你也想按ID排序)
StudentEntity
BaseEntity
@Getter @Setter @FieldDefaults(level = AccessLevel.PRIVATE) @Entity class StudentEntity extends BaseEntity { String firstName; String surname; }
最后,SORT_BY_CREATED_AT_DESC的服务和用法,可能不仅会在StudentService中使用。
SORT_BY_CREATED_AT_DESC
StudentService
@Service class StudentService { @Autowired StudentDAO studentDao; Iterable<StudentEntity> findStudents() { return this.studentDao.findAll(SORT_BY_CREATED_AT_DESC); } }