未解耦:
public class CustomerServiceImpl implements CustomerService {
private CustomerDao dao = new CustomerDaoMySqlImpl();//练习时要解耦
public void addCustomer(Customer customer) {
//设置id
customer.setId(IdGenerator.genId());
//存储
dao.addCustomer(customer);
}
public void deleteCustomer(String customerId) {
try {
dao.deleteCustomer(customerId);
} catch (IdIsNullException e) {
throw new RuntimeException("ID不能为空");
}
}
public List<Customer> findAll() {
return dao.findAll();
}
public Customer findById(String customerId) {
Customer c = null;
try {
c = dao.findById(customerId);
} catch (IdIsNullException e) {
throw new RuntimeException("ID不能为空");
}
return c;
}
public void updateCustomer(Customer customer) {
try {
dao.updateCustomer(customer);
} catch (IdIsNullException e) {
throw new RuntimeException("ID不能为空");
}
}
}