使用springboot生成MySql数据库

时间:2017-01-13 14:34:09

标签: java mysql spring maven spring-boot

我想用springboot项目生成MySQL数据库。 我创建了一个maven项目,这是pom.xml:

<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>Springboot</groupId>
<artifactId>ma.mahmoud.springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringBootApps</name>
<description>First project</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.4.RELEASE</version>
</parent>

<properties>
    <hibernate.version>5.0.3.Final</hibernate.version>
    <spring.version>4.2.2.RELEASE</spring.version>
</properties>

<dependencies>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <!-- SPRING -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0.2</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-collections4</artifactId>
        <version>4.0</version>
    </dependency>
</dependencies>

<!-- BUILD -->
<build>
    <plugins>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

我创建了一个带有javax.persistence注释的实体,并使用spring数据创建了DAO,Service和一个控制器来公开我的服务。

我在src / main / resources

中添加了application.properties
server.port=1111
spring.datasource.url=jdbc:mysql://localhost:3306/sping-boot
spring.datasource.username=root
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create-drop

当我启动我的应用程序时,我没有任何错误,但数据库是创建的。

我忘记了什么吗?

1 个答案:

答案 0 :(得分:1)

你为什么要加上它?

   <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0.2</version>
    </dependency>

请添加示例EntityScanConfig类。另一方面,@EntityScan不会创建bean,据我所知。它仅标识特定持久化上下文应使用哪些类

@EnableAutoConfiguration
@EntityScan({"com.example.entity.model"})
public class EntityScanConfig {

}