0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

タスク管理システム #3(DB接続(MongoDB))

Posted at

1. MongoDBのインストール

1. MongoDB公式サイトへアクセス

2. Community Editionを選択し、インストーラーをダウンロード

3. インストーラーを実行し、以下の設定を選択

・MongoDB Serverをインストール
・MongoDB Compass(GUIツール)をインストール(任意)

4. 環境変数を設定

インストール時に自動設定されるが、念のためパスを確認
C:\Program Files\MongoDB\Server\8.0\bin

2. DockerでMongoDBをセットアップ

1. docker-compose.ymlにMongoDBを追加

以下のように記載

networks:
  app-network:
    driver: bridge
services:
  backend:
    build: ./backend
    ports:
      - "8080:8080"
    environment:
      SPRING_PROFILES_ACTIVE: ${SPRING_PROFILE}
      SPRING_DATA_MONGODB_URI: "mongodb://admin:password@mongo:27017/taskmanager"

    depends_on:
      mongo:
        condition: service_healthy
    networks:
      - app-network
    restart: always

  frontend:
    build: ./frontend
    ports:
      - "3000:3000"
    volumes:
      - ./src:/app/src  # ローカルのsrcをコンテナの/app/srcにマウント
    depends_on:
      - backend
    networks:
      - app-network
    restart: always

  mongo:
    image: mongo:latest
    ports:
      - "27017:27017"
    volumes:
      - mongo_data:/data/db
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_USERNAME}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_PASSWORD}
    networks:
      - app-network
    restart: always
    healthcheck:
      test: ["CMD", "mongosh", "--eval", "db.runCommand({ ping: 1 })"]
      interval: 10s
      timeout: 5s
      retries: 5


volumes:
  mongo_data:

この設定で、mongodbというサービスがDockerコンテナ内に作成され、27017ポートでアクセス可能になります。

3.Gradleの設定

backend/とbackemd/app/にあるbuild.gradle.ktsを以下のように記載

backend/build.gradle.kts

plugins {
    id("org.springframework.boot") version "3.2.2"
    id("io.spring.dependency-management") version "1.1.4"
    kotlin("jvm") version "2.1.0"
    kotlin("plugin.spring") version "2.1.0"
    application
}

repositories {
    mavenCentral()
}

dependencies {
    implementation(kotlin("stdlib"))

    // Spring Boot Essentials
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.boot:spring-boot-starter-data-mongodb")

    // Test
    testImplementation("org.jetbrains.kotlin:kotlin-test")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
}

java {
    toolchain {
        languageVersion.set(JavaLanguageVersion.of(21))
    }
}

tasks.withType<Test>().configureEach {
    useJUnitPlatform()
}

application {
    mainClass.set("org.example.TaskManagerApplication")
}

backend/app/build.gradle.kts

plugins {
    id("org.springframework.boot") version "3.2.2"
    id("io.spring.dependency-management") version "1.1.4"
    kotlin("jvm") version "2.1.0"
    kotlin("plugin.spring") version "2.1.0"
    application
}

repositories {
    mavenCentral()
}

dependencies {
    // Kotlin Standard Library
    implementation(kotlin("stdlib"))
    implementation("org.jetbrains.kotlin:kotlin-reflect")

    // Spring Boot Core
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.boot:spring-boot-starter-data-mongodb")

    // MongoDB with KMongo
    implementation("org.litote.kmongo:kmongo:4.9.0")

    // Unit Testing
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
}

java {
    toolchain {
        languageVersion.set(JavaLanguageVersion.of(21))
    }
}

tasks.withType<Test>().configureEach {
    useJUnitPlatform()
}

4. 接続テスト

1. 接続テストコードの配置

backend/app/src/test/kotlin/にMongoConnectionTest.ktというファイルを作成し以下を記載

package com.example.mongodb

import com.mongodb.client.MongoClients
import org.junit.jupiter.api.Test
import kotlin.test.assertNotNull

class MongoConnectionTest {
    @Test
    fun testMongoConnection() {
        val client = MongoClients.create("mongodb://admin:password@mongodb:27017")
        val database = client.getDatabase("task_management")
        assertNotNull(database, "MongoDBへの接続に失敗しました")
        println("MongoDBに接続成功: ${database.name}")
    }
}

2. 接続テストの実行

backend/app/で以下コマンドを実行

gradlew test

3. テストレポートの確認

build/reports/tests/test/index.htmlを確認

以上になります。
今後はもう少し、各記載を掘り下げた記事を書いていこうと思います。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?