3

我已经看到了一百万个 DAO 示例,并且在大多数情况下,它们都为单个实体实现了基本的 CRUD 操作,可能还有一些返回列表的方法(例如 List getCustomers())。

但是,我从未见过有更新、删除或创建多个实体的方法的示例,例如:void update(List)。

更新多个实体的方法通常不是 DAO 的一部分,还是它们只是不经常在示例中使用?我有一个要求,我必须进行一些批量插入,并且调用 myDAO.create() 一百次并不是非常有效。

我只是想确保在继续做看起来很明显的事情之前我没有遗漏任何东西。

4

3 回答 3

2

I find that batch updates are usually done with tools provided by the database vendor.

I agree that the DAOs that I've seen usually don't have methods for create/update/delete overloaded to take List, but there's no reason why they can't.

The one thought that brings me up short is that DAOs don't own transactions when I write them. They can never know where they're part of a larger unit of work. That's what services do.

My advice would be to leave the DAOs alone and let a separate service layer own the batch operations. Services own the transaction logic. It's also a good place to include logic for "chunking" a large update into pieces. That lets you checkpoint the batch and keep the size of your rollback logs manageable.

于 2009-05-28T20:59:31.913 回答
0

void update(List) should act the same as void update(Item), only the List one would update multiple items. If not, it should be unless there are very specific reasons why not to. The overhead of calling a method is not a concern if they do the same thing.

For example, calling update(Item) 1000 times vs calling update(List) 1 and it does the same as update(Item) only a 1000 times means there will be very little performance difference.

于 2009-05-28T20:59:43.507 回答
0

我认为这些示例不经常使用它。我的 DAO 具有访问、创建和更新单个实体和记录集合的方法。

于 2009-05-28T20:58:01.700 回答