0

带有 h2 的简单 Spring Boot 应用程序,其中有 1 个通过 data.sql 填充的 DB 表。如果我切换到 2.0.0.RELEASE,则使用 spring-boot-starter-parent 1.5.9.RELEASE 100% 工作,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>

......

以及在 src/main/resources/data.sql 下插入数据库表的 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>  
4

1 回答 1

3

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

此外,要在控制台上查看调试,只需放入debug=trueapplication.properties文件

在记忆中

URL jdbc:h2:mem:test 表示打开一个名为“test”的内存数据库。数据不会持久化,并且在关闭与数据库的最后一个连接时会丢失。多个线程可以访问同一个数据库,但数据只在同一个进程内可见

有关详细信息,请参阅http://www.h2database.com/html/features.html#database_url

于 2018-10-27T19:32:10.907 回答