标题:Spring Boot应用问题:【简单描述问题】 描述: 我正在使用 JPA 存储库开发 Spring Boot 应用程序,但遇到了问题。我的应用程序启动时没有错误,但我面临[描述具体问题,例如“从数据库检索任务时出现问题”或“意外异常”]。相关代码和配置如下:
代码: Spring Boot 应用:
package com.example.ToDoApplication;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@EnableJpaRepositories(basePackages = "com.example.repository")
public class ToDoApplication {
public static void main(String[] args) {
SpringApplication.run(ToDoApplication.class, args);
}
}
实体类:
package com.example.entity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class Tasks {
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Id
private Long id;
private String title;
private String description;
@Column(name = "is_completed")
private boolean completed;
// Getters and Setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public boolean isCompleted() {
return completed;
}
public void setCompleted(boolean completed) {
this.completed = completed;
}
}
控制器
package com.example.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.entity.Tasks;
import com.example.service.TaskService;
@RestController
@RequestMapping("/tasks")
public class TaskController {
@Autowired
private TaskService taskService;
@GetMapping("/hello")
public List<Tasks> getAllTasks() {
return taskService.getAllTasks();
}
@GetMapping("/{id}")
public Tasks getTaskById(@PathVariable Long id) {
return taskService.getById(id).orElse(null);
}
@PostMapping
public Tasks createTask(@RequestBody Tasks task) {
return taskService.saveTask(task);
}
@DeleteMapping("/{id}")
public String deleteById(@PathVariable Long id) {
taskService.deleteTask(id);
return "Deleted Successfully";
}
}
服务:
package com.example.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.entity.Tasks;
import com.example.repository.TaskRepository;
@Service
public class TaskService {
private static final Logger logger = LoggerFactory.getLogger(TaskService.class);
@Autowired
private TaskRepository taskRepository;
public List<Tasks> getAllTasks() {
logger.info("Fetching all tasks from the database...");
List<Tasks> tasks = taskRepository.findAll();
logger.info("Number of tasks retrieved: " + tasks.size());
return tasks;
}
public Optional<Tasks> getById(Long id) {
return taskRepository.findById(id);
}
public Tasks saveTask(Tasks task) {
return taskRepository.save(task);
}
public void deleteTask(Long id) {
taskRepository.deleteById(id);
}
}
存储库
package com.example.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.entity.Tasks;
@Repository
public interface TaskRepository extends JpaRepository<Tasks, Long> {
}
应用程序属性:
spring.datasource.url=jdbc:mysql://localhost:3306/demo_task
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root123
spring.jpa.hibernate.ddl-auto=update
server.port=8091
ToDoApplication 类中的包声明(第一行)是错误的。
应该是
包com.example;
而不是
包 com.example.ToDoApplication;
你不需要@EnableJpaRepositories注解,除非你有不同的Repositories/Repository配置集(这意味着同一应用程序中有不同的数据库),你不需要指定。
默认情况下,如果 Spring Boot 自动配置在类路径接口上检测到 Spring Data JPA,并且扩展 @JpaRepository 或 @CrudRepository 的 @Repository 被视为 JPA 存储库。