如何使用 Spring/Hibernate 创建数据库?

abs*_*ree 2 java mysql xampp 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

产品

@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;
    }
}
Run Code Online (Sandbox Code Playgroud)

产品库

@Repository
public interface ProductRepository extends CrudRepository<Product, Integer> {

}
Run Code Online (Sandbox Code Playgroud)

application.properties 文件

该文件如下所示:

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
Run Code Online (Sandbox Code Playgroud)

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.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>
Run Code Online (Sandbox Code Playgroud)

编译给出零错误。但是,数据库没有在我的本地主机中创建。我正在使用 XAMPP 来管理我的本地主机。

有人可以提供任何帮助吗?非常感谢!

Roh*_*wad 6

<property name="hibernate.hbm2ddl.auto">create</property> 将创建表。但它不会创建数据库。更改连接 url 以生成数据库,如下所示。

jdbc:mysql://localhost:8080/entmob?createDatabaseIfNotExist=true
Run Code Online (Sandbox Code Playgroud)

在 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>
Run Code Online (Sandbox Code Playgroud)