我该如何解决该错误?我已经尝试了所有可能的方法,但无法弄清

时间:2019-07-18 01:40:05

标签: rest maven spring-boot

springboot-jpa。

我尝试将@ ComponentScan,@ EntityScan和@EnableJpaRepositories添加到主应用程序类中,还尝试删除.m2文件夹并重新启动STS工具。还更新了Maven项目。我在pom.xml文件中添加了所有必需的依赖项。尝试从mysql数据库获取数据时出现以下错误。


申请无法开始


说明:

springboot.demo.QuoteService中的qi字段需要一个名为'entityManagerFactory'的bean。

操作:

考虑在您的配置中定义一个名为'entityManagerFactory'的bean。

/ 代码 /

package springboot.demo;

import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.EntityManagerFactory;

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.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.stereotype.Component;

@Configuration
@EnableJpaRepositories()
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
@EntityScan(basePackages = "springboot.demo")
@ComponentScan(basePackages = "springboot.demo")
@SpringBootApplication(scanBasePackages = { "springboot.demo" })
public class App {

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

/*Quote.java*/

package springboot.demo;

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

@Entity
@Table(name = "quotes")
public class Quote {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    public Quote(String author, String quote) {
        super();
        this.author = author;
        this.quote = quote;
    }

    @Override
    public String toString() {
        return "Quote [id=" + id + ", author=" + author + ", quote=" + quote + "]";
    }

    public Quote() {

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getQuote() {
        return quote;
    }

    public void setQuote(String quote) {
        quote = quote;
    }

    private String author;

    private String quote;

}


/* QuoteController.java*/
package springboot.demo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

@RestController
class QuoteController {

    /*
     * @Autowired QuoteRepository qRep;
     */

    @Autowired
    private QuoteService qs;

    @RequestMapping("/")
    public Quote getQuote() {
        return (Quote) qs.getQuotes();

    }

}





package springboot.demo;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

@Repository
public interface QuoteRepository extends JpaRepository<Quote, Integer> {

}




/*QuoteService.java*/

package springboot.demo;

import java.util.List;

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

@Service
public class QuoteService {

    @Autowired
    private QuoteRepository qi;

    public Quote getQuotes() {
        return (Quote) qi.findAll();
    }

}


/*application.properties file*/

spring.datasource.url=jdbc:mysql://localhost:3306/
spring.datasource.username=root
spring.datasource.password=root123
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration



/*pom.xml*/

0 个答案:

没有答案