春季启动:使用单个调用获取多个标识的数据库数据

huangapple go评论73阅读模式
英文:

Spring boot fetch database data for multiple IDs using single call

问题

在Spring Boot项目中,我有以下代码,其中在循环内部发生了多次数据库调用,这会导致性能下降,这是一个不良的实践。

userList.forEach(user -> {
     List<Project> userProjectsList = ProjectRepository.findByUser_UserIdOrderByDatecompletedDesc(user.getUserId());
}

注意: userList 是从存储过程返回的列表。
Project 是一个实体。与该实体相关的存储库是 ProjectRepository
User 也是一个实体。
ProjectUser 实体之间有 多对一 的关系。

Project 实体如下所示:

@Entity
@Table(name = "Project")
public class Project {
  @Id
  @GeneratedValue
  private Integer projectid;
  private Calendar datecompleted;
  @ManyToOne
  @JoinColumn(name = "userid", referencedColumnName = "userid")
  private User user;

// getters and setters
}

我需要修改这段代码以提高效率。作为解决方案,我计划从数据库中一次性获取所有用户所属的所有项目。(使用 MySQL 数据库)

我尝试了以下代码,但似乎不正确。

List<Integer> userIdList = UserList.stream().map(User::getUserId).collect(Collectors.toList());
List<List<Project>> userProjectsList = ProjectRepository.findByUser_UserIdInOrderByDatecompletedDesc(userIdList);

以下方法位于 ProjectRepository 中:

List<List<Project>> findByUser_UserIdInOrderByDatecompletedDesc(List<Integer> userIdList);

请帮助我找到更好的解决方案。提前感谢!

更新:

上述代码未返回正确的结果。我传递了两个用户ID。其中一个用户有2个项目,另一个用户有1个项目。
预期结果是:

[0]->[0]:project1, [1]:project2
[1]->[0]:project3

但实际返回的结果是:

[0]->[0]:project1
[1]->[0]:project2
[2]->[0]:project3
英文:

In spring boot project I have following code which cause low performance as several database calls happen inside a loop which is a bad practice.

userList.forEach(user -&gt; {
     List&lt;Project&gt; userProjectsList = ProjectRepository.findByUser_UserIdOrderByDatecompletedDesc(user.getUserId());
}

Note: userList is a List returned from a stored procedure.
Project is an entity. Relevant repository for that entity is ProjectRepository.
User is also an entity.
Project and User entity has many to one relationship.

Project Entity is like below;

@Entity
@Table(name = &quot;Project&quot;)
public class Project {
  @Id
  @GeneratedValue
  private Integer projectid;
  private Calendar datecompleted;
  @ManyToOne
  @JoinColumn(name = &quot;userid&quot;, referencedColumnName = &quot;userid&quot;)
  private User user;

// getters and setters
}

I need to modify this code to be more efficient. As a solution I'm planning to fetch all projects belongs to all users at once from database. (Mysql database is using)

I tried with below code but seems it's not correct.

List&lt;Integer&gt; userIdList = UserList.stream().map(User::getUserId).collect(Collectors.toList());
List&lt;List&lt;Project&gt;&gt; userProjectsList = ProjectRepository.findByUser_UserIdInOrderByDatecompletedDesc(userIdList);

Following method is in ProjectRepository.

List&lt;List&lt;Project&gt;&gt; findByUser_UserIdInOrderByDatecompletedDesc(List&lt;Integer&gt; userIdList);

Kindly support me to find a better solution for this. Thanks in advance

Update:

Above code is not returning the correct result. I passed 2 userIds. Out of them one user has 2 projects and the other user has 1 project.
Expected result is:

[0]-&gt;[0]:project1, [1]:project2
[1]-&gt;[0]:project3

But, returned result is:

[0]-&gt;[0]:project1
[1]-&gt;[0]:project2
[2]-&gt;[0]:project3

答案1

得分: 1

你可以使用 IN 查询来实现这个。你需要将方法签名声明如下:

findByNamesIn(Collection<String> names);

更多细节和其他查询,请参考 https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation

英文:

You can use IN query for this. You need to declare you method signature as below:

findByNamesIn(Collection&lt;String&gt; names);

For more details and other queries, refer https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation

huangapple
  • 本文由 发表于 2020年9月8日 18:04:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63791620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定