运行 Spring Boot 应用程序后发现 0 个 JPA 存储库接口

Anm*_*ngh 3 spring jpa spring-jdbc spring-data-jpa spring-boot

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;
  }
}
Run Code Online (Sandbox Code Playgroud)

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);
  }
}
Run Code Online (Sandbox Code Playgroud)

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> {
}
Run Code Online (Sandbox Code Playgroud)

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Auj帮助台应用程序

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);
    }

}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

我的 Spring 数据存储库未扫描。我已经检查了我的 application.properties 但它不起作用。

Par*_*ras 6

由于您的AujhelpdeskApplication包位于 package 中auj.helpdesk.starter;,Spring boot 将仅扫描以 开头的包auj.helpdesk.starter,这意味着它将跳过auj.helpdesk.repository,因为它们是兄弟姐妹,repository不位于auj.helpdesk.starterpackage 内。

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);
    }

}
Run Code Online (Sandbox Code Playgroud)

为了解决这个问题,您要么必须将主类移动到包中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);
    }

}
Run Code Online (Sandbox Code Playgroud)