Java Spring启动应用程序无法启动?

Java Spring启动应用程序无法启动?,java,spring,maven,spring-boot,spring-data-jpa,Java,Spring,Maven,Spring Boot,Spring Data Jpa,我仍然是SpringBoot的初学者,我正在努力学习如何使用SpringDataJPA。我试图从我的控制器获取一个查询参数,然后使用SpringDataJPA将该数据添加到MySQL数据库。但是我得到了这个奇怪的错误,我不知道如何修复它 这就是我到目前为止所尝试的 Home Controller package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.

我仍然是SpringBoot的初学者,我正在努力学习如何使用SpringDataJPA。我试图从我的控制器获取一个查询参数,然后使用SpringDataJPA将该数据添加到MySQL数据库。但是我得到了这个奇怪的错误,我不知道如何修复它

这就是我到目前为止所尝试的

Home Controller
package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.model.Home;
import com.example.demo.service.HomeServiceImpl;

@RestController
public class HomeController {

    @Autowired
    private HomeServiceImpl sh;

    @RequestMapping("/homedata")
    public Home Hello(@RequestParam("name") String name){
       Home s = sh.fetchinfo(name);

       System.out.println("sss"+s);
        return s;
    }


}

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan(basePackages="com.example.demo")
@EnableJpaRepositories(basePackages="com.example.demo")
@EnableTransactionManagement
@EntityScan(basePackages="com.example.demo")
public class SpringUnitTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringUnitTestApplication.class, args);
    }
}

Model class

package com.example.demo.model;

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

@Entity
@Table(name = "personal")
public class Home {


    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private int id;
    @Column(name = "name")
    private String name;



    public Home(String name) {
        this.name = name;

        // TODO Auto-generated constructor stub
    }
    public String getName() {
        return name;
    }

    public String toString(){
        return "Hello" + getName();
    }



}


Service layer


 package com.example.demo.service;

    import com.example.demo.model.Home;

    public interface HomeService {
        public Home fetchinfo(String name);


    }

package com.example.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.example.demo.model.Home;
import com.example.demo.repository.HomeRepository;

@Component
public class HomeServiceImpl implements HomeService{

    @Autowired
    private HomeRepository rs;

    @Override
    public Home fetchinfo(String name){
        Home s = new Home(name);
         rs.save(s) ;   
        System.out.println("This is from service layer"+s);
        return s;
    }



}
存储库

package com.example.demo.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.repository.CrudRepository;

import com.example.demo.model.Home;



public interface HomeRepository extends JpaRepository<Home, Long> {



}

您正在混合版本,而不是为数据jpa引入启动程序

请看这篇博客文章,详细说明如何以及为什么不应该这样做

您应该只需要声明以下依赖项

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

org.springframework.boot
弹簧启动机tomcat
假如
org.springframework.boot
弹簧起动试验
测试
org.springframework.boot
spring引导启动器数据jpa

您正在混合版本,而不是为数据jpa启动

请看这篇博客文章,详细说明如何以及为什么不应该这样做

您应该只需要声明以下依赖项

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

org.springframework.boot
弹簧启动机tomcat
假如
org.springframework.boot
弹簧起动试验
测试
org.springframework.boot
spring引导启动器数据jpa

从SpringUnitTestApplication类中删除除@SpringBootApplication之外的所有注释。然后请尝试告诉我您的情况。@Zenith:-删除*******************************后,应用程序无法启动****************************说明:com.example.demo.service.HomeServiceImpl中的字段rs需要一个找不到的“com.example.demo.repository.homerervitory”类型的bean。Actudio:考虑在您的配置中定义“COM.Sem.Exem.Demo.RealStemial.HooRePiSteme”类型的bean。遵循这里给出的代码示例:删除所有注解,除了从SrimutUnTestAp应用类中的“SpRunBooTeApple”。然后请尝试告诉我您的情况。@Zenith:-删除*******************************后,应用程序无法启动****************************说明:com.example.demo.service.HomeServiceImpl中的字段rs需要一个找不到的“com.example.demo.repository.homerervitory”类型的bean。操作:考虑在您的配置中定义一个类型为“COM..Exem.Demo.RealStemial.HealeRePiple”的bean。
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>