我是 Spring 和 Spring Boot 的新手。我尝试通过以下方式构建一个项目 我在这里找到的一个例子:http://www.javaguides.net/2018/09/spring-mvc-using-spring-boot2-jsp-jpa-hibernate5-mysql-example.html.
这是我的申请:
package com.SportyShoe;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@ComponentScan(basePackages = "com.SportyShoe")
@SpringBootApplication
@EntityScan("com.SportyShoe.*")
@EnableJpaRepositories
public class SportyShoeApplication {
public static void main(String[] args) {
SpringApplication.run(SportyShoeApplication.class, args);
}
}
这是我的实体:
package com.SportyShoe.Entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="Shoe")
public class Shoe {
@Id
@Column(name="id")
private String id;
@Column(name="colour")
private String colour;
@Column(name="gender")
private String gender;
@Column(name="category")
private String category;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getColour() {
return colour;
}
public void setColour(String colour) {
this.colour = colour;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
}
这是我的存储库:
package com.SportyShoe.repositories;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.SportyShoe.Entity.Shoe;
@Repository
public interface ShoeRepositories extends JpaRepository<Shoe, Integer>{
}
这是我的控制器:
package com.SportyShoe.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.SportyShoe.repositories.ShoeRepositories;
@Controller
public class ShoeController {
@Autowired
ShoeRepositories shoeRepo;
@RequestMapping("/shoes")
public String shoeList(Model model) {
model.addAttribute("shoes", shoeRepo.findAll());
return "shoes";
}
}
这是我的应用程序。属性:
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
logging.level.org.springframework=INFO
################### DataSource Configuration ##########################
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/Sporty_Shoes
spring.datasource.username=root
spring.datasource.password=MPword@123
################### Hibernate Configuration ##########################
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
当我在示例中到达这一点时,据说运行应用程序将在数据库中创建表,但我得到的只是标题中提到的错误。
现在应该做什么才能使其发挥作用?
我想这个评论是关键。
当我使用 2.7.2 而不是我最初使用的 3.0.0(SnapShot) 时 它开始工作了。
阅读文档,我们意识到
Spring Boot JPA
版本的Spring Boot 3
模块部分转向与Jakarta Persistence API (JPA)一起使用,而不是与javax.persistence.api
一起使用。因此,即使正确配置了 Spring JPA
注释(如 @EntityScan
),它也找不到实体。
将
Spring Boot
升级到版本 3 时,还必须迁移 Persistence API
工件。
有关此更改的更多背景信息,在this其他SO线程中得到了很好的解释。
希望对其他人有帮助!
主要问题是我使用的Spring boot版本。
当我使用 2.7.2 而不是我最初使用的 3.0.0(SnapShot) 时,它开始工作。
您的
Shoe
类中有 String ID,但您创建了 JpaRepository<Shoe, Integer>
而不是 JpaRepository<Shoe, String>
的存储库接口。因此,我建议在您的 Shoe
类中定义 Integer ID 以匹配存储库。
问题也可能出在包定义中 - javadoc 建议使用基本包名称,如“com.SportyShoe”,而不是“com.SportyShoe.*”。
您也可以尝试使用类型安全的实体扫描,如下所示:
@EntityScan(basePackageClasses = Shoe.class)
或者如果您有多个实体,则像这样:
@EntityScan(basePackageClasses = {Shoe.class, Lace.class})
还尝试删除
@EntityScan
、@ComponentScan
和 @EnableJpaRepositories
- spring-boot 会尝试在默认情况下具有 @SpringBootApplication
注释的包中和下查找实体和组件(以及 jpa 存储库,如果您有依赖项)在类路径上)。这些注释可用于额外配置。