【发布时间】:2018-07-07 10:19:27
【问题描述】:
我正在尝试在 Spring Boot 中访问数据 mysql。当我运行应用程序时。出现此错误
在类路径资源 [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class] 中创建名称为“entityManagerFactory”的 bean 时出错:调用 init 方法失败;嵌套异常是 java.lang.NoClassDefFoundError: javassist/bytecode/ClassFile
我的代码是
控制器
@GetMapping(path="/add")
public @ResponseBody String addNewUser (@RequestParam String name
, @RequestParam String email) {
UserPojo n = new UserPojo();
n.setName(name);
n.setEmail(email);
userRepository.save(n);
return "Saved";
}
服务是
@Service("service")
@Transactional
public class UserService {
@Autowired
private User user;
public void addUser(UserPojo pojo) {
UserEntity userEntity = new UserEntity();
userEntity.setCustomerName(pojo.getCustomerName());
userEntity.setEmail(pojo.getEmail());
user.save(userEntity);
}
}
道是
public interface User extends CrudRepository<UserEntity, Integer> {
}
实体是-
package com.example.entity;
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 = "customer")
public class UserEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column
private String customerName;
@Column
private String email;
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
我的宝乔
public class UserPojo {
private String customerName;
private String email;
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
我的 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0
<groupId>com.example</groupId>
<artifactId>Test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Test</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Use MySQL Connector-J -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
application.properties
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
server.port=8085
server.servlet.context-path=/finance
# Database
spring.datasource.url=jdbc:mysql://localhost:3306/test?
autoReconnect=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# Hibernate
hibernate.dialect: org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql: true
hibernate.hbm2ddl.auto: create
【问题讨论】:
-
你在哪里定义了
entityManager? -
@yassadi 我在这里得到这个代码spring.io/guides/gs/accessing-data-mysql
-
您的项目运行情况如何?通过 IDE 还是通过 cmd?你也在使用 Maven 还是 Gradle。你能分享你的
pom.xml -
@Aris 我只是从 Eclipse IDE 运行 >> 作为 Spring Boot App 运行
-
我已经实施了你的项目,一切顺利!!我会发布吗?
标签: java spring spring-boot