简体   繁体   English

MySQL和Spring Boot

[英]Mysql and spring boot

I have a project am working on.it contains four java files including two repositories and a task file.I use the first repository to get user ids and forum ids from a mysql data table with the help of a query and the second repository to check if the user ids and forum ids are already included in a second mySQL database table also using a query . 我正在处理一个项目,它包含四个Java文件,包括两个存储库和一个任务文件。我使用第一个存储库在查询的帮助下从mysql数据表中获取用户ID和论坛ID,第二个存储库用于检查如果还使用查询在第二个mySQL数据库表中已经包含用户ID和论坛ID。 What I don't know is how to insert the user id s and forum ids to the second table if its not already there,using the task.java file. 我不知道的是如何使用task.java文件将用户ID和论坛ID插入第二张表(如果尚未插入)。 Below are the query's in the repositories. 下面是存储库中的查询。

//1st repository
@query("select t from UserTestSummary t where forumid is not null")
List<UserTestSummary>FindbyId();

//2nd repository
@query("select f from followedforum f")
List<FollowedForum>findbyid();

For sure you could do this with a query in a repository using @Query annotation. 当然,您可以使用@Query批注对存储库中的查询进行此操作。

But I personally would implement a service, that uses the repositories, like this : 但是我个人将实现一个使用存储库的服务,如下所示:

  1. create a service 'SecondService' 创建服务“ SecondService”
  2. There create a method SecondService.insertIfNotExist(Long userId,Long forumId), there (pseudo code): 在那里创建方法SecondService.insertIfNotExist(Long userId,Long forumId),那里(伪代码):

    if(!userRepository.exists(userId)&&!forumRepository.exists(forumId){ secondEntity=new SecondEntity(...) secondRepository.save(secondEntity) } if(!userRepository.exists(userId)&&!forumRepository.exists(forumId){secondEntity = new SecondEntity(...)secondRepository.save(secondEntity)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM