員工類
@Entity
@Table(name = "employees")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "eid", unique = true, nullable = false)
private long eid;
@Column(name ="first_name")
private String firstName;
@Column(name ="last_name")
private String lastName;
@Column(name ="email_id")
private String emailId;
public Employee() {
}
public Employee(String firstName, String lastName, String emailId) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.emailId = emailId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
}
員工回購
@Repository
public interface EmployeeRepo extends JpaRepository<Employee, Long>{
}
控制器類
@CrossOrigin(origins = {"http://localhost:3000/"})
@RestController
@RequestMapping("/api/v1/")
public class EmployeeController {
@Autowired //Creates object internally on runtime
private EmployeeRepo employeeRepo;
//get all employee
@GetMapping("/employees")
public List<Employee> getAllEmployees(){
return employeeRepo.findAll();
}
}
應用程式屬性
spring.jpa.database-platform = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql = 真
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto = 更新
當我向/api/v1/employees發送獲取請求時,我得到的回應是
[ {“名字”:“aa”,“姓氏”:“bb”,“emailId”:“[email protected]”}]
我希望是 [ { "eid" : 1, "firstName": "aa", "lastName": "bb", "emailId": "[email protected]" } ]
hibernate 運行的查詢包括選擇查詢中的 eid,但我仍然沒有得到它的回應

uj5u.com熱心網友回復:
我相信您需要一個 Employee 類的 eid 吸氣劑。
將該方法添加到您的 Employee 類。
public long getEid() {
return this.eid;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/406473.html
標籤:
上一篇:使用gavlyukovskiy依賴項顯示查詢日志記錄時的資料源例外
下一篇:如何修復無法延遲初始化集合
