在我 运行 我的 spring 启动应用程序后找到 0 个 JPA 存储库接口
Found 0 JPA repository interfaces after I ran my spring boot application
1.用户记录
package auj.helpdesk.model;
package auj.helpdesk.model;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class UserRecord {
@Id
private int id;
private String name;
private String email;
//default conatructor
public UserRecord() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
2。用户控制器
package auj.helpdesk.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import auj.helpdesk.model.UserRecord;
import auj.helpdesk.services.UserService;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/")
public List<UserRecord> getAllUser() {
return userService.getAllUsers();
}
@RequestMapping(value = "/add-user", method = RequestMethod.POST)
public void addUser(@RequestBody UserRecord userRecord) {
userService.addUser(userRecord);
}
}
3。用户库
package auj.helpdesk.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;
import auj.helpdesk.model.UserRecord;
public interface UserRepository extends CrudRepository<UserRecord, String> {
}
4. UserService
package auj.helpdesk.services;
import java.util.List;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import auj.helpdesk.model.UserRecord;
import auj.helpdesk.repository.UserRepository;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public List<UserRecord> getAllUsers() {
List<UserRecord> userRecords = new ArrayList<>();
userRepository.findAll().forEach(userRecords::add);
return userRecords;
}
public void addUser(UserRecord userRecord) {
userRepository.save(userRecord);
}
}
AujhelpdeskApplication
package auj.helpdesk.starter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AujhelpdeskApplication {
public static void main(String[] args) {
SpringApplication.run(AujhelpdeskApplication.class, args);
}
}
5. Application.properties
spring.datasource.url=jdbc:oracle:thin:@LAPTOP-U9NGFKE9:1521:XE
spring.datasource.username=M1
spring.datasource.password=M1
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
spring.jpa.show-sql=true
server.port=8090
6. 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>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>auj.helpdesk</groupId>
<artifactId>aujhelpdesk</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>aujhelpdesk</name>
<description>A help desk for Auj</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-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>
我的 Spring 数据存储库未扫描。我已经检查了我的 application.properties,但它不工作。
在您的 UserRepository 界面中使用 @Repository
。
因为你的 AujhelpdeskApplication
在包 auj.helpdesk.starter;
中,Spring 引导将只扫描以 auj.helpdesk.starter
开头的包,这意味着它会跳过 auj.helpdesk.repository
因为他们是兄弟姐妹,所以 repository
不在 auj.helpdesk.starter
包内。
package auj.helpdesk.starter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AujhelpdeskApplication {
public static void main(String[] args) {
SpringApplication.run(AujhelpdeskApplication.class, args);
}
}
为了解决这个问题,您要么必须将主 class 移动到包 auj.helpdesk
以便包含包是所有包的父级,要么必须指定要组件查看的位置.
package auj.helpdesk.starter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = "auj.helpdesk")
public class AujhelpdeskApplication {
public static void main(String[] args) {
SpringApplication.run(AujhelpdeskApplication.class, args);
}
}
您可以为此尝试 2 种解决方案。
将您的服务和存储库包作为“auj.helpdesk.starter”的子包。然后将扫描您的服务和存储库以创建 bean。
或者您可以通过指定服务和存储库包在主 class 上添加 @ComponentScan 注释。
例如@ComponentScan({ "auj.helpdesk.repository", "auj.helpdesk.services" })
1.用户记录
package auj.helpdesk.model;
package auj.helpdesk.model; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class UserRecord { @Id private int id; private String name; private String email; //default conatructor public UserRecord() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
2。用户控制器
package auj.helpdesk.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import auj.helpdesk.model.UserRecord; import auj.helpdesk.services.UserService; import java.util.List; @RestController public class UserController { @Autowired private UserService userService; @RequestMapping("/") public List<UserRecord> getAllUser() { return userService.getAllUsers(); } @RequestMapping(value = "/add-user", method = RequestMethod.POST) public void addUser(@RequestBody UserRecord userRecord) { userService.addUser(userRecord); } }
3。用户库
package auj.helpdesk.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;
import auj.helpdesk.model.UserRecord;
public interface UserRepository extends CrudRepository<UserRecord, String> {
}
4. UserService
package auj.helpdesk.services;
import java.util.List;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import auj.helpdesk.model.UserRecord;
import auj.helpdesk.repository.UserRepository;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public List<UserRecord> getAllUsers() {
List<UserRecord> userRecords = new ArrayList<>();
userRepository.findAll().forEach(userRecords::add);
return userRecords;
}
public void addUser(UserRecord userRecord) {
userRepository.save(userRecord);
}
}
AujhelpdeskApplication
package auj.helpdesk.starter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AujhelpdeskApplication {
public static void main(String[] args) {
SpringApplication.run(AujhelpdeskApplication.class, args);
}
}
5. Application.properties
spring.datasource.url=jdbc:oracle:thin:@LAPTOP-U9NGFKE9:1521:XE
spring.datasource.username=M1
spring.datasource.password=M1
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
spring.jpa.show-sql=true
server.port=8090
6. 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>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>auj.helpdesk</groupId>
<artifactId>aujhelpdesk</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>aujhelpdesk</name>
<description>A help desk for Auj</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-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>
我的 Spring 数据存储库未扫描。我已经检查了我的 application.properties,但它不工作。
在您的 UserRepository 界面中使用 @Repository
。
因为你的 AujhelpdeskApplication
在包 auj.helpdesk.starter;
中,Spring 引导将只扫描以 auj.helpdesk.starter
开头的包,这意味着它会跳过 auj.helpdesk.repository
因为他们是兄弟姐妹,所以 repository
不在 auj.helpdesk.starter
包内。
package auj.helpdesk.starter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AujhelpdeskApplication {
public static void main(String[] args) {
SpringApplication.run(AujhelpdeskApplication.class, args);
}
}
为了解决这个问题,您要么必须将主 class 移动到包 auj.helpdesk
以便包含包是所有包的父级,要么必须指定要组件查看的位置.
package auj.helpdesk.starter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = "auj.helpdesk")
public class AujhelpdeskApplication {
public static void main(String[] args) {
SpringApplication.run(AujhelpdeskApplication.class, args);
}
}
您可以为此尝试 2 种解决方案。
将您的服务和存储库包作为“auj.helpdesk.starter”的子包。然后将扫描您的服务和存储库以创建 bean。
或者您可以通过指定服务和存储库包在主 class 上添加 @ComponentScan 注释。 例如@ComponentScan({ "auj.helpdesk.repository", "auj.helpdesk.services" })