简体   繁体   English

使用Spring MVC修改数据库对象列表的正确方法是什么?

[英]What is a proper way to modify a list of DB Objects using Spring MVC?

Question

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解决方案,我应该如何在不增加数据库查询的情况下处理大量的数据库对象?


Research tracks 研究方向

Track 1 音轨1

  • Step 1: SELECT the objects with the DAO and put them in a (big) List with a RowMapper . 步骤1:使用DAO SELECT对象,然后使用RowMapper将它们放入(大) List
  • Step 2: deal with my objects within the associated service in Java. 步骤2:在Java相关服务中处理我的对象。
  • step 3: loop on the (n-big) List to do n simple UPDATE in the DB 步骤3:在(n-big) List上循环以在数据库中执行n个简单的UPDATE

Queries for n objects: 1 + n 对n个对象的查询: 1 + n

Track 2 音轨2

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

Thoughts 思想

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


Simple case 简单的情况

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 . 我有一个MyObjectDaoMyObjectService

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层,则可以执行任何您想做的事情,但是作为对您的论据的重视,我会说:

  1. The service should only know the pagination it wants. 服务应该只知道它想要的分页。
  2. How this is implemented should sit below the service. 如何执行此操作应位于服务下方。

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: 从我的工作中得出的结论是,在大多数情况下,该过程应为:

  • Pull the data from the DB with the DAO in a generic way 使用DAO以通用方式从数据库中提取数据
  • Edit the objects the way you need it with the service / business layer 通过服务/业务层按需要编辑对象
  • Transmit the objects to the DAO to do a generic batchUpdate 将对象传输到DAO以进行一般的batchUpdate

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.

相关问题 使用Spring MVC流媒体的正确方法是什么 - What is proper way to stream media with Spring MVC 定义一个bean来保存在Spring中从地图创建的对象列表的正确方法是什么 - What is the proper way to define a bean that holds a list of objects created from a map in Spring 使用地图修改对象列表的有效方法 - Efficient way to modify a list of objects using maps 在 spring 集成中将 XML 转换为 Java 对象的正确方法是什么? - What is the proper way to convert XML to Java objects in spring integration? 使用Spring MVC和Ajax处理对象列表 - using Spring MVC and ajax to handle list of objects Spring MVC @ResponseBody返回列表不适合使用hibernate的json响应 - Spring MVC @ResponseBody return list is not proper json reponse using hibernate 使用另一个列表中的值修改列表中对象的最佳方法 - Best way to modify objects in list using values from another list 在Spring MVC中打开事务并与数据库建立连接的最佳方法是什么 - what is the best way to open transaction and have connections with DB in Spring mvc 使用Guice在方法内部创建对象的正确方法是什么 - What is proper way to create objects inside method using Guice 使用jackson对一系列空对象进行反序列化的正确方法是什么? - What is the proper way to deserialize an array of of empty objects using jackson?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM