Edited at

JavaのORMapper「DBflute」とDocker「Mysql 」を用いた環境構築


概要

JavaのORMapper DBflute をDocker Mysql と組み合わせて使うときの環境設定方法をまとめる。


Mysqlイメージの作成


docker-compose.ymlの作成

例として以下の docker-compose.ymlDockerfile でイメージを作成してみる

※ 全体感はサンプルリポジトリ参照


docker-compose.yml

version: "3.6"

services:
mysql:
build: mysql
ports:
- "{ホスト側のポート番号}:3306"
environment:
MYSQL_DATABASE: {データベース名}
MYSQL_USER: {ユーザー名}
MYSQL_PASSWORD: {パスワード}


コンテナの起動

$ cd {docker-compose.yml のディレクトリ}

$ docker build
$ docker-compose up -d


DBflute側の設定


データベース情報の設定

databaseInfo.Mapの設定


databaseInfoMap.dfprop

map:{

; driver = com.mysql.jdbc.Driver
; url = jdbc:mysql://127.0.0.1:{ホスト側のポート番号}/{データベース名}
; schema =
; user = {ユーザー名}
; password = {パスワード}

...(略)
}



データの初期化

$ cd {dbflute_XXXX のディレクトリ}

$ sh manage.sh 0
/nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
...Calling the ReplaceSchema task
nnnnnnnnnn/
Buildfile: ../mydbflute/dbflute-1.x/build-torque.xml

replace-schema:
[df-replace-schema] templates
[df-replace-schema] 2019-01-10 23:37:26,268 INFO - +------------------------------------------+
[df-replace-schema] 2019-01-10 23:37:26,280 INFO - | |
[df-replace-schema] 2019-01-10 23:37:26,281 INFO - | ReplaceSchema |
[df-replace-schema] 2019-01-10 23:37:26,318 INFO - | |
[df-replace-schema] 2019-01-10 23:37:26,318 INFO - +------------------------------------------+
[df-replace-schema] 2019-01-10 23:37:26,319 INFO - ...Waiting for your GO SIGN from stdin before ReplaceSchema:
[df-replace-schema] /- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[df-replace-schema] Database: jdbc:mysql://127.0.0.1:3309/hakiba_db
[df-replace-schema] Schema: hakiba_db
[df-replace-schema] - - - - - - - - - -/
[df-replace-schema] ReplaceSchema will
[df-replace-schema]
[df-replace-schema] DDD EEEEE L EEEEE TTTTT EEEEE
[df-replace-schema] D D E L E T E
[df-replace-schema] D D E L E T E
[df-replace-schema] D D EEEEE L EEEEE T EEEEE
[df-replace-schema] D D E L E T E
[df-replace-schema] D D E L E T E
[df-replace-schema] DDD EEEEE LLLLL EEEEE T EEEEE your all data!
[df-replace-schema]
[df-replace-schema] <Process Flow>
[df-replace-schema] 1. initialize your schema (*droping all existing tables)
[df-replace-schema] 2. create tables as your DDL
[df-replace-schema] 3. load your test data, e.g. excel files
[df-replace-schema]
[df-replace-schema] (input on your console)
[df-replace-schema] Are you ready? (y or n):
y

...

BUILD SUCCESSFUL
Total time: 30 seconds

Mysqlに接続してデータが入っていることを確認

$ docker exec -it {コンテナ名} mysql -u {ユーザー名} -D {データベース名} -p

Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.12 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show tables;
...