在Spring Boot应用程序中配置嵌入式Derby

Jac*_*b J 2 derby spring-boot

您能帮我在Spring Boot应用程序中建立与嵌入式Derby数据库的连接吗?

我在网上搜索,但只能找到服务器类型Derby的解决方案,而不能找到嵌入式Derby的解决方案。

spring.jpa.database = ?
spring.jpa.hibernate.ddl-auto = create-drop
Run Code Online (Sandbox Code Playgroud)

Sha*_*mud 6

Derby作为内存数据库

如果您想使用Spring Boot 配置内存中的 Derby数据库,您需要做的最简单的事情就是将运行时依赖项(以及spring-boot-starter-data-jpa)提到为

<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derby</artifactId>
    <scope>runtime</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

最低限度的最低配置应适用于Spring Boot中的任何嵌入式数据源。但是从当前的Spring Boot版本(2.0.4.RELEASE)开始,这仅对Derby导致错误

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing
DDL via JDBC Statement
Caused by: java.sql.SQLSyntaxErrorException: Schema 'SA' does not exist
Run Code Online (Sandbox Code Playgroud)

发生这种情况是由于默认配置,spring.jpa.hibernate.ddl-auto=create-drop 请参阅此春季启动问题以获取详细信息。

因此,您需要在application.propertiesas中覆盖该属性

spring.jpa.hibernate.ddl-auto=update
Run Code Online (Sandbox Code Playgroud)

Derby作为持久数据库

如果要使用Derby作为永久数据库。添加这些应用程序属性

spring.datasource.url=jdbc:derby:mydb;create=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.DerbyTenSevenDialect
spring.jpa.hibernate.ddl-auto=update
Run Code Online (Sandbox Code Playgroud)


jam*_*iss -3

如果您使用 Spring Boot,则不需要连接属性。只需将这些添加到您的 POM 中:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

然后添加常用的控制器、服务和存储库类。