没有“com.jpa.dao.userrepository”类型的合格bean可用

plupiseo  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(332)

我是第一次尝试字符串jpa。
我在关注一段由durgesh先生拍摄的youtube视频。
这就是错误

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.jpa.dao.UserRepository' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:351)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:342)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1177)
    at com.jpa.demo.BootjpaApplication.main(BootjpaApplication.java:13)

以下是我的档案
bootjpaaapplication.java(包含main)

package com.jpa.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import com.jpa.dao.UserRepository;
import com.jpa.entities.user;
@SpringBootApplication
public class BootjpaApplication {

    public static void main(String[] args) {
        ApplicationContext context= SpringApplication.run(BootjpaApplication.class, args);
        UserRepository userrepo= context.getBean(UserRepository.class);
        user user=new user();
        user.setName("Varun Dhawan");
        user.setCity("Paris");
        user.setStatus("happy");
        user user1= userrepo.save(user);
        System.out.println(user1);
    }

java(有4个变量。id是自动生成的。另外3个有干将和二传手。

package com.jpa.entities;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class user {
    @Id
    @GeneratedValue(strategy =GenerationType.AUTO)
    private int id;

    private String name;
    private String city;
    private String status;
    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 getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public user(int id, String name, String city, String status) {
        super();
        this.id = id;
        this.name = name;
        this.city = city;
        this.status = status;
    }
    public user() {
        super();
        // TODO Auto-generated constructor stub
    }
    @Override
    public String toString() {
        return "user [id=" + id + ", name=" + name + ", city=" + city + ", status=" + status + "]";
    }

}

用户存储库.java

package com.jpa.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.jpa.entities.user;
@Repository
public interface UserRepository extends JpaRepository<user,Integer> {

}

应用程序属性

spring.datasource.name=jpa
spring.datasource.url=jdbc:mysql://localhost:3306/jpa
spring.datasource.username=root
spring.datasource.password=root@123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect

spring.jpa.hibernate.ddl-auto=update

谢谢您!

yacmzcpb

yacmzcpb1#

看起来你的包裹没有被正确扫描。
你能试着添加吗 @SpringBootApplication(scanBasePackages = "com.jpa") 而不是 @SpringBootApplication .

相关问题