CrudRepository 未从 schema.sql 读取数据

CrudRepository not reading data from schema.sql

设置: 我有一个 spring-boot 应用程序,带有一个简单的@Entity Customer 对象和 CustomerRepository。我想用 here in my other question 描述的测试数据预加载数据库,所以我创建了 schema.sql 和 data.sql 文件来加载数据库。

问题: CrudRepository 似乎使用了与 schema.sql 和 data.sql 创建的数据库不同的数据库。我没有在任何地方明确定义数据源,因为我希望 spring-boot 可以为我默认所有内容(即没有在 application.properties 中定义 spring.datasource),即使我这样做了它什么都不做。

@Autowired
CustomerRepository r;
r.findAll(); // nothing but it should return the row "John Doe"

当我在存储库上调用 findAll() 时,我没有收到任何错误,只是 returns 什么都没有。

schema.sql

drop table customer if exists;

create table customer (
    id bigint auto_increment,
    firstname varchar(80) null,
    lastname varchar(80) null
);

data.sql

insert into customer (firstname, lastname) values ('John', 'Doe');

Customer.java

package sample.jsp;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
public class Customer implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;

    private String firstName;
    private String lastName;

    protected Customer() {}

    public Customer(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return String.format(
                "Customer[id=%d, firstName='%s', lastName='%s']",
                id, firstName, lastName);
    }

}

CustomerRepository.java

package sample.jsp;

import java.util.List;

import org.springframework.data.repository.CrudRepository;

public interface CustomerRepository extends CrudRepository<Customer, Long> {

    List<Customer> findByLastName(String lastName);
    List<Customer> findAll();
}

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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.8.RELEASE</version>
    </parent>
    <artifactId>TestApp</artifactId>
    <packaging>war</packaging>
    <name>Spring Boot Web JSP Sample</name>
    <description>Spring Boot Web JSP Sample</description>
    <url>http://projects.spring.io/spring-boot/</url>
    <organization>
        <name>Pivotal Software, Inc.</name>
        <url>http://www.spring.io</url>
    </organization>
    <properties>
        <main.basedir>${basedir}/../..</main.basedir>
        <m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</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>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Spring 引导文档的这一部分描述了它是如何工作的:

http://docs.spring.io/spring-boot/docs/1.2.2.RELEASE/reference/htmlsingle/#howto-intialize-a-database-using-spring-jdbc

特别是您必须确保两个 sql 文件都在类路径的根目录中。或者,您可以在 application.properties 中指定(即使只是为了查看它是否以这种方式工作)确切的 location/filename。

spring.datasource.schema=file:scripts/my-schema.sql

data.sql 的名称更改为 import.sql

Spring Boot Database Initialization

我遇到了同样的问题并设法解决了。

问题不在于 SQL 文件位置(日志显示脚本执行)。

It was the default behavior of DDL generation of Springboot with In-memory DB like H2 以及 SQL 文件在 DDL 生成之前执行的事实(内存数据库默认为 'create-drop')。

在 application.properties 中添加这一行:

spring.jpa.hibernate.ddl-auto=validate

你会好起来的。

我遇到了类似的问题,并且成功解决了。

为了让它工作,我必须为我需要测试数据的每个测试方法添加这个注释:

@Sql("classpath:data.sql")

我正在使用 spring-boot-starter-data-jpa 和 H2。在我的例子中,我 运行 一个依赖于一些初始数据的测试(在 data.sql 中创建)。

当我 运行 测试时,模式创建正确,但就像你的情况一样,CrudRepository 似乎使用不同的数据)。

注意:我知道回答这个问题可能会晚了,但我希望这对遇到同样问题的人有所帮助。