Springboot Series 03 – Complete the first sample program using IDEA

Time:2019-10-2

No reprinting without permission

Original author: Alphabet blog

The complete series of this article comes from: Springboot in-depth shallow series

First, use IntellijIDEA to build the first spring boot project

Springboot Series 03 - Complete the first sample program using IDEA
Usually only the pro paid version defaults to Spring Initialzr, if your IDEA does not have this option. Reference can be made to “Spring Initializr” at the end of this article.
Springboot Series 03 - Complete the first sample program using IDEA
Here we can choose the third-party software class libraries we need to rely on, including spring-boot-web, MySQL driver, mybatis and so on. We will not add any dependencies here for the time being. We will add Maven dependencies manually later. This is more conducive to understanding. Once you are familiar with maven dependencies, you can go back here and try checking again.
Springboot Series 03 - Complete the first sample program using IDEA
The project creation process may be due to maven dependencies, long download time and patience. After the project is built, delete the following files, which are Maven version control related files. We manage Maven in combination with IDEA. Generally speaking, these files are not needed.
Springboot Series 03 - Complete the first sample program using IDEA

2. Hello World Sample Program

Change application. properties to application. yml. The YML file and the properties configuration file have the same functions. The difference between the two is that:

  • The hierarchy of YML files is clearer and more intuitive, but format indentation and alignment should be paid attention to when writing. YML format configuration file is more conducive to express the configuration of complex data structures. For example: lists, objects (detailed in later chapters).
  • Properties are not as intuitive as YML in reading. The advantage is that you don’t have to pay special attention to format indentation and alignment when writing.
server:
  Port: 8888  Web application service port

Introduce spring-boot-starter-web dependencies (you don’t need a version number, you can read the previous section if you don’t understand)

org.springframework.boot
      spring-boot-starter-web

A Hello World test Controller

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello(String name) {
        return "hello world, " +name;
    }
}

Take a test.
Springboot Series 03 - Complete the first sample program using IDEA

3. Brief introduction of catalogue structure of project structure

Springboot Series 03 - Complete the first sample program using IDEA
The project structure catalogue as a whole meets the requirements of maven specification:

Directory location function
src/main/java Project java file storage location, initialization contains the main program entry XxxApplication, you can start the Spring Boot application directly by running this class.
src/main/resources Store static resources, images, CSS, JavaScript, web page template files, etc.
src/test Unit Test Code Directory
.gitignore Git version management exclusion file
Target folder Project Code Construction Packaging Result File Storage Location, No Man-made Maintenance
pom.xml Maven project configuration file
application.properties(application.yml) Configuration information of various dependent modules used to store programs, such as service ports, database connection configurations, etc.
  • Src / main / resources / static is mainly used to store css, pictures, JS and other development static files
  • Src/main/resources/public is used to store HTML files that can be accessed directly
  • Src/main/resources/templates for storing web development template files

Attachment: Spring Initializr

If IDEA does not have Spring Initializr option, install the IDEA plug-in first.
Springboot Series 03 - Complete the first sample program using IDEA

No reprinting without permission

Original Author: Alphabetic Brother Blog

The complete series of this article comes from: Springboot in-depth shallow series