java关于DAOs的正确使用
DAO是否仅为基本CRUD操作保留,或者它还可能包含一些复杂的搜索逻辑
想象一下Book和Author这两个实体类以及它们之间的多对多关系。此时,一个DAO就足够了,因为两个实体的基本CRUD操作是相同的
然而,随着应用程序变得越来越复杂,出现了新的需求,例如: 1.为给定作者查找第一本书 2.查找作者在一段时间内所写的书籍 3.查找特定作者所写的关于特定主题的书籍 4.等
这些都应该去哪里?我现在是否为每个实体创建一个单独的DAO类,并将它们放在那里?它们是否属于DAO层
我面临着这样的困境: 以下方法应用于何处: findBooks(作者) findBooks(出版商) 都在书道里,还是一个在作者道里,另一个在出版道里
或者我应该考虑一个全新的抽象,它位于DAO之上,并将它们留给基本的CRUD操作——例如一个SearchService,它列出了所有可能的搜索
# 1 楼答案
这些都应该去哪里?我现在是否为每个实体创建一个单独的DAO类,并将它们放在那里?它们到底属于道层吗
我有一个难题:以下方法应该去哪里:findBooks(Author-Author)findBooks(Publisher-Publisher)在BookDAO中,或者一个在AuthorDAO中,另一个在Publisher-dao中
或者我应该考虑一个全新的抽象概念,它位于DAO之上,并将它们留给基本的CRUD操作——例如一个SearchService,它列出了所有可能的搜索
# 2 楼答案
不。请随意将所有数据访问逻辑放在DAOs中
不要把DAO和DAL混淆起来:
# 3 楼答案
肯定是DAO(层)的工作和要求。你所拥有的一切都无关紧要。具体的方法应该去它所属的道
这里有两件事