提问人:Andrew26 提问时间:11/13/2023 最后编辑:Andrew26 更新时间:11/13/2023 访问量:45
JPA 存储库在我的项目中不可见
JPA Repository is not visible in my project
问:
我在启动我的Spring Boot项目时遇到了麻烦,因为我收到一个错误,指出我没有在我的项目中定义Repository类型的bean,即使我有。这是我的代码:
控制器:
package com.example.demo1.controller;
import com.example.demo1.entities.Book;
import com.example.demo1.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/api")
public class BookController {
@Autowired
BookService bookService;
@GetMapping(value="/books/all")
public ResponseEntity<List<Book>> getAllBooks(){
return ResponseEntity.ok(bookService.getAllBooks());
}
}
服务
package com.example.demo1.service;
import com.example.demo1.entities.Book;
import com.example.demo1.repository.BookJPARepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookService {
@Autowired
BookJPARepository bookJPARepository;
public List<Book> getAllBooks(){
return bookJPARepository.findAll();
}
}
存储 库
package com.example.demo1.repository;
import com.example.demo1.entities.Book;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface BookJPARepository extends JpaRepository<Book, Long> {
}
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.1.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
<configuration>
<image>
<builder>paketobuildpacks/builder-jammy-base:latest</builder>
</image>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
实体:
package com.example.demo1.entities;
import jakarta.persistence.*;
import lombok.Data;
@Entity
@Table(name = "books", schema = "books")
@Data
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "book_id", nullable = false)
private Long id;
@Basic
@Column(name = "book_name", nullable = false, length = 255)
private String title;
@Basic
@Column(name = "book_author", nullable = false, length = 255)
private String author;
@Basic
@Column(name = "book_publishing_house", nullable = false, length = 255)
private String publishing_house;
@Basic
@Column(name = "book_price")
private float price;
public Book(String book_name, String book_author, String book_price, String book_publishing_house) {
this.title=book_name;
this.author=book_author;
this.price=Float.parseFloat(book_price);
this.publishing_house=book_publishing_house;
}
}
应用程序是这个:
package com.example.demo1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@ComponentScan
@Configuration
public class Demo1Application {
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
}
}
错误是这样的:
应用程序无法启动
描述:
com.example.demo1.service.BookService 中的字段 bookJPARepository 需要一个找不到的“com.example.demo1.repository.BookJPARepository”类型的 bean。
注入点具有以下注释: - @org.springframework.beans.factory.annotation.Autowired(required=true)
行动:
请考虑在配置中定义一个类型为“com.example.demo1.repository.BookJPARepository”的 bean。
我错过了什么吗?你能帮帮我吗?
我希望我的应用程序应该启动。使用 Spring initialzr 创建项目时,我是否遗漏了任何依赖项?
答:
0赞
sarthak sethi
11/13/2023
#1
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) 删除此行它将起作用,它禁用数据库
评论
@SpringBootApplication
com.example.demo1
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@ComponentScan
@Configuration
@ EnableAutoConfiguration
@ComponentScan
@Configuration
MySQLDialect