您的位置:首页 > 其它

JPA排序查询new Sort() Intellij红线报错问题

2020-06-28 05:08 579 查看


原因

  • 在最新的spring-boot(spring-boot-data)版本中通过new 来创建Sort是不可以的
  • 查看源码,原来是Sort的构造器私有了private;所以不能通过new Sort()的方式来创建Sort对象

而是用 Sort.by()

Sort id = by(DESC,"id");//DESC降序,ASC升序;

直接写字段默认升序,Sort源码如下

示例

@GetMapping("/findAll/{username}")
public List<User> findAllByUsername(@PathVariable(value = "username") String username) {

User user = new User();
user.setUsername(username);

ExampleMatcher matching = ExampleMatcher.matching()
.withMatcher("username", ExampleMatcher.GenericPropertyMatcher::startsWith)
.withIgnorePaths("password");

Example<User> example = Example.of(user, matching);

Sort sort = by(DESC,"id");

return userRepository.findAll(example,sort);

}

结果 (localhost:8080/findAll/jpa)

[
{"id":8,"username":"jpa-2","password":"234"},
{"id":7,"username":"jpa-1","password":"123"},
{"id":6,"username":"jpa-dead","password":"123123123123"},
{"id":5,"username":"jpa-result","password":"12345"}
]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: