How to use OrderBy with findAll in Spring Data

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

    AFAIK, I don't think this is possible with a direct method naming query. You can however use the built in sorting mechanism, using the Sort class. The repository has a findAll(Sort) method that you can pass an instance of Sort to. For example:

    import org.springframework.data.domain.Sort;
    
    @Repository
    public class StudentServiceImpl implements StudentService {
        @Autowired
        private StudentDAO studentDao;
    
        @Override
        public List findAll() {
            return studentDao.findAll(sortByIdAsc());
        }
    
        private Sort sortByIdAsc() {
            return new Sort(Sort.Direction.ASC, "id");
        }
    } 
    

提交回复
热议问题