2016-11-22 83 views
0

我對Hibernate,Spring和Postgres數據庫世界是全新的。用「術語」來描述可能存在差異。從單個數據庫獲取數據的ID和字符串

所以問題是,我想在spring引導中使用hibernate,JPA註釋給出「id」和「string」匹配的數據。

我知道基本像repository.findOne(id),但我不知道如何獲取具有兩個參數的數據。

我想我需要這樣的東西,我不知道,因爲我是新來的DB世界

SELECT * 
    FROM student 
    WHERE type='commerce' 
    AND id='1'" 

我感謝你在和指南,以進一步研究是最歡迎的。

回答

4

Spring引導爲您提供了一些自動存儲庫,因此您只需創建一個擴展「JpaRepository」的接口,然後使用自然語言創建方法。

喜歡的東西:

@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"); 
} 

沒有,也沒有必要提供給接口「StudentRepository」的實施,因爲彈簧數據會在幕後提供給你。

這是如何工作的

的更多信息,你可以找到合適的spring-data documentarion

歡呼聲中,尼古拉斯·

相關問題