我尝试在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;
}
}
我的Pojo
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
答案 0 :(得分:0)
我已经实施了您的项目。一切都很好。我正在逐步描述。我这样写我的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.avijit</groupId>
<artifactId>bootproject</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
然后我写了我的主类并为jsp页面创建了一个bean。
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
@Bean
public ViewResolver getViewResolver(){
InternalResourceViewResolver resolver = new
InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
}
此后,我编写了
这样的Controller类。 @Autowired
UserService userService;
@RequestMapping(value="/sample", method= RequestMethod.GET)
public String sample()
{
return "sample";
}
@RequestMapping(value="/sample", method= RequestMethod.POST)
public String dosave(@RequestParam String name, @RequestParam String email)
{
UserPojo n = new UserPojo();
n.setCustomerName(name);
n.setEmail(email);
userService.addUser(n);
return "sample";
}
已编辑的服务类别
@Autowired
private User user;
public void addUser(UserPojo pojo) {
UserEntity userEntity = new UserEntity();
userEntity.setCustomerName(pojo.getCustomerName());
userEntity.setEmail(pojo.getEmail());
user.save(userEntity);
}
在复制代码时,我发现在控制器类中您犯了一个错误n.setName(name);
,它是n.setCustomerName(name)
。在我的财产文件中,我是这样写的。
spring.datasource.url =jdbc:mysql://127.0.0.1:3306/InternDatabase
spring.datasource.username = root
spring.datasource.password = 123456
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto = update
检查更新的代码,并告诉我们。