[英]How do I create a database with Spring/Hibernate?
首先,是的,我已经搜索过以前的问题。 不幸的是,所有答案都无法正常工作。
我创建了一个实体类Product
,一个存储库类ProductRepository
和一个主类Application
。
在下面找到代码:
@SpringBootApplication
public class Application {
public static void main (String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);
ProductRepository repo = ctx.getBean(ProductRepository.class);
Product product = new Product();
product.setDescription("HDD");
repo.save(product);
}
}
@Entity
@Table(name="Products")
public class Product {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int product_id;
private String description;
public int getProduct_id() {
return product_id;
}
public void setProduct_id(int product_id) {
this.product_id = product_id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
@Repository
public interface ProductRepository extends CrudRepository<Product, Integer> {
}
该文件如下所示:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:8080/entmob
spring.datasource.username=entmob
spring.datasource.password=*******
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.show_sql=true
spring.jpa.hibernate.hbm2ddl.auto=create
<?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.springframework</groupId>
<artifactId>gs-relational-data-access</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>[5,]</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
编译给出零错误。 但是,该数据库未在我的本地主机中创建。 我正在使用XAMPP来管理我的本地主机。
有人可以提供帮助吗? 非常感谢!
<property name="hibernate.hbm2ddl.auto">create</property>
将创建表。 但是它不会创建数据库。 更改连接URL以生成数据库,如下所示。
jdbc:mysql://localhost:8080/entmob?createDatabaseIfNotExist=true
在pom.xml中使用以下连接器,并在连接URL上方使用。
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version> <!-- or 5.1.28 / 5.1.30 -->
</dependency>
这是解决方案。 正如我在评论部分所建议的:
我首先尝试从pom.xml删除H2依赖项,然后尝试。 我怀疑您的数据库已嵌入。
当springboot找到h2依赖项时,它将把它视为具有嵌入式数据库的webapp,并且将忽略您的mysql配置
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.