gpt4 book ai didi

java - 每当检查连接时就会运行 schema.sql

转载 作者:行者123 更新时间:2023-12-01 16:46:24 25 4
gpt4 key购买 nike

我遇到了一个奇怪的问题。我的 springboot 应用程序中有一个 schema.sql 文件,它创建表(具有外键约束)并在应用程序运行时插入数据。但这里的问题是,当连接空闲时,然后对连接进行测试。现在,这给我带来了问题,因为再次执行 schema.sql 文件,并且您知道我的表包含外键约束,因此脚本失败,因为它不允许删除包含外键约束的表外键约束。

是因为我的application.properties吗?

这是我的application.properties

spring.datasource.url=jdbc:mysql://dummy_url
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.initialization-mode=always
mybatis.config-location=classpath:/config/mybatis-config.xml

有没有办法让 schema.sql 只执行一次(仅在应用程序运行时)?我的示例 schema.sql 如下所示:

DROP TABLE IF EXISTS `t001_map_mst`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `t001_map_mst` (
`MAP_ID` int NOT NULL AUTO_INCREMENT,
`MAP_NAME` varchar(45) DEFAULT NULL,
PRIMARY KEY (`MAP_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci COMMENT='Contains map info';
/*!40101 SET character_set_client = @saved_cs_client */;

最佳答案

因为您使用的是 spring.datasource.initialization-mode=always,所以此属性具有以下值:

ALWAYS Always initialize the data source.
EMBEDDED Only initialize an embedded data source.
NEVER Do not initialize the data source.
check official document

我提供了三种方法来解决这个问题1.更改您的schema.sql,如果表存在则不要运行脚本2. 第一次运行完成后,更改 application.properties,将其更改为 spring.datasource.initialization-mode=none
3.从代码初始化:检查Spring Boot - Loading Initial Data

关于java - 每当检查连接时就会运行 schema.sql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61766949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com