pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.5.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
</dependencies>
公共接口EmployeeInterface扩展CrudRepository{}
@RestController
public class EmployeeController {
@Autowired
EmployeeInterface empInterface;
@RequestMapping("/getAll")
public List<Employee> getEmployees() {
return (List<Employee>)empInterface.findAll();
}
}
具有getter和setter 的员工实体
@Entity
@Table(name="Employee")
public class Employee implements Serializable {
private static final long serialVersionUID = 5406213393320888178L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
private Long id;
@Column(name="fname")
private String fname;
@Column(name="lname")
private String lname;
当我执行jar创建的时
java-Djava.security.egd=文件:/dev/./urandom-Dspring.profiles.active=容器-jar demo.jar
我看到这个错误
由以下原因引起:org.springframework.beans.factory.BeanCreationException:创建名为"jpaMappingContext"的bean时出错:调用init方法失败;嵌套异常为java.lang.IollegalArgumentException:必须至少存在一个JPA元模型!
使用此依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.5.6.RELEASE</version>
</dependency>
您使用的是spring-boot 1.5.6版本,但提供了spring-boot2.0.3 的jpa依赖性
更改您的mysql连接器版本:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
查看您的POM条目:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.5.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
用于弹簧靴的版本有1.5和2.0。Spring boot starter在您的情况下使用1.5.6,而您的starter jpa使用2.0,这在兼容性方面差异太大。始终使用相同的版本。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.5.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.5.6.RELEASE</version>
</dependency>
以上内容应该可以解决JPA bean的创建问题。
我遇到了同样的错误,我试图连接到一个不存在的模式。必须从application.properties文件中删除架构名称。然后它起作用了。