■ 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
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 **
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
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
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
Open docker-compose.yml and start it
There is no problem if the app and db containers are running
Caution The app takes time to start for the first time, so wait about 3 to 5 minutes. It will start after a while
Let's access http: // localhost /
It will be completed when the user list is displayed
There is Database in the menu on the right, so press + Data Source > MySQL
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
Since the DB table is displayed Let's try adding a record
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