Make a simple CRUD with SpringBoot + JPA + Thymeleaf ① ~ Hello World ~

Contents

--Create a simple CRUD app with Spring Boot ――This time, make a template and check the operation

Creating a template

Use Spring Initializr

-You can drop the zip at here, but this time it is created from the STS plugin. --For Eclipse The STS plugin can be downloaded from the Eclipse marketplace in the help

procedure

--Creating a template

スクリーンショット 2017-10-25 0.47.15.png

--Since it is a sample, the app name can be anything.

スクリーンショット 2017-10-25 1.10.13.png

--Both maven and gradle are often used as build tools, but this time I chose maven ――It doesn't matter which one you choose in the range you make this time

スクリーンショット 2017-10-25 0.49.26.png

――If you select the required library, you can create a template in the bundled state. --This time, select the following 5 (can be added later) --Devtools ・ ・ ・ You will be able to do hot deployment --JPA ・ ・ ・ ORMapper that connects Java and DB --H2 ・ ・ ・ In-memory DB --Thymeleaf ・ ・ ・ Template engine --Web ・ ・ ・ You can easily create an endpoint (URL)

Launch the app

――When you finish generating the template, first check the operation --It is convenient to use the Boot dashboard to start the application.

スクリーンショット 2017-10-25 0.57.49.png

--If the Boot dashboard is not displayed, you can add it by following the steps below.

スクリーンショット 2017-10-25 22.03.08.png スクリーンショット 2017-10-25 22.05.08.png

--When you start it, the output like this is output to the console

スクリーンショット 2017-10-25 0.59.09.png

--If it says started at the end, the startup is completed normally. --You can also start the app on the terminal with the following command - mvn spring-boot:run

Check the file generated by the template

BaseballApplication.java

src/main/java/com/example/baseball/BaseballApplication.java


package com.example.baseball;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BaseballApplication {

    public static void main(String[] args) {
        SpringApplication.run(BaseballApplication.class, args);
    }
}

--This main method is the entry point of the app and launches the Spring app.

application.properties

src/main/resources/application.properties



--Empty file at this point --Describe settings related to the app

BaseballApplicationTests.java

src/test/java/com/example/baseball/BaseballApplicationTests.java


package com.example.baseball;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class BaseballApplicationTests {
    @Test
    public void contextLoads() {
    }
}

--Test code template --Tests can be run from the menu below

スクリーンショット 2017-10-25 1.26.03.png

Check Hello World from your browser

--Hello World is displayed when accessing http: // localhost: 8080 /

procedure

--Two files to create --The first is a file that receives access from the user and returns a template --The second is a template to return to the user

HelloWorld.java --Create HelloWorld.java in com.example.baseball

src/main/java/com/example/baseball/HelloWorld.java


package com.example.baseball;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller // ①
public class HelloWorld {
    @GetMapping("/") // ②
    public String hello() {
        return "hello"; // ③
    }
}

-①: By adding @Controller, this Class can receive access from the user. -②: If you set @GetMapping ("/ "), this method will be called when the http method is GET and the URL is /. -The path in () represents the content following http: // localhost: 8080 --For example, @GetMapping ("/ users ") will be mapped to http: // localhost: 8080 / users. -③: Returns hello.html undersrc / main / resources / templates /to the user --The reason why it behaves like that even though it just returns a String is because this Class has @Controller.

hello.html

--Create hello.html insrc / main / resources / templates /

src/main/resources/templates/hello.html


<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>baseball</title>
  </head>
  <body>
    <h1>HelloWorld</h1>
  </body>
</html>

Operation check

--When you access http: // localhost: 8080 /, the following screen will be displayed.

スクリーンショット 2017-10-25 1.50.09.png

Hot deploy

--If you add / modify a file while the app is running, the app will be restarted automatically every time the changes are saved. --Because I put devtools when generating the template, it works like this

hotreload2.gif

next time

--Continued here

Recommended Posts

Make a simple CRUD with SpringBoot + JPA + Thymeleaf ① ~ Hello World ~
Make a simple CRUD with SpringBoot + JPA + Thymeleaf ⑤ ~ Template standardization ~
Create a simple CRUD with SpringBoot + JPA + Thymeleaf ③ ~ Add Validation ~
Create a simple CRUD with SpringBoot + JPA + Thymeleaf ④ ~ Customize error messages ~
Show a simple Hello World with SpringBoot + IntelliJ
Create a simple CRUD with SpringBoot + JPA + Thymeleaf ② ~ Screen and function creation ~
Hello World with SpringBoot / Gradle
Create a simple DRUD application with Java + SpringBoot + Gradle + thymeleaf (1)
Hello world with Java template engine Thymeleaf
[docker] [nginx] Make a simple ALB with nginx
04. I made a front end with SpringBoot + Thymeleaf
Hello World with Micronaut
A simple CRUD app made with Nuxt / Laravel (Docker)
I wrote a CRUD test with SpringBoot + MyBatis + DBUnit (Part 1)
Hello World with Spring Boot
Hello World with Spring Boot!
Hello World with VS Code!
Create a Hello World web app with Spring framework + Jetty
Let's make a simple API with EC2 + RDS + Spring boot ①
Hello World with Spring Boot
Hello, World! With Asakusa Framework!
[Swift] Create a project with Xcode (ver 12.1) and display "Hello, World!"
Try Hello World with the minimum configuration of Heroku Java spring-boot
Make a digging maze with Ruby2D
Make a language! (Making a simple calculator ②)
Try to make a simple callback
Implement text link with Springboot + Thymeleaf
Make a slideshow tool with JavaFX
Make a Christmas tree with swift
Hello world with Kotlin and JavaFX
Make a garbage reminder with line-bot-sdk-java
(Intellij) Hello World with Spring Boot
Make a list map with LazyMap
Make a language! (Making a simple calculator ①)
Hello World with GlassFish 5.1 + Servlet + JSP
Create PDF with itext7 ~ Hello World ~
"Hello world" for ImageJ with Eclipse
Hello World with GWT 2.8.2 and Maven
Make a typing game with ruby
Try writing "Hello, World" with a combination of various languages and libraries
Let's create a TODO application in Java 2 I want to create a template with Spring Initializr and make a Hello world
Let's make a Christmas card with Processing!
Make a family todo list with Sinatra
Hello World with Eclipse + Spring Boot + Maven
Create a simple on-demand batch with Spring Batch
[Rails withdrawal] Create a simple withdrawal function with rails
Create a simple bar chart with MPAndroidChart
Make a family todo list with Sinatra
Let's make a smart home with Ruby!
Java development with Codenvy: Hello World! Run
Make a login function with Rails anyway
"Hello, World!" With Kotlin + CLI in 5 minutes
Hello world with Kotlin and Tornado FX
Make a site template easily with Rails
Deserialize XML into a collection with spring-boot
Implement CRUD with Spring Boot + Thymeleaf + MySQL
Implement simple CRUD with Go + MySQL + Docker
How Spring Security works with Hello World
(IntelliJ + gradle) Hello World with Spring Boot
I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished)
Hello World, a cross-platform GUI app with Groovy running on the Java platform