Sal*_*yan 4 java annotations hibernate spring-mvc spring-boot
我是 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);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的实体:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的存储库:
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>{
}
Run Code Online (Sandbox Code Playgroud)
这是我的控制器:
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";
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的 application.properties:
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
Run Code Online (Sandbox Code Playgroud)
当我在示例中到达这一点时,据说运行应用程序将在数据库中创建表,但我得到的只是标题中提到的错误。
现在应该做什么才能使其发挥作用?
Dan*_*ani 14
我想这个评论是关键。
当我使用 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
还必须迁移工件。
有关此更改的更多背景信息,在另一个 SO 线程中得到了很好的解释。
希望它对其他人有帮助!
归档时间: |
|
查看次数: |
16143 次 |
最近记录: |