[JAVA] Create Restapi with Spring Boot ((1) Until Run of App)

■ Create an app using Spring Initializer

SpringInitiallizer

スクリーンショット 2020-01-08 16.05.36.png スクリーンショット 2020-01-08 16.05.51.png

■ Add lombok

build.gradle


dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'mysql:mysql-connector-java'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    //Describe the lombok settings below
    compileOnly 'org.projectlombok:lombok:1.18.10'
    annotationProcessor 'org.projectlombok:lombok:1.18.10'
}

Reload gradle

スクリーンショット 2020-01-08 16.39.04.png

Open Gradle from Tool,

スクリーンショット 2020-01-08 16.39.14.png

Press the update button

Check if lombok is installed

entity/Product.java


package com.example.reviewapiapp.entity;

import lombok.Data;

@Data
public class Product {}

If you can install it, you can use @Data. If you haven't installed it, you can't use @Data.

mysql

mysql> SELECT user, host FROM mysql.user;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| hogehogehoge  | %         |
| hogehoge      | %         |
| mysql.session | localhost |
| mysql.sys     | localhost |
| root          | localhost |
+---------------+-----------+
5 rows in set (0.01 sec)

mysql> grant all on review_api_db.* to 'root'@'localhost';
Query OK, 0 rows affected (0.01 sec)

application.yml


spring:
  datasource:
    initialization-mode: always
    sql-script-encoding: UTF-8
    driver-class-name: com.mysql.cj.jdbc.Driver
  jpa:
    database: MYSQL
    hibernate:
      ddl-auto: none
  profiles:
    active: local

application-local.yml


spring:
  profiles:
    active: local
  datasource:
    url: jdbc:mysql://localhost:3306/review_api_db
    username: root
    driver-class-name: com.mysql.cj.jdbc.Driver
    schema: classpath:schema.sql

sql file

schema.sql


create table if not exists products
(
    id          bigint unsigned not null auto_increment primary key comment 'Product ID',
    title       varchar(100)    not null unique comment 'Product title',
    description varchar(500)    not null comment 'Product description',
    price       int unsigned    not null comment 'Product price',
    image_path  text comment 'Product image path',
    create_time datetime        not null default current_timestamp comment 'Creation date and time',
    update_time datetime        not null default current_timestamp on update current_timestamp comment 'Update date and time'
) comment 'Product table' engine = innodb
                   charset = utf8mb4;

You can run.

Recommended Posts

Create Restapi with Spring Boot ((1) Until Run of App)
Create an app with Spring Boot 2
Create an app with Spring Boot
Create a simple search app with Spring Boot
Run LIFF with Spring Boot
Create microservices with Spring Boot
Until "Hello World" with Spring Boot
Run WEB application with Spring Boot + Thymeleaf
Create a Spring Boot app development project with the cURL + tar command
Create a website with Spring Boot + Gradle (jdk1.8.x)
Until data acquisition with Spring Boot + MyBatis + PostgreSQL
Create CRUD apps with Spring Boot 2 + Thymeleaf + MyBatis
Create your own Utility with Thymeleaf with Spring Boot
Create Spring Boot environment with Windows + VS Code
Create a web api server with spring boot
Create a Spring Boot development environment with docker
Create Spring Cloud Config Server with security with Spring Boot 2.0
Download with Spring Boot
Create a portfolio app using Java and Spring Boot
Access the built-in h2db of spring boot with jdbcTemplate
Until you start development with Spring Boot in eclipse 1
Create a simple demo site with Spring Security with Spring Boot 2.1
How to boot by environment with Spring Boot of Maven
Until you start development with Spring Boot in eclipse 2
Hello World (console app) with Apache Camel + Spring Boot 2
Generate barcode with Spring Boot
Hello World with Spring Boot
Implement GraphQL with Spring Boot
Get started with Spring boot
Hello World with Spring Boot!
SNS login with Spring Boot
File upload with Spring Boot
Spring Boot starting with copy
Spring Boot starting with Docker
Hello World with Spring Boot
Set cookies with Spring Boot
Use Spring JDBC with Spring Boot
Add module with Spring Boot
Getting Started with Spring Boot
Send email with spring boot
A story packed with the basics of Spring Boot (solved)
Extract SQL to property file with jdbcTemplate of spring boot
Create a Hello World web app with Spring framework + Jetty
Until INSERT and SELECT to Postgres with Spring boot and thymeleaf
Flow until output table data to view with Spring Boot
Introduction of library ff4j that realizes FeatureToggle with Spring Boot
Use Basic Authentication with Spring Boot
Memorandum of understanding when Spring Boot 1.5.10 → Spring Boot 2.0.0
gRPC on Spring Boot with grpc-spring-boot-starter
Hot deploy with Spring Boot development
Database linkage with doma2 (Spring boot)
Spring Boot programming with VS Code
Inquiry application creation with Spring Boot
Going out of message (Spring boot)
Get validation results with Spring Boot
(Intellij) Hello World with Spring Boot
[Spring Boot] Role of each class
Google Cloud Platform with Spring Boot 2.0.0
Create command line app with maven
Check date correlation with Spring Boot
See the behavior of entity update with Spring Boot + Spring Data JPA