作为一名新的 Spring 开发人员,我正在尝试将实体与数据库映射
但是,我看不到“客户表”和“序列”id
@Entity
@Table(name = "customer")
public class Customer{
@Id
@SequenceGenerator(
name = "customer_id_sequence",
sequenceName = "customer_id_sequence"
)
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "customer_id_sequence"
)
private Integer id;
private String name;
private String email;
private Integer age;
public Customer(){
}
@Service
public class CustomerService {
private final CustomerDao customerDao;
public CustomerService(CustomerDao customerDao) {
this.customerDao = customerDao;
}
public List<Customer> getAllCustomers(){
return customerDao.selectAllCustomers();
}
public Customer getCustomer(Integer id){
return customerDao.selectCustomersById(id)
.orElseThrow(()->new ResourceNotFound(("customer with id [%s] not found".formatted(id))));
}
}
@RestController
public class CustomerController {
private final CustomerService customerService;
public CustomerController(CustomerService customerService) {
this.customerService = customerService;
}
@GetMapping("/api/v1/customers")
public List< Customer> getCustomers(){
return customerService.getAllCustomers();
}
@GetMapping("/api/v1/customers/{customerId}")
public Customer getCustomer(@PathVariable("customerId")Integer customerId){
return customerService.getCustomer(customerId);
}
}
@Repository
public class CustomerDataAccessService implements CustomerDao{
//数据库 私人最终静态列表客户;
Postgres
问题:
关于属性
jpa:
hibernate:
ddl-auto: create-drop
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
format_sql: true
show_sql: false
您需要在实体类中拥有 getter setter 才能访问您的私有字段。数据库中应该仍然显示一个空表。 如果您可以分享 hibernate 运行的 sql 查询的日志,它会提供更清晰的图片