Java 从单个数据库中按id和字符串获取数据
我对Hibernate、Spring和postgres数据库世界一无所知。用于描述的“术语”可能有所不同 所以问题是,我想在SpringBoot中使用hibernate、JPA注释获取一个与给定的“id”和“字符串”匹配的数据 我知道像Java 从单个数据库中按id和字符串获取数据,java,spring,postgresql,hibernate,spring-data-jpa,Java,Spring,Postgresql,Hibernate,Spring Data Jpa,我对Hibernate、Spring和postgres数据库世界一无所知。用于描述的“术语”可能有所不同 所以问题是,我想在SpringBoot中使用hibernate、JPA注释获取一个与给定的“id”和“字符串”匹配的数据 我知道像repository.findOne(id)这样的basic,但我不知道如何使用两个参数获取数据 我想我需要一些东西,比如,我不确定,因为我是db世界的新手 SELECT * FROM student WHERE type='commerce'
repository.findOne(id)
这样的basic,但我不知道如何使用两个参数获取数据
我想我需要一些东西,比如,我不确定,因为我是db世界的新手
SELECT *
FROM student
WHERE type='commerce'
AND id='1'"
我提前向您表示感谢,欢迎您进一步学习教程。Spring boot为您提供了一些自动存储库,因此您只需创建一个扩展“JpaRepository”的界面,然后使用自然语言创建方法 比如:
@Repository
public interface StudentRepository extends JpaRepository<Student, Long> {
public List<Student> findByIdAndType(Long id, String type);
}
@存储库
公共界面StudentRepository扩展了JpaRepository{
公共列表findByIdAndType(长id,字符串类型);
}
稍后,假设您想使用这个类:
@Autowired
private StudentRepository studentRepository;
public void doSomething() {
List<Student> students = studentRepository.findByIdAndType(1, "commerce");
}
@Autowired
私立学校;私立学校;
公共无效剂量测定法(){
列出学生=studentRepository.findByIdAndType(1,“商业”);
}
不,不需要为接口“StudentRepository”提供实现,因为spring数据将在幕后为您提供它
有关如何工作的更多信息,您可以在适当的
干杯,NikolasSpring boot为您提供了一些自动存储库,因此您只需创建一个扩展“JpaRepository”的接口,然后使用自然语言创建方法 比如:
@Repository
public interface StudentRepository extends JpaRepository<Student, Long> {
public List<Student> findByIdAndType(Long id, String type);
}
@存储库
公共界面StudentRepository扩展了JpaRepository{
公共列表findByIdAndType(长id,字符串类型);
}
稍后,假设您想使用这个类:
@Autowired
private StudentRepository studentRepository;
public void doSomething() {
List<Student> students = studentRepository.findByIdAndType(1, "commerce");
}
@Autowired
私立学校;私立学校;
公共无效剂量测定法(){
列出学生=studentRepository.findByIdAndType(1,“商业”);
}
不,不需要为接口“StudentRepository”提供实现,因为spring数据将在幕后为您提供它
有关如何工作的更多信息,您可以在适当的
干杯,尼古拉斯