未找到类型为'com.jpa.test.UserRepository'的合格bean的异常在主线程中发生
P粉170438285
P粉170438285 2023-08-29 14:35:44
[MySQL讨论组]

我是 Spring-boot 的初学者,当我尝试运行 Spring-boot 应用程序时,当我尝试运行 Spring-Boot 应用程序时,我遇到了这个问题。

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type com. JPA. test. User Repository available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1148)
    at com. JPA. test. Test Application. main(TestApplication.java:17)

我希望数据能够正确保存在数据库中。

目录文件夹排列:目录文件夹排列

错误页面:错误页面1 错误页面2

属性接口:属性申请页面

主类代码:

package com.jpa.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.ApplicationContext;


@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class TestApplication {

    public static void main(String[] args) {
        
        ApplicationContext context = SpringApplication.run(TestApplication.class, args);
    
        
        UserRepository ur  = context.getBean(UserRepository.class);
        
        User user = new User();
        user.setName("XYZ");
        user.setStatus("Active");
        user.setCity("OOPS");
        
        User save = ur. save(user);
        System.out.println(save);
    }

}

实体类(用户):

package com.jpa.test;

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

@Entity
public class User {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    
    private String name;
    
    private String city;
    
    private String status;
    
    public User() {
        super();
        // TODO Auto-generated constructor stub
    }

    public User(int id, String name, String city, String status) {
        super();
        this.id = id;
        this.name = name;
        this.city = city;
        this.status = 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;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", city=" + city + ", status=" + status + "]";
    }
    
    

}

Dao(用户存储库)

package com.jpa.test;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends CrudRepository<User, Integer> {

}

P粉170438285
P粉170438285

全部回复(1)
P粉345302753

尝试将这些注释添加到您的程序中:

@Configuration
@EnableAutoConfiguration
@ComponentScan
@EntityScan("com.jpa.test")
@EnableJpaRepositories("com.jpa.test")
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号