简体   繁体   English

为什么import.sql在Spring Boot中失败?

[英]Why would import.sql fail in Spring Boot?

I've followed this tutorial on Spring Boot. 我在Spring Boot上遵循了本教程

The guy goes fairly quickly but it seems like everything is the same with our code. 这个家伙走得很快,但似乎我们的代码都一样。 When I get to the point to view the H2 console I noticed I'm missing my Speaker table. 当我到达查看H2控制台的位置时,我注意到我缺少Speaker表。

I've seen lots of questions on here, blogs everywhere, and it seems all you have to do is have the file in main/resources and it works. 我在这里看到了很多问题,到处都是博客,看来您要做的就是在main/resources拥有该文件,并且可以正常工作。 Well, it doesn't! 好吧,事实并非如此!

Some of the answers talk about persistence.xml and/or a configuration file for H2. 一些答案是关于persistence.xml和/或H2的配置文件。 Well, I don't have those and neither does that tutorial and yet his works. 好吧,我没有这些,那个教程也没有,但是他的作品。

I'm finding that some of the most seemingly simple things are terribly frustrating with Spring and I'm sick of looking around and finding the same answer which doesn't work. 我发现一些看起来最简单的事情使Spring感到非常沮丧,而我厌倦了环顾四周并找到无法解决的相同答案。

Can someone shed some light on why this would fail? 有人可以阐明为什么会失败吗?

I can't imagine what else I would need aside from my pom.xml since the tutorial simply adds import.sql and like everyone else claims - it just works. 我无法想象除了pom.xml之外我还需要什么,因为本教程只是添加了import.sql并像其他人一样宣称它就可以了。 I will add more if needed. 如果需要,我会添加更多。

pom.xml 的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.test</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <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>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

import.sql import.sql

INSERT INTO SPEAKER(ID, FIRST_NAME, LAST_NAME, TWITTER, BIO) VALUES (0, 'Foo', 'Baz', 'foobaz', 'Foo Baz hates Twitter');
INSERT INTO SPEAKER(ID, FIRST_NAME, LAST_NAME, TWITTER, BIO) VALUES (1, 'Bar', 'Baz', 'barbaz', 'Bar Baz hates Twitter too');
INSERT INTO SPEAKER(ID, FIRST_NAME, LAST_NAME, TWITTER, BIO) VALUES (2, 'Santa', 'Clause', 'saintnick', 'Santa is a Twitter champ');

I found the answer in a minor but important detail with a little more careful investigation. 通过更仔细的调查,我以较小但重要的细节找到了答案。 Apparently, when the console started it added default values to the form entries and one differed from the tutorial's. 显然,当控制台启动时,它将默认值添加到了表单条目中,而默认值与本教程的有所不同。

For the JDBC URL the default was jdbc:h2:~/test . 对于JDBC URL ,默认jdbc:h2:~/test

在此处输入图片说明

I had to change it to jdbc:h2:mem:testdb . 我不得不将其更改为jdbc:h2:mem:testdb

在此处输入图片说明

I'm now able to see the Speaker table and data. 现在,我可以查看“ Speaker表和数据。

Once I made the change it stayed as the default. 进行更改后,它将保留为默认设置。 I suppose the author had already done that and I missed the difference. 我想作者已经做到了,而我错过了区别。

Thanks for your help @M. 感谢您的帮助@M。 Deinum! Deinum!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM