如何使用 Spring Boot 查找数据库中现有的对象 id?
该应用程序有两个功能,我可以创建新的脚本文件,也可以编辑现有的脚本文件。我用于创建和编辑的逻辑基本上是相同的,唯一的主要区别是,为了编辑而不是创建新脚本,我必须找到现有脚本并覆盖它。我不知道应该如何在 Spring Boot 中通过 id 查找脚本。
这是我的创建脚本功能:
控制器类:
@Controller
@RequestMapping("/api/auth/")
public class TestServerController {
@Autowired
ScriptsRepository scriptsRepository;
@Autowired
LineItemRepository lineItemRepository;
@Autowired
LineItemDataRepository lineItemDataRepository;
public TestServerController() {
}
@CrossOrigin(origins = "*")
@RequestMapping(value = "createScript")
public ResponseEntity<?> createScript(@RequestBody String body, @RequestParam String name) throws Exception {
JSONArray script = new JSONArray(body);
Scripts new_script = new Scripts(name);
scriptsRepository.save(new_script);
for (int i = 0;i<script.length();i++) {
JSONObject current_line_item = (JSONObject) script.get(i);
if(!current_line_item.has("keyword") ){
return ResponseEntity
.badRequest()
.body(new MessageResponse("Invalid Json Object"));
}
String keyword = current_line_item.getString("keyword");
LineItem new_li = new LineItem(keyword,i);
new_li.setSid(new_script);
lineItemRepository.save(new_li);
int order = 0;
var keys = current_line_item.keys();
for (Iterator<String> it = keys; it.hasNext(); ) {
var key = it.next();
if(key.equals("keyword")){
continue;
}
var value = current_line_item.getString(key);
// we will need to do something about this if the column values arent strings
LineItemData vb = new LineItemData(key,value,order);
vb.setLid(new_li);
lineItemDataRepository.save(vb);
order++;
}
}
return ResponseEntity.ok(new MessageResponse("Script Uploaded Successfully"));
}
对于编辑脚本,我想删除 Scripts new_script = new Scripts(name); 并使用 findbyId 在数据库中查找脚本并将其传递到 scriptRepository.save(new_script); 我只需要使用 id 的脚本文件,而不是 new_script。这对我不起作用并且我收到错误。还有其他方法可以解决这个问题吗?
The application has 2 functionalities where I can create a new script file and also edit the existing script file. The logic I have for both create and edit will basically be the same with the only major difference being that for edit instead of creating new scripts, I will have to find the existing script and override it. I am not sure how I should go finding the script by id in spring boot.
Here is my create script functionality:
Controller Class:
@Controller
@RequestMapping("/api/auth/")
public class TestServerController {
@Autowired
ScriptsRepository scriptsRepository;
@Autowired
LineItemRepository lineItemRepository;
@Autowired
LineItemDataRepository lineItemDataRepository;
public TestServerController() {
}
@CrossOrigin(origins = "*")
@RequestMapping(value = "createScript")
public ResponseEntity<?> createScript(@RequestBody String body, @RequestParam String name) throws Exception {
JSONArray script = new JSONArray(body);
Scripts new_script = new Scripts(name);
scriptsRepository.save(new_script);
for (int i = 0;i<script.length();i++) {
JSONObject current_line_item = (JSONObject) script.get(i);
if(!current_line_item.has("keyword") ){
return ResponseEntity
.badRequest()
.body(new MessageResponse("Invalid Json Object"));
}
String keyword = current_line_item.getString("keyword");
LineItem new_li = new LineItem(keyword,i);
new_li.setSid(new_script);
lineItemRepository.save(new_li);
int order = 0;
var keys = current_line_item.keys();
for (Iterator<String> it = keys; it.hasNext(); ) {
var key = it.next();
if(key.equals("keyword")){
continue;
}
var value = current_line_item.getString(key);
// we will need to do something about this if the column values arent strings
LineItemData vb = new LineItemData(key,value,order);
vb.setLid(new_li);
lineItemDataRepository.save(vb);
order++;
}
}
return ResponseEntity.ok(new MessageResponse("Script Uploaded Successfully"));
}
For the edit script, I was thinking to remove Scripts new_script = new Scripts(name);
and use the findbyId to find the script in the database and pass that in the scriptsRepository.save(new_script);
where instead of new_script, I would just have the script file using the id. This hasn't worked for me and I'm getting errors. Is there another way to go about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您使用scriptsRepository.save(script)时,它将把脚本保存在您的数据库中。但是,如果数据库中已经有一个脚本,其 ID 与您通过参数传递的脚本的 ID 相同,则它不会创建新脚本,而只是使用您尝试保存的脚本的信息来更新它。
遵循 REST API 设计的脚本控制器(仅使用更新方法)将是这样的:
要访问它,您需要使用端点 localhost:8080/scripts/{id} (在如果您使用默认的 Spring Boot localhost:8080)。
您不需要抛出异常,但如果您愿意,您需要创建一个像这样的简单类:
When you use scriptsRepository.save(script) it will save the script on your Database. But if you already have a script in the database with the same ID of the one you are passing through the parameter it will not create a new one, but just update it with the information of the one you are trying to save.
A Script controller (only with the update method), following REST API design, would be something like this:
To access it you would need to use the endpoint localhost:8080/scripts/{id} (in case you use the default Spring Boot localhost:8080).
You don't need to throw the exception but in case you want you would need to create a simple class like this: