IntelliJ + Docker (APP + DB) + SpringBoot (Maven) environment construction

Premise

■ JAVA is installed This time I'm using Java SE 14 https://www.oracle.com/java/technologies/javase-downloads.html

■ IntelliJ is installed [Official download IntelliJ IDEA] https://www.jetbrains.com/ja-jp/idea/download/#section=windows

■ Docker is installed See below for how to build [Build Docker for free on Windows 10] https://qiita.com/SSM3G/items/79b7becc2169aac8ec6f

Target

While using IntelliJ Build application server and database server with Docker Aim to display the contents of the database using sample projects and sources

** Access http: // localhost / and display the screen below ** screenshot.47.jpg

Create Spring Boot project --Spring Initializr -

https://start.spring.io/ Select what you need to create a project at the above URL You can easily create it by GENERATE Dependencies can be added from pom.xml later by adding something that seems to be used for the time being screenshot.41.jpg

You can drop it as a zip file, so unzip it and store it in an appropriate directory.

IntelliJ

Open the file created earlier from "Open or Import"

Create docker-compose.yml under "demo"

** Addition ** If you don't understand a single line of what is written in docker-compose.yml, click here [Somehow understand docker-compose.yml] https://qiita.com/SSM3G/items/4e5fc3e880fff0de3bfe

docker-compose.yml


version: '3'
services:
  app:
    image: openjdk:14-jdk-alpine
    ports:
      - "80:8080"
    volumes:
      - .:/app
    working_dir: /app
    command: ./mvnw spring-boot:run
    environment:
      DATASOURCE: jdbc:mysql://db:3306/sample
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: sample
      MYSQL_USER: user
      MYSQL_PASSWORD: passwrod
    command: mysqld --character-set-server=utf8 --collation-server=utf8_unicode_ci
    volumes:
      - ./docker/mysql:/docker-entrypoint-initdb.d
    ports:
      - "3306:3306"

src\main\resources\application.properties Write the settings for connecting to the DB in This time, make a DB connection using JDBC

application.properties


spring.datasource.url=${DATASOURCE:jdbc:mysql://localhost:3306/sample}
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

Next, create sample data Create docker \ mysql \ sample.sql under "demo"

sample.sql


SET CHARSET UTF8;
create table sample.users(
  id bigint(20) unsigned AUTO_INCREMENT PRIMARY KEY,
  name varchar(100),
  age tinyint,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME
);
INSERT INTO sample.users(name,age) VALUES ('Taro',30),('Jiro',25),('Saburo',19);

After that, create the familiar Controller and HTML src\main\java\com\example\demo\controller\AppController.java

AppController


package com.example.demo.controller;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

@Controller
public class AppController {
    final
    JdbcTemplate jdbcTemplate;

    public AppController(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @RequestMapping("/")
    String index(Model model) {
        List<Map<String, Object>> users = jdbcTemplate.queryForList("select * from sample.users");
        model.addAttribute("title", "User list");
        model.addAttribute("users", users);
        return "index";
    }
}

src\main\resources\templates\index.html

index.html


<!DOCTYPE html>
<html lang="ja" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        table {
            border-collapse: collapse;
        }

        th, td {
            padding: 3px 5px 3px 5px;
            border: 1px solid #aaa;
        }

        th {
            background-color: #eee;
        }
    </style>
</head>
<body>
<h1 th:text="${title}"></h1>
<table>
    <thead>
    <tr>
        <th>#</th>
        <th>name</th>
        <th>age</th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="user: ${users}">
        <td th:text="${user['id']}"></td>
        <td th:text="${user['name']}"></td>
        <td th:text="${user['age']}"></td>
    </tr>
    </tbody>
</table>
</body>
</html>

Docker Next, prepare for Docker If you can successfully install Docker Since the Docker icon is displayed at the bottom right, open the Setting screen Expose daemon on tcp://localhost:2375 without TLS Check to change without TLC screenshot.48.jpg

Open the Setting screen in IntelliJ Ctrl + Alt + S Install "Docker" with Plugins Then, the Docker item will be displayed on the Setting screen. Press + to add Docker and you're done

Launch Docker from Services in the bottom menu of IntelliJ I think it will start if there is no problem screenshot.51.jpg

Open docker-compose.yml and start it screenshot.62.jpg

There is no problem if the app and db containers are running screenshot.63.jpg screenshot.64.jpg

Caution The app takes time to start for the first time, so wait about 3 to 5 minutes. It will start after a while screenshot.56.jpg

Operation check

Let's access http: // localhost / It will be completed when the user list is displayed

Perform DB operation from IntelliJ

There is Database in the menu on the right, so press + Data Source > MySQL screenshot.66.jpg

screenshot.67.jpg

Fill in as shown in the image above User:root Password:password

docker-compose.yml


MYSQL_ROOT_PASSWORD: password

Since it is set as, it will be the above value. Edit it later as you like

Don't forget to select Schemas screenshot.68.jpg

Since the DB table is displayed Let's try adding a record screenshot.69.jpg screenshot.70.jpg screenshot.71.jpg screenshot.72.jpg

If you access the localhost and it's updated, you're done! Thank you for your hard work \ (^ o ^) / After that, customize it as you like and deepen your knowledge.

Recommended Posts

IntelliJ + Docker (APP + DB) + SpringBoot (Maven) environment construction
Docker environment construction
Rails Docker environment construction
Development environment construction using IntelliJ IDEA + Maven + Tomcat 9
MySQL 5.7 (Docker) environment construction memo
Redmine (Docker) environment construction memo
[Docker] Rails 5.2 environment construction with docker
Docker × Spring Boot environment construction
[Docker] postgres, pgadmin4 environment construction
React environment construction with Docker
[Portfolio] Manage site with laravel APP implementation (Docker environment construction)
Rails + MySQL environment construction with Docker
Node.js environment construction with Docker Compose
Environment construction with Docker for beginners
Laravel + Docker Laradock usage environment construction
Rails on Docker environment construction procedure
[Environment construction with Docker] Rails 6 & MySQL 8
[Java & SpringBoot] Environment Construction for Mac
[Note] Docker version Db2 environment acquisition procedure
SQL statement learning ~ Environment construction ~ Docker + MySQL
Rails environment construction with Docker (personal apocalypse)
Laravel development environment construction with Docker (Mac)
Sapper × Go (echo) × Docker development environment construction
Docker Intellij
Environment construction with Docker (Ubuntu20.04) + Laravel + nginx
[Java] Connection with local DB (IntelliJ + SpringBoot)
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
Spring Boot + Docker Java development environment construction
Laravel + MySQL + phpMyadmin environment construction with Docker
Spring Boot environment construction with Docker (January 2021 version)
Docker + DynamoDB local + C ++ environment construction and practice
Make SpringBoot1.5 + Gradle4.4 + Java8 + Docker environment compatible with Java11
Environment construction command memo with Docker on AWS
Kaggle environment construction using official Docker and vscode
Rails6 [API mode] + MySQL5.7 environment construction with Docker
DB environment construction with DBFlute Intro + H2 Database
A reminder of Docker and development environment construction
React + Django + Nginx + MySQL environment construction with Docker
Wordpress local environment construction & development procedure with Docker
[Java] Environment construction
[Spring] Environment construction
Docker + Spring-boot start
How to build Docker + Springboot app (for basic learning)
BEAR application Docker development environment construction example (docker-sync, Mutagen)
virtulbox + vagrant + Docker + nginx + puma + MySQL Rails environment construction
Database environment construction with Docker in Spring boot (IntellJ)
Construction of data analysis environment using Docker (personal memorandum)