Spring boot2. X Quick Start Guide

Keywords: Java Spring Maven xml


Spring boot2. X Quick Start Guide (1)


  • IDE: IntelliJ IDEA 2020.3
  • Java environment jdk1.8

Quickly create spring boot project on the official website

Now let's get to the point:

Get into https://start.spring.io/ Build an initial project

Here you will download a zip project package

Using Maven to import the Spring Boot project

Remember to copy the path of the demo folder after decompressing demo.zip
The development tool used here is IntelliJ IDEA
Here is the import process:
In the IDEA, click file - > Open - > paste the just demo folder path - > find pom.xml and double-click
->Open as peoject - > wait for Maven to finish loading. Can't see the figure below

The following direct fix appears. Ignore it if it doesn't appear

Maven switching domestic source

You'll find that he's very slow to download
Or you can surf the Internet scientifically
Next, turn it off

Then reopen
Add at the bottom of pom.xml file

        <!--Alibaba cloud main warehouse, agent maven central and jcenter Warehouse-->
        <!--Alicloud agent Spring Official warehouse-->
            <name>Spring Milestones</name>
        <!--Alicloud agent Spring Plug in warehouse-->

Take off, wait for download to complete

SpringBoot project launch

Preparation before startup
Move the DemoApplication startup class to the outermost layer of the package according to the figure below
The startup class is equivalent to the person in charge of the management project. If you throw it at the same level as the control layer, there must be an error. No

The purpose is to open a web application and add it under < dependencies > in pom.xml


Modify BootDemoApplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

 * description : The @ RestController here is equivalent to @ ResponseBody+@Controller
 * Using @ RestController is equivalent to adding @ ResponseBody annotation to each method
 * @author: Robot
 * @date 0:03 2020/5/8
public class BootDemoApplication {

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

     * Here, @ GetMapping is equivalent to @ RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String test(){
        return "i love java";

The project structure is as follows

It is recommended to start the debugging mode. In the future, we will do the project as well

Access after startup success http://localhost:8080/hello

The success of the above figure indicates that the project can be accessed, and you have successfully made the first step towards Spring

Configure application.yml

What is yml?
YML file format is a file format written by YAML (YAML airport markup language). YAML is an intuitive data serialization format that can be recognized by computers, and it is easy for human to read, interact with script language easily, and can be imported by different programming language programs supporting YAML library, such as C / C + +, ruby, python, Java, Perl, C + +, PHP, etc.

I don't understand. I don't understand either
It's the configuration file equivalent to xml and properties. It's more intuitive. Let's go to the code

The following properties

spring.resources.locations= classpath:/templates

After changing to yml format

    static-locations: classpath:/templates

yml should note that the colon (:) should be followed by a space, and the second level and the first level should be separated by a Tab


  port: 8080
# If you are connected to a database, you need to have a database that can be used. Otherwise, an error is reported  
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://
    username: root
    password: root

Permanent update address


Posted by Francky683 on Thu, 07 May 2020 20:22:11 -0700