DAO 实现:使一个 DAO 对象成为其他 DAO 的 属性
DAO implementation : Making a DAO object a property of other DAO
如何使一个 DAO 对象成为其他 DAO 的 属性?
假设我有一个带有部门的 Employee 对象 属性
public class Employee {
public Department;
//setter and getters
}
我有这个 EmployeeDAO 和 DepartmentDAO 接口以及相应的实现
我有 DAOFactory
public abstract class DAOFactory {
// db connection instantiation here
public IEmployeeDAO getEmployeeDAO() {
return new EmployeeDAOImpl(this);
}
public IDepartmentDAO getDepartmentDAO() {
return new DepartmentDAOImpl(this);
}
}
我有一个 servlet 实例化这个 DAOfactory
public class EmployeeController extends HttpServlet {
public EmployeeController() {
super();
DBUtils dbInstance = DBUtils.getInstance("mysql");
System.out.println("DAOFactory successfully obtained: " + dbInstance);
// Obtain UserDAO.
employeeDAO = dbInstance.getEmployeeDAO();
departmentDAO = dbInstance.getDepartmentDAO();
jobDAO = dbInstance.getJobDAO();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
employees = employeeDAO.findAll();
request.setAttribute("employees", employees);
}
我的问题是,当我调用 employeeDAO 的 findAll 方法时,如何将 Department 对象映射到 employeeDAO 或其实现中?
我在尝试映射结果时遇到了这样的事情:
private Employee map(ResultSet rs) throws SQLException {
Employee employee = new Employee();
employee.setEmployeeID(rs.getInt("EMPLOYEE_ID"));
employee.setFirstName(rs.getString("FIRST_NAME"));
employee.setLastName(rs.getString("LAST_NAME"));
Department department = new DepartmentDAOImpl().getDepartmentByID(rs
.getInt("DEPARTMENT_ID"));
employee.setDepartment(department);
return employee;
}
但我认为这是一种错误的做法。有人可以帮我解决这个问题吗?
您 EmployeeDAOImpl
依赖 IDepartmentDAO
。不要直接实例化一个,而是将其声明为依赖项,然后让构造 EmployeeDAOImpl
的代码找出如何解决它。
假设
interface IEmployeeDAO {
Employee load(long id);
}
interface IDepartmentDAO {
Department load(long id);
}
因为接口需要在构造函数中使用所需的 dao
class EmployeeDAOImpl implements IEmployeeDAO {
private final DAOFactory factory;
private final IDepartmentDAO departmentDAO;
public EmployeeDAOImpl(DAOFactory factory, IDepartmentDAO departmentDAO) {
this.factory = factory;
this.departmentDAO = departmentDAO;
}
...
现在您可以在任何地方使用它。例如
@Override
public Employee load(long id) {
...
long departmentId = ....
Department department = departmentDAO.load(departmentId);
employee.department = department;
return employee;
}
你的 DAOFactory
知道你使用哪种实现,现在可以通过添加一个简单的参数来提供依赖关系
public IEmployeeDAO getEmployeeDAO() {
return new EmployeeDAOImpl(this, getDepartmentDAO());
}
如何使一个 DAO 对象成为其他 DAO 的 属性?
假设我有一个带有部门的 Employee 对象 属性
public class Employee {
public Department;
//setter and getters
}
我有这个 EmployeeDAO 和 DepartmentDAO 接口以及相应的实现
我有 DAOFactory
public abstract class DAOFactory {
// db connection instantiation here
public IEmployeeDAO getEmployeeDAO() {
return new EmployeeDAOImpl(this);
}
public IDepartmentDAO getDepartmentDAO() {
return new DepartmentDAOImpl(this);
}
}
我有一个 servlet 实例化这个 DAOfactory
public class EmployeeController extends HttpServlet {
public EmployeeController() {
super();
DBUtils dbInstance = DBUtils.getInstance("mysql");
System.out.println("DAOFactory successfully obtained: " + dbInstance);
// Obtain UserDAO.
employeeDAO = dbInstance.getEmployeeDAO();
departmentDAO = dbInstance.getDepartmentDAO();
jobDAO = dbInstance.getJobDAO();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
employees = employeeDAO.findAll();
request.setAttribute("employees", employees);
}
我的问题是,当我调用 employeeDAO 的 findAll 方法时,如何将 Department 对象映射到 employeeDAO 或其实现中?
我在尝试映射结果时遇到了这样的事情:
private Employee map(ResultSet rs) throws SQLException {
Employee employee = new Employee();
employee.setEmployeeID(rs.getInt("EMPLOYEE_ID"));
employee.setFirstName(rs.getString("FIRST_NAME"));
employee.setLastName(rs.getString("LAST_NAME"));
Department department = new DepartmentDAOImpl().getDepartmentByID(rs
.getInt("DEPARTMENT_ID"));
employee.setDepartment(department);
return employee;
}
但我认为这是一种错误的做法。有人可以帮我解决这个问题吗?
您 EmployeeDAOImpl
依赖 IDepartmentDAO
。不要直接实例化一个,而是将其声明为依赖项,然后让构造 EmployeeDAOImpl
的代码找出如何解决它。
假设
interface IEmployeeDAO {
Employee load(long id);
}
interface IDepartmentDAO {
Department load(long id);
}
因为接口需要在构造函数中使用所需的 dao
class EmployeeDAOImpl implements IEmployeeDAO {
private final DAOFactory factory;
private final IDepartmentDAO departmentDAO;
public EmployeeDAOImpl(DAOFactory factory, IDepartmentDAO departmentDAO) {
this.factory = factory;
this.departmentDAO = departmentDAO;
}
...
现在您可以在任何地方使用它。例如
@Override
public Employee load(long id) {
...
long departmentId = ....
Department department = departmentDAO.load(departmentId);
employee.department = department;
return employee;
}
你的 DAOFactory
知道你使用哪种实现,现在可以通过添加一个简单的参数来提供依赖关系
public IEmployeeDAO getEmployeeDAO() {
return new EmployeeDAOImpl(this, getDepartmentDAO());
}