[英]What is a proper way to modify a list of DB Objects using Spring MVC?
Using the spring MVC model and without any ORM solution, how am I supposed to treat a large list of database objects without multiplying the database queries? 使用spring MVC模型并且没有任何ORM解决方案,我应该如何在不增加数据库查询的情况下处理大量的数据库对象?
SELECT
the objects with the DAO and put them in a (big) List
with a RowMapper
. SELECT
对象,然后使用RowMapper
将它们放入(大) List
。 List
to do n simple UPDATE
in the DB List
上循环以在数据库中执行n个简单的UPDATE
Queries for n objects: 1 + n 对n个对象的查询: 1 + n
Write a SQL query in the DAO that directly updates all the corresponding objects without extracting them. 在DAO中编写一个SQL查询,该查询直接更新所有相应的对象而无需提取它们。
Queries for n objects: 1 对n个对象的查询: 1
As track 2 seems far more efficient, it also seems to straightly go against the Spring MVC model as my services will tend to empty and my DAO will tend to expand. 由于第2轨的效率似乎更高,它也似乎与Spring MVC模型完全相反,因为我的服务将趋于空白,而DAO则会趋于扩展。
Maybe there is a generic way to do an UPDATE
on a Java List
of objects in a database without multiplying the queries? 也许有一种通用的方法可以在不增加查询的情况下对数据库中的Java对象
List
执行UPDATE
?
Given this table: 给定此表:
+----+---------+------+
| MY_OBJECT |
+----+---------+------+
| ID | BOOLEAN | DATE |
+----+---------+------+
| 0 | 0 | NULL |
| 1 | 1 | NULL |
| 2 | 0 | NULL |
| 3 | 1 | NULL |
| 4 | 1 | NULL |
+----+---------+------+
I have a MyObjectDao
and a MyObjectService
. 我有一个
MyObjectDao
和MyObjectService
。
I'd like to set DATE
to the SYSDATE()
for all objects that have the boolean to true
. 对于所有将boolean设置为
true
对象,我想将DATE
设置为SYSDATE()
。
Should it be full DAO with a query or should the DAO retrieve the objects, the service edit them and then the DAO update them? 应该是带有查询的完整DAO,还是DAO检索对象,服务应对其进行编辑,然后DAO更新它们?
This is something that would typically be handled by the ORM layer. 这通常由ORM层处理。
If you are using Hibernate (other ORMs are available) then I would delegate to that. 如果您使用的是Hibernate(其他ORM可用),那么我将委托给它。
If you have your own home spun ORM layer you can do whatever you want, but as weight to your arguments I would say: 如果您有自己的家庭旋转ORM层,则可以执行任何您想做的事情,但是作为对您的论据的重视,我会说:
I did some researches and put a lot of thoughts into this question. 我做了一些研究,并对这个问题进行了很多思考。 What I deduced from my work is that in the majority of cases the process should be:
从我的工作中得出的结论是,在大多数情况下,该过程应为:
The key element I was missing is the batchUpdate . 我缺少的关键元素是batchUpdate 。 Without it, updating a big
List
of elements would take minutes which is unacceptable. 没有它,更新大的元素
List
将花费几分钟,这是不可接受的。 The big difference is not the amount of queries executed but how they are transmitted to the DB, avoiding multiple usages of the network layer in the batch update case. 最大的区别不是执行的查询数量,而是查询如何传输到数据库,从而避免了在批量更新情况下多次使用网络层。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.