【发布时间】:2021-06-21 17:06:12
【问题描述】:
我有 3 个 .sql 文件:
-
drop.sql(每次启动 webapp 时重置 DBMS)
-
schema.sql(包含 DBMS 架构)
-
data.sql(包含一些用户和一些角色的记录)
我还有 2 个 .properties 文件
-
application-sviluppo.properties(我在 PC 上使用)
-
application.properties(我将它用于 SERVER)
我想用 Spring Boot 2.5.2 更新 pom.xml,但我不知道应该如何更改我的文件。
文件 application-sviluppo.properties
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://127.1.1.1:5432/test
spring.datasource.username=postgres
spring.datasource.password=password
# This code is deprecated but it works.
spring.datasource.schema = classpath:drop.sql, classpath:schema.sql
# This code is not deprecated, it is recommended but it does not work.
#spring.sql.init.schema-locations = classpath:drop.sql, classpath:schema.sql
logging.level.org.springframework=TRACE
spring.datasource.initialization-mode=always
spring.datasource.continue-on-error=false
文件 application.properties
spring.profiles.active=sviluppo
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=???
spring.datasource.username=???
spring.datasource.password=???
logging.level.org.springframework=TRACE
spring.datasource.initialization-mode=always
spring.datasource.continue-on-error=false
我没有列出我所做的测试和尝试,因为这个页面还不够。
我在这里读到:
我在下面展示了一个非常简单且非常简短的示例,如果您将其加载到 IntelliJ 上,将会澄清问题。
项目下载链接:
问题:
# This code is deprecated but it works.
spring.datasource.schema = classpath:drop.sql, classpath:schema.sql
# This code is not deprecated, it is recommended but it does not work.
#spring.sql.init.schema-locations = classpath:drop.sql, classpath:schema.sql
DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Profile;
@SpringBootApplication
@Profile("sviluppo")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
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 https://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>2.5.2</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>gestioneutenti</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<packagingExcludes>
WEB-INF/classes/it/applicazionijava/gestioneutenti/CrittografarePassword.class,
WEB-INF/classes/application-sviluppo.properties,
WEB-INF/classes/templates/pagineapplicazione.html
</packagingExcludes>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
schema.sql
CREATE TABLE IF NOT EXISTS utenti (
id BIGSERIAL NOT NULL,
nome VARCHAR(100) NOT NULL,
password VARCHAR(255) NOT NULL,
CONSTRAINT utenti_pk PRIMARY KEY(id),
CONSTRAINT utenti_uk UNIQUE (nome)
);
CREATE TABLE IF NOT EXISTS persistent_logins (
username VARCHAR(100) NOT NULL,
series VARCHAR(64) PRIMARY KEY,
token VARCHAR(64) NOT NULL,
last_used TIMESTAMP NOT NULL
);
data.sql
BEGIN TRANSACTION;
INSERT INTO utenti (nome, password)
VALUES ('kkkkkkk', 'kkkkkkkkkkkkkkkkkkkkkkkkkk') ON CONFLICT DO NOTHING;
COMMIT;
drop.sql
DROP TABLE IF EXISTS utenti CASCADE;
【问题讨论】:
-
从你的问题中我不清楚你想要做出什么改变。如果是关于已弃用的 DataSource 初始化属性,它们在
spring.sql.init.*中有替换,如in the release notes 所述。 -
我做了很多测试,我都不记得了。我唯一无法修复的是这行代码: spring.datasource.schema = classpath: drop.sql, classpath: schema.sql 我无法在开发中加载 drop.sql 和 schema.sql,而在生产中只能加载 schema.sql (应用程序。属性)。弃用的解决方案有效(目前我已经离开了弃用的版本)。
-
spring.sql.init.schema-locations是spring.datasource.schema的替代品。 -
我认为你是对的,但如果我写“spring.sql.init.schema-locations = classpath: drop.sql, classpath: schema.sql”,我的 webapp 将无法工作。 Spring Boot 2 有一个错误。如果我使用您建议 drop.sql 文件未加载的代码。差不多一个月前我打开了这个帖子,但还没有收到解决方案。遗憾的是 Spring Boot 没有专门的论坛,也没有可以用来报告 bug 的电子邮件。
-
提供minimal, reproducible, example 是一种好方法,可以让人们更轻松地帮助您并更快地获得问题的答案。