Spring Boot 2.0.0.RELEASE-不运行H2 data.sql

时间:2018-07-23 16:11:54

标签: spring-boot h2 h2db

带有h2的简单Spring Boot应用程序,该应用程序具有1个通过data.sql填充的DB表。如果我切换到2.0.0,则可以与spring-boot-starter-parent 1.5.9.RELEASE一起工作100%.RELEASE data.sql在启动时不再运行。

在我的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>

......

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
     <!-- <version>2.0.0.RELEASE</version> -->  
    <version>1.5.9.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-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
     <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>     
        <scope>runtime</scope>       
    </dependency>


</dependencies>

......

,以及一个SQL文件,该文件带有插入数据库表中的 src / main / resources / data.sql

在application.properties文件中

spring.datasource.url=jdbc:h2:file:~/greeter;AUTO_SERVER=TRUE
spring.datasource.initialization-mode=always

spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create-drop

在查看日志时使用1.5.9.RELEASE进行启动:

2018-07-23 17:53:41.219  INFO 11404 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000227: Running hbm2ddl schema export
Hibernate: drop table greeting if exists
Hibernate: create table greeting (id bigint not null, action varchar(255), say varchar(255), primary key (id))
2018-07-23 17:53:41.238  INFO 11404 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000230: Schema export complete
2018-07-23 17:53:41.341  INFO 11404 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2018-07-23 17:53:41.590  INFO 11404 --- [           main] o.s.jdbc.datasource.init.ScriptUtils     : Executing SQL script from URL [file:/..../classes/data.sql]

使用2.0.0.RELEASE

我的application.properties文件似乎什么都没捡到。 没有删除/重新创建数据库,也没有将data.sql导入到数据库中。和日志如下; 它似乎与Maven有关。问题所在的启动日志

2018-07-23 21:16:39.532  INFO 3600 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2018-07-23 21:16:39.741  INFO 3600 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2018-07-23 21:16:39.880  INFO 3600 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2018-07-23 21:16:39.911  INFO 3600 --- [  restartedMain] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2018-07-23 21:16:40.114  INFO 3600 --- [  restartedMain] org.hibernate.Version                    : HHH000412: Hibernate Core {5.2.14.Final}
2018-07-23 21:16:40.119  INFO 3600 --- [  restartedMain] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2018-07-23 21:16:40.382  INFO 3600 --- [  restartedMain] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2018-07-23 21:16:40.685  INFO 3600 --- [  restartedMain] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2018-07-23 21:16:41.840  INFO 3600 --- [  restartedMain] o.h.t.schema.internal.SchemaCreatorImpl  : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@542d091e'
2018-07-23 21:16:41.850  INFO 3600 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2018-07-23 21:16:43.574  INFO 3600 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@29bb347c: startup date [Mon Jul 23 21:16:33 CAT 2018]; root of context hierarchy
2018-07-23 21:16:43.710  WARN 3600 --- [  restartedMain] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning

除了pom.xml中的版本已更改外,其他所有内容...

在Spring 2.0.0.RELEASE中需要什么才能使其运行?

编辑1编辑1编辑1 它似乎以某种方式与Maven依赖关系有关? 如果我将存储库包含在pom.xml中,例如下面的所有内容都可以,请删除它们,我有上述问题

<repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>  

1 个答案:

答案 0 :(得分:2)

从h2-console连接时,请确保使用 jdbc:h2:mem:testdb 作为JDBC URL。当我们拥有h2提供的默认网址(jdbc:h2:~/test)时,您将不会在数据库中看到脚本结果。

此外,要在控制台上查看调试,只需将debug=true放在 application.properties 文件中。

内存中

  

URL jdbc:h2:mem:test表示打开一个内存数据库   名为“测试”。数据不持久,最后一次连接时丢失   到数据库已关闭。多个线程可以访问相同的   数据库,但数据仅在同一进程中可见

有关更多信息,请参见http://www.h2database.com/html/features.html#database_url