我正在使用 CustomerDTO:
public class CustomerDTO {
private int customerId;
private String customerName;
private String customerAddress;
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getCustomerAddress() {
return customerAddress;
}
public void setCustomerAddress(String customerAddress) {
this.customerAddress = customerAddress;
}
}
然后我使用 CustomerDAO 我根据 customerId 获取客户
public class CustomerDAO {
private CustomerDTO customer;
public void setCustomer(CustomerDTO customer) {
this.customer = customer;
}
public CustomerDTO getCustomer(int customerId){
try{
if("you get customer based on customer id") then "return the customer";
}catch(Exception ex){
return new CustomerDTO();
//some other work
}
}
}
现在,当我尝试访问时,CustomerDAO 中的这段代码new CustomerDTO();
会给出。我正在考虑使用默认值初始化该字段。null
new CustomerDTO().getCustomerName()
使用/不使用 Spring 框架如何实现这一点?