如何在 Spring Boot 应用程序启动时通过 schema.sql 避免或忽略 SQLExceptions [英] How to avoid or ignore SQLExceptions by schema.sql at Spring Boot app startup

查看:51
本文介绍了如何在 Spring Boot 应用程序启动时通过 schema.sql 避免或忽略 SQLExceptions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Spring Boot 应用程序,它有一个嵌入式内存 Derby DB,schema.sql 在启动时由 Spring Boot 执行.

I have a Spring Boot application that has an embedded in-memory Derby DB and schema.sql is executed by Spring Boot at startup.

schema.sql:

schema.sql:

create schema demo;
create table users (id varchar(10) primary key, name varchar(30), password varchar(30), mail varchar(100));
insert into users (id, name) values ('admin','admin');

我想引入 spring-boot-devtools 来自动重新加载我的应用程序中的更改,因此我将依赖项添加到 pom.xml.

I wanted to introduce spring-boot-devtools to automatically reload changes in my application, so I added the dependency to pom.xml.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>

但是,上述 SQL 会导致 SQLExceptions 并且无法重新加载,因为架构和表已经存在.

However, the above SQLs causes SQLExceptions and fails to reload because the schema and table has already exist.

所以我尝试添加以下 SQL,但是 if exists 子句不能在 Derby 中使用.

So I tried to add the following SQLs, but if existsclause cannot be used in Derby.

drop schema if exists demo;
drop table if exists users;

谁能告诉我解决方案(替代 SQL 或如何忽略 SQL 异常)?

Could anyone tell me the solution (alternative SQLs or how to ignore the SQL exceptions)?

推荐答案

我可以通过添加以下属性来修复:

I can fix by adding the following property:

spring.datasource.continue-on-error=true

它可以忽略 SQLExceptions 并重新加载我的应用程序中的更改.

It can ignore SQLExceptions and reload changes in my application.

这篇关于如何在 Spring Boot 应用程序启动时通过 schema.sql 避免或忽略 SQLExceptions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆