java(Springboot)のh2データベースについて

イメージ説明
h2データベースがうまく動きません。
・エクリプス
・言語→java
・フレームワーク→SpringBoot
・ビルドツール→ gradle
です。

h2データベースに接続してWebサイトを作成したいのですが、schema.sqlとdata.sqlがうまく動いてくれません。

起動時にschemaで定義したクリエイト文が実行されるはずなのですが、実行されません。h2コンソールを立ち上げてもテーブルが追加されていません。
h2コンソールと接続自体はできています。

schemaとdataのファイルパスの場所が違うのでしょうか?どなたかアドバイスお願いします。
//application.properties
spring.application.name=Book
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.show-sql=true

mybatis.config-location=classpath:mybatis-config.xml

spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:px_test;MODE=MYSQL
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.initialization-mode=always
spring.datasource.schema=classpath:database/schema.sql
spring.datasource.data=classpath:database/data.sql
spring.datasource.platform=h2
spring.h2.console.path=/h2-console

//build.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '3.3.0'
id 'io.spring.dependency-management' version '1.1.5'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '21'
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}

repositories {
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.0'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.h2database:h2'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

tasks.named('test') {
useJUnitPlatform()
}

//data.sql
INSERT INTO author (name, birthdate) VALUES ('Haruki Murakami', '1949-01-12');
INSERT INTO author (name, birthdate) VALUES ('J.K. Rowling', '1965-07-31');
INSERT INTO author (name, birthdate) VALUES ('George Orwell', '1903-06-25');
INSERT INTO author (name, birthdate) VALUES ('Agatha Christie', '1890-09-15');
INSERT INTO author (name, birthdate) VALUES ('J.R.R. Tolkien', '1892-01-03');

//schema.sql
-- Create the author table
CREATE TABLE IF NOT EXISTS author (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
birthdate DATE
);

コメントを投稿

0 コメント