Description of content

The most common use of SpringBoot is for Web API projects.

This article shows you how to quickly build a sample project with the simplest POM dependencies using the automatic configuration feature.

The function is: receive HTTP request and return JSON format data.

1. The required POM dependencies

A major feature of SpringBoot is the auto-configuration, which greatly simplifies the pom. XML and the amount of code.

A simple Web API project needs the following functions: handle HTTP requests, read and write MySQL. In actual projects, MySQL connection pool and SQL parsing tools are basically used to improve performance and development efficiency. Druid + MyBatis – Plus is used here

By convention, the automatically configured package name typically includes starter. The starter used in this project is as follows:

  • The web starter includes the automatic configuration of Tomcat, because the project uses SpringBoot’s parent, so there is no need to specify a version here
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  • Druid’s starter is used to automatically configure connection pooling for MySQL connections
< the dependency > < groupId > com. Alibaba < / groupId > < artifactId > druid - spring - the boot - starter < / artifactId > < version > 1.2.1 < / version > </dependency>
  • Starter for MyBatis Plus, used to load dependencies such as the Mapper class
< the dependency > < groupId > com. Baomidou < / groupId > < artifactId > mybatis - plus - the boot - starter < / artifactId > < version > 3.3.1 < / version >  </dependency>

2. Configuration information

When using automatic configuration, you typically use SpringBoot’s configuration file application.yml to customize various parameters. The configuration used in this project is MySQL connection information and Web project startup port. The details are as follows:

spring: datasource: druid: url: jdbc:mysql://localhost:3306/test_db? zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC username: root password: 123456 server: port: 8999
  • Through the use ofdruid-spring-boot-starter, will automatically generate a connection for MySQL access according to the MySQL configuration.
  • portSpecify a port for Web access. API projects typically choose a port other than 80

3. Code description

In order to improve development efficiency, mybatis plus-generator is used in the project to generate entity, mapper and service classes required to read and write tables.

For more efficient configuration with front-end development, it is common for Web API projects to use a unified return class to control the return value format and ensure that the interface returns in a uniform format.

In addition to the generated code, the project only needs to implement the common format class CommonResVo and the controller class UserController to provide API access. The directory is as follows:

Source address, https://gitee.com/dothetrick/…

The above content is a personal learning summary, if there is any improper place, welcome to correct in the comments