Posted in

使用 Java、React 和 Spring Boot 进行全栈开发,第二部分_AI阅读总结 — 包阅AI

包阅导读总结

1.

关键词:Full-stack、Java、Spring Boot、React、TodoService

2.

总结:文本主要介绍了使用 Java、React 和 Spring Boot 进行全栈开发中 `TodoService` 类的相关内容,包括注解、依赖注入以及其中的 CRUD 操作方法。

3.

主要内容:

– `TodoService` 类使用 `@Service` 注解表明其为服务类。

– 通过 `@Autowired` 引入 `TodoRepository` 。

– 类中的方法:

– `getTodos` 方法通过 `todoRepository.findAll` 获取所有待办事项。

– `createTodo` 方法创建并保存新的待办事项。

– `getTodo` 根据 ID 获取待办事项。

– `deleteTodo` 根据 ID 删除待办事项。

– `saveTodo` 保存待办事项。

– 提及 Spring 默认使用单例注入,该注入方式适用于当前情况。

– 每个方法都通过 `TodoRepository` 执行对应的 CRUD 操作。

思维导图:

文章地址:https://www.infoworld.com/article/3478086/full-stack-development-with-java-react-and-spring-boot-part-2.html

文章来源:infoworld.com

作者:InfoWorld

发布时间:2024/7/31 9:00

语言:英文

总字数:1465字

预计阅读时间:6分钟

评分:88分

标签:全栈开发,Java,React,Spring Boot,MongoDB


以下为原文内容

本内容来源于用户推荐转载,旨在分享知识与观点,如有侵权请联系删除 联系邮箱 media@ilingban.com

// src/main/java/com/example/iwreactspring/service/TodoService.java package com.example.iwreactspring.service;import java.util.List;import java.util.ArrayList;import com.example.iwreactspring.model.TodoItem;import org.springframework.stereotype.Service;import org.springframework.beans.factory.annotation.Autowired;import com.mongodb.client.MongoClient;import com.mongodb.client.MongoClients;import com.mongodb.client.MongoCollection;import com.mongodb.client.MongoDatabase;import org.bson.codecs.configuration.CodecRegistry;import org.bson.codecs.pojo.PojoCodecProvider;import org.bson.Document;import com.example.iwreactspring.repository.TodoRepository;@Servicepublic class TodoService {  @Autowired  private TodoRepository todoRepository;  public List<TodoItem> getTodos() {    return todoRepository.findAll();  }  public TodoItem createTodo(TodoItem newTodo) {    TodoItem savedTodo = todoRepository.save(newTodo);    return savedTodo;  }  public TodoItem getTodo(String id) {    return todoRepository.findById(id).orElse(null);  }  public boolean deleteTodo(String id) {    TodoItem todoToDelete = getTodo(id);    if (todoToDelete != null) {      todoRepository.deleteById(id);      return true;    } else {      return false;    }  }  public TodoItem saveTodo(TodoItem todoItem) {    TodoItem savedTodo = todoRepository.save(todoItem);    return savedTodo;  }}

We annotate this class with @Service to denote it as a service class. Again, this is not strictly required, because Spring can use the class as an injected bean without the annotation, but annotating the class makes things more descriptive. Next, we use @AutoWired to bring the TodoRepository class in. This will be populated by Spring based on the class type, which is the com.example.iwreactspring.repository.TodoRepository we saw earlier.

By default, Spring uses singleton injection (one instance of the injected bean class), which works well for us.

CRUD operations on the service class

Each method on this class is dedicated to performing one CRUD operation using the repository. For example, we need a way to get all the to-dos in the database, and getTodos() does that for us. The Repository class makes it very easy, too: return todoRepository.findAll() returns all the records (aka documents) in the todo collection (aka, database).