拥有几个DAO很好吗? [英] Is it fine to have several DAOs?

查看:90
本文介绍了拥有几个DAO很好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含两个表的数据库:companiesemployees. 我应该创建companyDAOemployeeDAO,如果我选择,更新,添加和删除这两个表?

Let's say I have one database with two tables: companies and employees. Should I create companyDAO and employeeDAO If I select, update, add and remove from those two tables?

还是我应该创建一个DAO,为数据库中的每个表编写CRUD操作的方法?

Or I should create one DAO in which I'll write methods for CRUD operations for every table in the database?

推荐答案

是的,对于大多数实体,使用单独的DAO是有意义的.

Yes, it makes sense to have separate DAOs for most of the entities.

通常您会使用通用的DAO,即您有一个抽象超类.在一个简化的示例中,它可能看起来像这样:

Often times you'll work with generified DAOs, i.e. you have an abstract super class. In a simplified example, it could look like this:

class BaseDAO<T extends Entity>

提供了许多有用的方法来查找,删除或更新实体.为每个实体定义该BaseDAO的子类时,例如

that provide a bunch of useful methods to find, remove or update the entities. When defining a subclass of that BaseDAO for each Entity, e.g.

class EmployeeDAO extends BaseDAO<Employee> 

请确保您所有的DAO返回的类型正确,而这些类型仅在 中必须在BaseDAO中实现.因此,EmployeeDAO中的findAll()方法将返回List<Employee>.

you make sure that all your DAOs return the correct type for the methods which you only have to implement in your BaseDAO. So, the findAll() method in your EmployeeDAO will return List<Employee>.

此外,您可以为某些实体实现用于自定义操作的方法,例如findEmployeesWithSalaryAbove(long)在相应的类中.您不希望这些杂乱无章的DAO.

Moreover you can implement methods for customized operations for certain entities, e.g. findEmployeesWithSalaryAbove(long) in the respective class. You don't want these to clutter a single DAO.

这篇关于拥有几个DAO很好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆