完整的项目可以在我的 GitHub 上找到:https://github.com/plasmastorm36/ToDoList/
最大的问题是我的应用程序找不到这个神秘的entityManagerFactory。
我尝试更改 application.properties 并更改 pom.xml 文件,看看是否可以修复任何问题。我也尝试过使用自动装配。
每次我尝试使用 Maven 运行它时,都会收到此错误消息:
********************************
应用程序无法启动
********************************
描述:
com.todo.demo.service.TaskService 构造函数参数 0 需要一个名为“entityManagerFactory”的 bean,但无法找到。
行动:
考虑在您的应用程序中定义一个名为“entityManagerFactory”的 bean 配置。
此时问题可能来自任何方面,但我认为问题可能出在此处。
我的实体类
package com.todo.demo;
import java.time.LocalDate;
import jakarta.persistence.*;
@Entity
@Table(name = "tasks")
/**
* @Author Noah Rouse
* @email: [email protected]
* @description: this class is designed to store and represent tasks in a todo list
*/
public class Task {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private long id;
@Column(name = "description", nullable = false)
private String description;
@Column(name = "completed")
private boolean completed;
@Column(name = "priority")
private String priority;
@Column(name = "due_date")
private LocalDate dueDate;
@Column(name = "created")
private LocalDate created;
@Column(name = "last_updated")
private LocalDate lastUpdated;
public Task () {}
public Task (final Long id, final String description, final String priority,
final LocalDate dueDate) {
this.id = id;
this.completed = false;
this.description = description;
this.priority = priority;
this.dueDate = dueDate;
this.created = LocalDate.now();
this.lastUpdated = created;
}
/**
* @return task id
*/
public long id () {
return this.id;
}
/**
* @return description
*/
public String description () {
return this.description;
}
/**
* set task description to new description and changes last updated value
* @return updated description
*/
public String description (final String description) {
this.description = description;
this.lastUpdated = LocalDate.now();
return this.description;
}
/**
* @return if the task is completed
*/
public boolean completed () {
return this.completed;
}
/**
* Set completed to true or false
* @return updated completed status
*/
public boolean completed (final boolean completed) {
this.completed = completed;
this.lastUpdated = LocalDate.now();
return this.completed;
}
/**
* @return priority of task
*/
public String priority () {
return this.priority;
}
/**
* sets task to new priority
* @return new priority
*/
public String priority (final String priority) {
this.priority = priority;
this.lastUpdated = LocalDate.now();
return this.priority;
}
/**
* @return current task due date
*/
public LocalDate dueDate () {
return this.dueDate;
}
/**
* change due date to new local date
* @return new due date
*/
public LocalDate dueDate (final LocalDate dueDate) {
this.dueDate = dueDate;
this.lastUpdated = LocalDate.now();
return this.dueDate;
}
/**
* @return when the task was last updated
*/
public LocalDate lastUpdated () {
return this.lastUpdated;
}
}
我的服务班
package com.todo.demo.service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.todo.demo.Task;
import com.todo.demo.repository.TaskRepository;
import java.time.LocalDate;
import java.util.Optional;
@Service
@Transactional
public class TaskService {
public class TaskNotFoundException extends Exception {
}
private final TaskRepository taskRepos;
public TaskService (final TaskRepository taskRepository) {
taskRepos = taskRepository;
}
public void updateTaskDescription (final long taskId, final String description)
throws TaskNotFoundException {
final Optional<Task> potentialTask = taskRepos.findById(taskId);
if (potentialTask.isPresent()) {
final Task task = potentialTask.get();
task.description(description);
taskRepos.save(task);
} else {
throw new TaskNotFoundException();
}
}
public void updateTaskCompletion (final long taskId, final boolean completed)
throws TaskNotFoundException {
final Optional<Task> potentialTask = taskRepos.findById(taskId);
if (potentialTask.isPresent()) {
final Task task = potentialTask.get();
task.completed(completed);
taskRepos.save(task);
} else {
throw new TaskNotFoundException();
}
}
public void updateTaskPriority (final long taskId, final String priority)
throws TaskNotFoundException {
final Optional<Task> potentialTask = taskRepos.findById(taskId);
if (potentialTask.isPresent()) {
final Task task = potentialTask.get();
task.priority(priority);
taskRepos.save(task);
} else {
throw new TaskNotFoundException();
}
}
public void updateTaskDueDate (final long taskId, final LocalDate dueDate)
throws TaskNotFoundException {
final Optional<Task> potentialTask = taskRepos.findById(taskId);
if (potentialTask.isPresent()) {
final Task task = potentialTask.get();
task.dueDate(dueDate);
taskRepos.save(task);
} else {
throw new TaskNotFoundException();
}
}
}
我的 pom.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.todo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.214</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>3.3.5</version>
</dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我的application.properties文件:
spring.application.name=demo
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.h2.console.enabled=true
将此部分替换为 pom.xml 中的
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>3.3.5</version>
</dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>3.2.0</version>
</dependency>
与
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>