我知道H2是一个内存中的数据库。我看了一个关于Mockito的(有点老)课程的视频,其中使用H2 DB作为说明。在视频中,我看到了H2 sql语句跟踪创建表,并在Eclipse IDE控制台中插入行。但是,我在IntelliJ IDE中没有找到任何类似的跟踪。是否有自动插入数据的设置?
还是每次都要手动插入数据。
此外,当他们在存储库上运行JUnit时,他们似乎没有运行SpringBoot服务器,我很困惑-DB是否存在于IDE之外?
我阅读了更多的功能,并尝试将URL更改为
spring.datasource.url=jdbc:h2:mem:testdb;INIT=RUNSCRIPT FROM '~/Documents/Projects/regular-encourager/encourage/src/main/resources/data.sql'
但我得到了错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #2 of URL [file:/C:/Users/Me/Documents/Projects/regular-encourager/encourage/target/classes/data.sql]: CREATE TYPE Category AS ENUM('WhoIAmInChrist','Default'); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Domain "CATEGORY" already exists; SQL statement:
CREATE TYPE Category AS ENUM('WhoIAmInChrist','Default') [90119-214]
当我注解掉CREATEEnum语句时,我得到了一个未知类型的错误。
CREATE TYPE Category AS ENUM('WhoIAmInChrist','Default');
CREATE TYPE Tone AS ENUM('Default', 'Uplifting', 'Urging', 'Warning', 'Soothing', 'Comforting', 'Inspiring', 'Centering', 'Balanced');
CREATE TYPE Topic AS ENUM('Default', 'AcceptedInChrist', 'SignificantInChrist', 'SecureInChrist', 'NoAnxietyInChrist');
data.sql为:
drop table ENCOURAGEMENT if exists;
CREATE TYPE Category AS ENUM('WhoIAmInChrist','Default');
CREATE TYPE Tone AS ENUM('Default', 'Uplifting', 'Urging', 'Warning', 'Soothing', 'Comforting', 'Inspiring', 'Centering', 'Balanced');
CREATE TYPE Topic AS ENUM('Default', 'AcceptedInChrist', 'SignificantInChrist', 'SecureInChrist', 'NoAnxietyInChrist');
CREATE TABLE Encouragement(ID INT PRIMARY KEY, CATEGORY Category, TOPIC Topic, TONE Tone, MESSAGE VARCHAR(512));
INSERT INTO Encouragement VALUES(-1, 'Default', 'Default', 'Default', 'We walk by Faith, not by Sight');
INSERT INTO Encouragement VALUES(0,'WhoIAmInChrist','AcceptedInChrist','Uplifting','John 1:12 I am God''s child.');
INSERT INTO Encouragement VALUES(1,'WhoIAmInChrist','AcceptedInChrist','Uplifting','John 15:15 As a disciple, I am a friend of Jesus Christ.');
1条答案
按热度按时间laximzn51#
| H2模式|连接的应用程序|持续|
| - ------| - ------| - ------|
| 内存中|1个|❌|
| 文件持久性|1个|✅|
| 服务器模式|多重|✅|
H2可以在多种模式下工作:内存中、文件持久性和服务器中。请参见H2 Cheat Sheet:
最后,还可以提供一个SQL脚本,以便在启动H2时运行,这样,表和数据在应用启动时就已经存在了。