我对 Hibernate、Spring 和 postgres 数据库世界完全陌生。用于描述的“术语”可能有所不同。
所以问题是,我想在 spring boot 中使用 hibernate 、JPA 注释获取匹配给定“id”和“string”的数据。
我知道像 repository.findOne(id)
这样的基本知识,但我不知道如何使用两个参数获取数据。
我想我需要类似的东西,我不确定,因为我是 db world 的新手
SELECT *
FROM student
WHERE type='commerce'
AND id='1'"
在此先感谢您,欢迎提供进一步学习的教程。
最佳答案
Spring Boot 为您提供了一些自动存储库,因此您只需创建一个扩展“JpaRepository”的接口(interface),然后使用自然语言创建方法。
类似于:
@Repository
public interface StudentRepository extends JpaRepository<Student, Long> {
public List<Student> findByIdAndType(Long id, String type);
}
稍后,假设您想使用这个类:
@Autowired
private StudentRepository studentRepository;
public void doSomething() {
List<Student> students = studentRepository.findByIdAndType(1, "commerce");
}
不,不需要为接口(interface)“StudentRepository”提供实现,因为 spring data 会在幕后提供它。
有关其工作原理的更多信息,您可以在适当的 spring-data documentarion 上找到
干杯,尼古拉斯
关于java - 从单个数据库中通过 id AND String 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40748376/