0
0

More than 3 years have passed since last update.

Spring boot H2 の基礎のアレコレ。

Last updated at Posted at 2021-08-15

H2 Database

必要な Spring Dependencies

  • H2 Database
  • JDBC API
  • Spring Web
  • Spring Boot Devtools(you shoud install if you need.)

application.properties で以下のように書くより、yml で書く方が好きなので、ここでは yml ファイルで設定を書いていきます。

// application.properties(個人的に嫌い。。。)
spring.datasource.url=~
spring.datasource.username=~
// mem(メモリ)=Springを起動するたびに初期化される。
spring:
  datasource:
    url: jdbc:h2:mem:test
    driverClassName: org.h2.Driver
    // 管理者権限
    username: sa
    password:
// Webブラウザから確認できるようにする。
  h2.console.enabled: true

この状態で、localhost:8080/h2-consoleにアクセス出来ると思います。

開発では、事前に仮想 DB としてテーブルを作成して、データを突っ込んだ上で使用するだろうから、その部分をやっていきます。

src/resouce/~
ここに SQL を作って配置するだけ〜。
今回は、2 ファイル用意。

  • scheme.sql
  • data.sql
// scheme.sql
create table music(
   id int not null auto_increment,
   bandName varchar(100) not null,
   bestSong varchar(100) not null,
   created datetime not null,
   primary key(id)
);

// data.sql
insert into music (bandName,bestSong,created) values('Queen', 'We Will Rock You', '2019-11-12 08:34:19');

snap1.jpg
snap2.jpg

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0