Java 如何在Spring中将OrderBy与findAll一起使用
我正在使用Spring数据,希望从数据库中获取按id排序的数据。当我编写findAllByOrderById时,出现了错误。我想错误代码是“找不到类型Employee的属性id!” 但我不知道去哪里修理。 存储库Java 如何在Spring中将OrderBy与findAll一起使用,java,spring,spring-boot,Java,Spring,Spring Boot,我正在使用Spring数据,希望从数据库中获取按id排序的数据。当我编写findAllByOrderById时,出现了错误。我想错误代码是“找不到类型Employee的属性id!” 但我不知道去哪里修理。 存储库 @Repository public interface EmployeeRepository extends JpaRepository<Employee, Long> { public List<Employee> findAllByOrderByI
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
public List<Employee> findAllByOrderById();
}
控制器
@Controller
public class DemoController {
@Autowired
EmployeeRepository empRepository;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String index(Model model) {
List<Employee> emplist = empRepository.getAllEmployeesOrderById();
model.addAttribute("emplist", emplist);
return "view/index";
}
}
您应该为该方法使用的命名可能是:
findAllByOrderByEmp_IdAsc()
或
根据你的需要
用于查询创建方法命名的完整文档
Java中也推荐使用camelCase作为字段命名
因此,例如,如果您的字段将命名为
empId
,那么您的方法将命名为findAllByOrderByEmpIdAsc()
对不起,我可以获取数据,但数据没有排序。这是另一个问题吗?是的,您可能必须在末尾添加“Asc”或“Desc”
<body>
<div id="wrapper">
<div id="header">
<h1>日報管理システム</h1>
</div>
<div id="parent" th:include="app"></div>
</div>
<h1 th:text="${title}"></h1>
<table>
<tr th:each="emp : ${emplist}" th:object="${emp}">
<td th:text="*{emp_id}"></td>
<td th:text="*{emp_name}"></td>
</tr>
</table>
<br>
</body>
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List spring.repositries.EmployeeRepository.findAllByOrderById()! No property id found for type Employee!
Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List spring.repositries.EmployeeRepository.findAllByOrderById()! No property id found for type Employee!
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property id found for type Employee!
findAllByOrderByEmp_IdAsc()
findAllByOrderByEmp_IdDesc()