gpt4 book ai didi

java - Spring Boot - “Error creating bean with name 'entityManagerFactory'” - 启动

转载 作者:行者123 更新时间:2023-12-02 08:39:17 25 4
gpt4 key购买 nike

我看到了很多问题,但答案并不能满足我的问题...

我用 Spring initialzr 生成我的项目 enter image description here

我尝试了 3 个 java 版本 14、11、8。

这是我的项目结构。

enter image description here

GardenApplication.java。我包括所有注释,甚至 spring 文档说使用 @SpringBootApplication 就足够了。在此错误之前,无法找到@Repository,我包含@EnableJpaRepositories

package com.garden.garden;

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;

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan({"com.garden.garden.controller", "com.garden.garden.service.impl"})
@EnableJpaRepositories(basePackages = {"com.garden.garden.repository"})
@EntityScan(basePackages = {"com.garden.garden.model"})
public class GardenApplication {

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

}

VegetableController.java

package com.garden.garden.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.garden.garden.model.Vegetable;
import com.garden.garden.service.VegetableService;

@Controller
@RequestMapping
public class VegetableController {

@Autowired
private VegetableService vegetableService;

@GetMapping("/vegetableList")
public String list(Model model) {
List<Vegetable> listVegetables = vegetableService.list();
model.addAttribute("vegetables", listVegetables);
return "index";
}

}

VegetableServiceImpl.java

package com.garden.garden.service.impl;

import java.util.List;
import java.util.Optional;

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

import com.garden.garden.model.Vegetable;
import com.garden.garden.repository.VegetableRepository;
import com.garden.garden.service.VegetableService;

@Service
public class VegetableServiceImpl implements VegetableService {

@Autowired
private VegetableRepository vegetableRepository;

@Override
public List<Vegetable> list() {
return (List<Vegetable>) vegetableRepository.findAll();
}

@Override
public Optional<Vegetable> listById(int id) {
// TODO Auto-generated method stub
return null;
}

@Override
public int save(Vegetable vegetable) {
// TODO Auto-generated method stub
return 0;
}

@Override
public void delete(int id) {
// TODO Auto-generated method stub

}

}

VegetableService.java

package com.garden.garden.service;

import java.util.List;
import java.util.Optional;

import com.garden.garden.model.Vegetable;

public interface VegetableService {
public List<Vegetable> list();
public Optional<Vegetable> listById(int id);
public int save(Vegetable vegetable);
public void delete(int id);
}

VegetableRepository.java

package com.garden.garden.repository;

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

import com.garden.garden.model.Vegetable;

@Repository
public interface VegetableRepository extends JpaRepository<Vegetable, Integer> {

}

Vegetable.java

package com.garden.garden.model;

import java.math.BigDecimal;

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

@Entity
@Table(name = "vegetable")
public class Vegetable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String family;
private int daysOnNursery;
private int daysToGrow;
private int daysHarvesting;
private java.math.BigDecimal plantsPerSquare;
private java.math.BigDecimal price;
private java.math.BigDecimal weight;



public Vegetable(int id, String name, String family, int daysOnNursery, int daysToGrow, int daysHarvesting,
BigDecimal plantsPerSquare, BigDecimal price, BigDecimal weight) {
super();
this.id = id;
this.name = name;
this.family = family;
this.daysOnNursery = daysOnNursery;
this.daysToGrow = daysToGrow;
this.daysHarvesting = daysHarvesting;
this.plantsPerSquare = plantsPerSquare;
this.price = price;
this.weight = weight;
}

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 getFamily() {
return family;
}

public void setFamily(String family) {
this.family = family;
}

public int getDaysOnNursery() {
return daysOnNursery;
}

public void setDaysOnNursery(int daysOnNursery) {
this.daysOnNursery = daysOnNursery;
}

public int getDaysToGrow() {
return daysToGrow;
}

public void setDaysToGrow(int daysToGrow) {
this.daysToGrow = daysToGrow;
}

public int getDaysHarvesting() {
return daysHarvesting;
}

public void setDaysHarvesting(int daysHarvesting) {
this.daysHarvesting = daysHarvesting;
}

public java.math.BigDecimal getPlantsPerSquare() {
return plantsPerSquare;
}

public void setPlantsPerSquare(java.math.BigDecimal plantsPerSquare) {
this.plantsPerSquare = plantsPerSquare;
}

public java.math.BigDecimal getPrice() {
return price;
}

public void setPrice(java.math.BigDecimal price) {
this.price = price;
}

public java.math.BigDecimal getWeight() {
return weight;
}

public void setWeight(java.math.BigDecimal weight) {
this.weight = weight;
}

}

我还尝试解决添加此依赖项的问题:

<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>

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.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.garden</groupId>
<artifactId>garden</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>garden</name>
<description>Market garden web application</description>

<properties>
<java.version>14</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-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

application.properties

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

spring.datasource.url=jdbc:mysql://localhost:3306/garden
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.hibernate.ddl-auto=create

最佳答案

请删除 application.properties 中对自动配置的排除,并添加 hibernate-dialect,即更新您的 application.properties,如下所示:

spring.datasource.url=jdbc:mysql://localhost:3306/garden
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.hibernate.ddl-auto=create
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect

此外,您可以将GardenApplication简化为

package com.garden.garden;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EnableJpaRepositories
@EntityScan
public class GardenApplication {

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

}

并从 pom 中删除 tomcat 依赖项(它通过 spring-boot-starter-web 传递)。此外,在您的依赖项中添加 lombok,您可以使用 @Data 注释 Vegetable 并删除 Getters、Setters 和构造函数。

关于java - Spring Boot - “Error creating bean with name 'entityManagerFactory'” - 启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61486804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com