2016-04-26 64 views
0

我是新來的postgresql數據庫和使用hibernate + spring.I想知道如何編寫查詢以檢索表中的所有行。我不知道如何編寫查詢DAOIMPL。我成功完成了創建服務。如何從postgresql數據庫中使用spring查找所有記錄

@Entity 
@Table(name = "STUDENT_RECORD") 
public class StudentRecord{ 

@Id 
@GeneratedValue 
@Column(name = "id") 
private long id; 


@Column(name = "student_Name") 
private String studentName; 

@Column(name = "student_Id") 
private String studentId; 

'''' 
getter setter methods.. 
.... 
.... 
} 

studentDaoImpl: 

@Repository 
@Transactional 
public class studentDaoImpl implements studentDao{ 
@Override 
public List<StudentRecord> studentRecord() { 
    List<StudentRecord> entities = null; 
    StringBuilder sql = new StringBuilder(); 
    sql.append("SELECT e.* STUDENT_RECORD e"); 
    entities = sql.list(); 
    return entities; 

} 
} 

我的studentName表有5條記錄,同時也完成了Get方法的控制器。請幫幫我。

+0

請與添加代碼一個查詢(你的嘗試)。 –

回答

0

好吧,你已經有了你的實體映射。

接下來要做的是寫一個服務來檢索它。

假設您使用註釋正確配置了彈簧。

定義StudentRecordRepository這樣的:

@Repository 
public interface StudentRecordRepository extends JpaRepository<StudentRecord, Long> { 

} 

注意!您的存儲庫擴展了JpaRepository。在JpaRepository中定義了一些基本方法,允許您在StudentRecord實體上執行基本的CRUD操作。

在你的控制器類你注入這樣的倉庫:

@Controller 
public class StudentRecordController { 

    @Autowired 
    private StudentRecordRepository studentRecordRepository; 


} 

後注入倉庫到你的控制器,你可以找到所有StudentRecords這樣的:

studentRecordRepository.findAll(); 
相關問題