Pra*_*kar 246 spring spring-data spring-data-jpa
我正在使用spring数据,我的DAO看起来像
public interface StudentDAO extends JpaRepository<StudentEntity, Integer> {
public findAllOrderByIdAsc(); // I want to use some thing like this
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,注释行显示了我的意图.Spring Data可以提供内置功能,使用这种方法通过ASC/DESC查找某些列的所有记录顺序吗?
Sik*_*kor 574
public interface StudentDAO extends JpaRepository<StudentEntity, Integer> {
public List<StudentEntity> findAllByOrderByIdAsc();
}
Run Code Online (Sandbox Code Playgroud)
上面的代码应该有效.我正在使用类似的东西:
public List<Pilot> findTop10ByOrderByLevelDesc();
Run Code Online (Sandbox Code Playgroud)
它返回10行,具有最高级别.
重要提示: 由于我被告知很容易错过这个答案的关键点,这里有一点澄清:
findAllByOrderByIdAsc(); // don't miss "by"
^
Run Code Online (Sandbox Code Playgroud)
Pau*_*tha 51
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");
}
}
Run Code Online (Sandbox Code Playgroud)
Tiz*_*ard 20
简单的方法:
repository.findAll(Sort.by(Sort.Direction.DESC, "colName"));
Run Code Online (Sandbox Code Playgroud)
来源:https : //www.baeldung.com/spring-data-sorting
Nar*_*chu 15
是的,您可以使用 Spring Data 中的查询方法进行排序。
例如:使用 id 字段的值进行升序或降序排列。
代码:
public interface StudentDAO extends JpaRepository<StudentEntity, Integer> {
public findAllByOrderByIdAsc();
}
Run Code Online (Sandbox Code Playgroud)
替代解决方案:
@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"));
}
}
Run Code Online (Sandbox Code Playgroud)
Spring数据排序:排序
shl*_*i33 12
请看这里:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods
在以下部分:"表2.3.方法名称中支持的关键字"
我认为它完全符合您的要求,并且您所说的相同查询应该有效...
小智 7
我尝试在这个示例中向您展示一个完整的示例来个性化您的 OrderBy 排序
import java.util.List;
import org.springframework.data.domain.Page;
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);
}
Run Code Online (Sandbox Code Playgroud)
您将使用以下示例:动态构建 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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
246630 次 |
| 最近记录: |