Spring Boot learning notes for beginners

Spring Boot is a new development framework under the spring family. Its design purpose is mainly to simplify the creation and development process of spring applications. It provides features such as automatic configuration and starter dependency, so as to free developers from a large number of XML configurations. Spring Boot is committed to becoming a leader in the booming field of rapid application development.

Springboot of power node from simple to deep, this course takes you to experience the rapid development process of Spring Boot. It is rich in content, covers all aspects of springboot development, and is synchronously updated to Spring Boot 2 The latest version of X series allows you to win the Spring Boot development framework at one time.

Video viewing address

https://www.bilibili.com/video/BV1XQ4y1m7ex

Spring Boot handout

Chapter 1 Introduction to Spring Boot framework

1.1 introduction to spring boot
Spring Boot yes Spring A new framework in the family, which is used to simplify Spring Application creation and

It can also be said that Spring Boot can simplify the development process. We used the spring MVC + Spring + mybatis framework before

Development process.

In the past, we used SpringMVC + Spring + MyBatis When developing the framework, build and integrate three frames

We need to do a lot of work, such as configuring the web XML, configure Spring, configure MyBatis, and integrate them into

The Spring Boot framework revolutionized the development process and completely abandoned the cumbersome xml configuration

Set the process, and adopt a large number of default configurations to simplify our development process.

So use Spring Boot You can easily and quickly create Spring Framework application, which allows editing

The code is simpler, the configuration is simpler, the deployment is simpler, and the monitoring is simpler. Because of Spring Boot, it turns into

Jane, let the development become extremely simple and fast, so it has attracted much attention in the industry.

Spring Boot Trend chart of domestic attention: http://t.cn/ROQLquP
1.2 features of spring boot
➢ Ability to quickly create Spring New applications
➢ Can be used directly java main Method to start the embedded Tomcat Server running Spring Boot Program, not required
 To deploy war Package file
➢ Provide agreed starter POM To simplify Maven Configuration, let Maven Simple configuration
➢ Automated configuration, based on Project Maven Dependent configuration, Spring boot Auto configuration Spring,Spring mvc
 etc.
➢ It provides functions such as health check of the program
➢ Basically, it can not be used at all XML Configuration file, using annotation configuration
Beijing power node http://www.bjpowernode.com
1.3 four cores of spring boot
1.3.1 automatic configuration
1.3.2 start dependence
1.3.3 Actuator
1.3.4 command line interface
Beijing power node http://www.bjpowernode.com

Chapter 2 Introduction to Spring Boot

2.1 the first SpringBoot project
2.1.1 development steps

Project Name: 001 - springboot first

(1) Create a Module and select the type Spring Initializr for quick build
Beijing power node http://www.bjpowernode.com
(2) Set GAV coordinates and pom configuration information
(3) Select the Spring Boot version and dependencies

The start dependency will be automatically added and configured according to the selected dependency

Beijing power node http://www.bjpowernode.com
(4) Set the module name, Content Root path and directory of module files

Click Finish. If it is created for the first time, you will be prompted in the lower right corner that relevant dependencies are being downloaded

Beijing power node http://www.bjpowernode.com
(5) The project is created as follows
(6) Project structure
Beijing power node http://www.bjpowernode.com

Static: store static resources, such as pictures, CSS, JavaScript, etc

templates: the template file that holds the Web page

application.properties/application.yml is used to store the configuration information of various dependent modules of the program, such as services

Port, database connection configuration, etc

2.2 introduction cases

Project Name: 002 - springboot springmvc

2.2.2 create a new Module and select the type as Spring Initializr
Beijing power node http://www.bjpowernode.com
2.2.3 specify GAV and pom configuration information
2.2.4 select Spring Boot version and dependency
The start dependency will be automatically added and configured according to the selected dependency
Beijing power node http://www.bjpowernode.com
2.2.5 modify the Content Root path and the directory where the file is located
2.2.6 for POM XML file

http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0org. springframework. boot spring-boot-starter-parent 2.2.1. RELEASE com. bjpowernode. springboot 002 - springboot-springmvc 1.0.0 002 - springboot-springmvc Demo project for Spring Boot 1.8org. springframework. boot spring-boot-starter-web org. springframework. boot spring-boot-starter-test test org. junit. Vintage JUnit Vintage engine Beijing power node http://www.bjpowernode.com
org.springframework.boot spring-boot-maven-plugin

Beijing power node http://www.bjpowernode.com
2.2.7 describe the structure of SpringBoot project
➢ .mvn|mvnw|mvnw.cmd: Execute using script actions maven Relevant orders are rarely used in China and can be deleted
 except
➢ .gitignore: Using version control tools git When, set some ignore the submitted content
➢ static|templates: The following is the directory where the files are stored in the template technology
➢ application.properties: SpringBoot Many integrated configurations can be in this file
 Configure, for example: Spring,springMVC,Mybatis,Redis Wait. Is currently empty
➢ Application.java: SpringBoot The entry of program execution, which executes the main method, SpringBoot
 It started
2.2.8 create a Spring BootController for Spring MVC

Package of SpringBootController class: com bjpowernode. springboot. web

package com.bjpowernode.springboot.web;

import org.springframework.stereotype.Controller;

Beijing power node http://www.bjpowernode.com

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

_/**

  • ClassName:SpringBootController
  • Package:com.bjpowernode.springboot.web
  • Description:
    */_
    @Controller
    public class SpringBootController {

@RequestMapping(value = "/springBoot/say")
public @ResponseBody String say() {
return "Hello,springBoot!";
}
}

Note: the newly created class must be located in Application Same level directory or lower level directory; otherwise SpringBoot plus

I can't.

2.2.9 right click in IDEA and run the main method in Application class
Through the output in the console, you can see the startup SpringBoot Framework, will start an embedded tomcat,end
 The slogan is 8080 and the context root is empty
2.2.10 enter in browser http://localhost:8080/springBoot/say visit
Beijing power node http://www.bjpowernode.com
2.3 introduction case analysis
➢ Spring Boot Parent dependency of spring-boot-starter-parent After configuration, the current project is Spring
Boot project
➢ spring-boot-starter-parent It's a Springboot Parent dependency, development SpringBoot All procedures need
 To inherit the parent project, it is used to provide related Maven Default dependency. After using it, common jar
 Package dependency can be eliminated version to configure
➢ Spring Boot What defaults are provided jar Package dependency. You can view the dependency of the parent pom file
➢ If you do not want to use a default dependent version, you can use the pom.xml The attribute configuration of the file overrides each
 Dependencies, such as overrides Spring edition

<spring-framework.version>5.0.0.RELEASE</ spring-framework.version >

</properties>
➢ @SpringBootApplication Annotation is Spring Boot The core annotation of the project is mainly used to open
Spring Auto configuration, if in Application If the annotation is removed from the class, it will not be started SpringBoot
 program
➢ main The method is a standard one Java programmatic main Method, which is mainly used as the input for the start-up and operation of the project
 mouth
➢ @Controller and @ResponseBody It's still what we used to be Spring MVC,because Spring Boot
 It still uses ours Spring MVC + Spring + MyBatis Equal framework
2.4 core configuration file of spring boot
Spring Boot The core configuration file is used for configuration Spring Boot Program name must be application start
2.4.1 core configuration format
( 7 ) . properties file (this file is adopted by default)

Modify the 002 - springboot springmvc project

Beijing power node http://www.bjpowernode.com

Project Name: 003 - springboot port context path

By modifying application Properties configuration file, modify the default tomcat port number and project upper and lower file roots

Key value pair properties Property file configuration mode

#Set the embedded Tomcat port number
server.port= 9090

#Configuration item context root
server.servlet.context-path=/003-springboot-port-context-path

After configuration, start the browser test

Page display results
Beijing power node http://www.bjpowernode.com
( 8 ) . yml file

Project Name: 004 - springboot YML, based on 003 project

yml It's a kind of yaml Format configuration file, which is mainly configured by certain space, line feed and other formats.
yaml It is an intuitive data serialization format that can be recognized by computer and easy to be read by human beings, yaml class

Similar to xml, but the syntax is much simpler than xml. There must be a space between the value and the colon configuration item before yml

You can also use the yaml suffix

Note: when the configuration files of two formats exist at the same time, use the properties configuration file. To demonstrate yml, you can

First change its name, re run the Application, and check the startup port and context root

We will use properties in the course of teaching later, so we will rename the configuration file after the yml effect is demonstrated

Beijing power node http://www.bjpowernode.com
2.4.2 multi environment configuration
In the actual development process, our project will go through many stages (Development)->test->Online), each stage

The configuration will be different, such as port, context root, database, etc. at this time, in order to facilitate different environments

To switch between, SpringBoot provides multi environment configuration. The specific steps are as follows

(9) Project Name: 005 - springboot multi environment
Create a configuration file for each environment, which must be named with application- Environmental identification .properties|yml
application-dev.properties
#development environment 
Beijing power node http://www.bjpowernode.com

#Set default port number of embedded Tomcat
server.port= 8080

#Set the context root of the project
server.servlet.context-path=/00 5 - springboot-multi-environment-dev

application-product.properties

#Production environment

#Configure embedded Tomcat default port number
server.port= 80

#Configuration item context root
server.servlet.context-path=/00 5 - springboot-multi-environment-product

application-test.properties

#Test environment

#Configure embedded Tomcat port number
server.port= 8081

#Context root of configuration item
server.servlet.context-path=/00 5 - springboot-multi-environment-test

In the general configuration file application Properties to activate the environment

#General configuration file of SpringBoot

#Activate development environment
#spring.profiles.active=dev

#Activate test environment
#spring.profiles.active=test

#Activate production environment
spring.profiles.active=product

The value on the right side of the equal sign is consistent with the environment ID name of the configuration file. You can change the configuration of the general configuration file and restart it

Beijing power node http://www.bjpowernode.com
function Application ,View the startup port and context root
(10) Project Name: 006 - springboot multi environment

Create a configuration file for each environment. The name must be identified by application environment properties|yml

SpringBoot general configuration file: application yml

#General configuration file springboot

#Activate development environment
_#spring:

profiles:

active: dev_

Beijing power node http://www.bjpowernode.com

#Activate test environment
_#spring:

profiles:

active: test_

#Activate production environment
spring :
profiles :
active : product

Development environment configuration file: application-dev.yml

#Set development environment configuration

server :
Port: 8080 # set Tomcat embedded port number
servlet :
Context path: / dev # sets the context root

Test environment configuration file: application test yml

#Set test environment configuration

server :
port : 9090
servlet :
context-path : /test

Production environment profile: application product yml

#Set production environment configuration

server :
port : 80
servlet :
context-path : /product

Beijing power node http://www.bjpowernode.com
2.4.3 Spring Boot custom configuration
stay SpringBoot In the core configuration file of, in addition to using built-in configuration items, we can also customize configuration items

Set, and then use the following annotation to read the configured attribute value

(11) @ Value annotation
A. Project Name: 007 - springboot custom configuration
Used to read one by one application.properties Configuration in
 Case demonstration
➢ In the core configuration file applicatin.properties In, add two custom configuration items school.name and
website. stay IDEA You can see that these two attributes cannot be changed SpringBoot Recognition, the background is orange
application.yml Format profile

#Set the port number and context root
server :
port : 9090
servlet :
context-path : /

school :
name : ssm
websit : http://www.baidu.com

➢ stay SpringBootController Define properties in and use@Value Annotation or custom configuration value, and
Beijing power node http://www.bjpowernode.com
The method is tested
@Controller
public class SpringBootController {
@Value("${school.name}")
private String schoolName;
@Value("${websit}")
private String websit;
@RequestMapping(value = "/springBoot/say")
public @ResponseBody String say() {
return schoolName + "------" + websit;
}
}
➢ Rerun Application,Test in browser
( 12 ) @ConfigurationProperties

Project Name: 008 - springboot custom configuration

Map the whole file into an object, which is used when there are many custom configuration items
 Case demonstration
➢ stay com.abc.springboot.config Create under package ConfigInfo Class and add Component and
ConfigurationProperties Comments, and in ConfigurationProperties Add attribute to annotation prefix,
Functions can distinguish configurations with the same name
@Component
Beijing power node http://www.bjpowernode.com
@ConfigurationProperties(prefix = "school")
public class ConfigInfo {
private String name;
private String websit;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWebsit() {
return websit;
}
public void setWebsit(String websit) {
this.websit = websit;
}
}
application.properties configuration file

#Set the embedded Tomcat port number
server.port= 9090

#Set context root
server.servlet.context-path=/config

school.name=ssm
school.websit=http://www.baidu.com

application.yml configuration file

server :
port : 9090
servlet :
context-path : /config

Beijing power node http://www.bjpowernode.com

school :
name : ABC
websit : http://www.baidu.com

➢ stay SpringBootController Medium injection ConfigInfo Configuration class
@Autowired
private ConfigInfo configInfo;
➢ modify SpringBootController Test methods in classes
@RequestMapping(value = "/springBoot/config")
public @ResponseBody String say() {
return configInfo.getName() + "=======" + configInfo.getWebsit();
}
➢ Rerun Application,Test in browser
(13) Warning resolution
➢ stay ConfigInfo Class ConfigurationProperties After annotation, IDEA A warning will appear,
It does not affect the execution of the program
➢ click open documentnation Jump to the web page and prompt the need to add a dependency in the web page. We will
 Dependent copy, paste to pom.xml In the file
Beijing power node http://www.bjpowernode.com
<!--Solve use@ConfigurationProperties Warning problem with annotation-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
(14) Chinese garbled code
If in SpringBoot If there is Chinese information in the core configuration file, there will be garbled Code:
◼ Generally, Chinese is not recommended in the configuration file (except notes)
◼ If yes, you can convert it to ASCII code
(15) Friendly tips
If you copy the configuration file from other places, you must delete the spaces in it
2.5 Spring Boot front end uses JSP

Project Name: 009 - springboot JSP

Beijing power node http://www.bjpowernode.com
2.5.4 in POM Configure the following dependencies in the XML file

org.apache.tomcat.embed tomcat-embed-jasper

javax.servlet javax.servlet-api

javax.servlet.jsp javax.servlet.jsp-api 2.3.1

javax.servlet jstl

2.5.5 in POM The following information should be configured in the build tag of XML
SpringBoot requirement jsp The file must be compiled to the specified META-INF/resources Directory, otherwise

Unable to access. In fact, the official has suggested using template technology (template technology will be discussed later)

Beijing power node http://www.bjpowernode.comsrc/main/webapp META-INF/resources **/.

2.5.6 in application The view of configuring Spring MVC in the properties file is displayed as
jsp, which is equivalent to the configuration of Spring MVC

#SpringBoot core configuration file
#Specify the embedded Tomcat port number
server.port= 8090

#Configure spring MVC view parser
#Where: / indicates that the directory is src/main/webapp
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp

After the integration, the remaining steps are the same as those we use Spring MVC equally

application. Configuration file in YML format

#SpringBoot core configuration file
#Specify the embedded Tomcat port number
server :
port : 8090
servlet :
context-path : /

#Configure spring MVC view parser
#Where: / indicates that the directory is src/main/webapp
spring :
mvc :
view :
prefix : /
suffix : .jsp

Beijing power node http://www.bjpowernode.com
2.5.7 on COM abc. springboot. Create the JspController class under the controller package and
Write code

@Controller
public class SpringBootController {

@RequestMapping(value = "/springBoot/jsp")
public String jsp(Model model) {

model.addAttribute("data", "spring boot front end uses JSP page!");

return "index";
}
}

2.5.8 create a webapp directory under src/main, and then create a new directory under this directory
index.jsp page
If in webapp Right click under the directory, not created jsp Options can be found in Project Structure Specified in webapp

Directory for web resource

Beijing power node http://www.bjpowernode.com
2.5.9 get the data passed by the Controller in jsp
2.5.10 re run the Application and access the test through the browser

Chapter 3 Spring Boot framework Web development

3.1 Spring Boot integration MyBatis

Project Name: 010 - springboot web mybatis

3.1.1 case ideas
adopt SpringBoot +MyBatis Realize the query operation of database student table
 Database reference: springboot.sql Script file
Beijing power node http://www.bjpowernode.com
3.1.2 implementation steps
(1) Prepare database
➢ start-up Linux On system mySQL Server, via Navicat connect
➢ create new database springboot,Specify the database character encoding as utf- 8
➢ Insert data into a table
(2) Create 010 - springboot web mybatis project
➢ Create a new SpringBoot of Module
Beijing power node http://www.bjpowernode.com

・ specify GAV coordinates

Beijing power node http://www.bjpowernode.com

Choose the SpringBoot version and web dependency

Beijing power node http://www.bjpowernode.com
➢ modify Content root as well as Mudule file location
(3) In POM Add relevant jar dependencies to XML
<!--MyBatis integration SpringBoot Start dependence-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
Beijing power node http://www.bjpowernode.com
<!--MySQL Driver dependency-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
(4) In the core configuration file of Springboot, application Configuration in properties
Set data source
Pay attention to modifying the following contents according to the information in your own database
#Configure embedded Tomcat port number
server.port= 9090
#Configuration item context root
server.servlet.context-path=/010-springboot-web-mybatis
#Configuration database connection information
#Note that the driver class here has changed
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=t
rue&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyD
atetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password= 123456
(5) Development code
➢ use Mybatis Reverse engineering generates interfaces, mapping files, and entities bean,See Appendix 1 for specific steps
Beijing power node http://www.bjpowernode.com

・ create StudentController under web package and write code

@Controller
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping(value = "/springBoot/student")
public @ResponseBody Object student() {
Student student = studentService.queryStudentById( 1 );
return student;
}
}

Create a service interface under the service package and write code

public interface StudentService {
/**
* Get student details according to student ID
* @param id
* @return
*/
Student queryStudentById(Integer id);
}

In service Create service interface and write code under impl package

@Service
public class StudentServiceImpl implements StudentService {
Beijing power node http://www.bjpowernode.com
@Autowired
private StudentMapper studentMapper;
@Override
public Student queryStudentById(Integer id) {
return studentMapper.selectByPrimaryKey(id);
}
}

If there is an error when importing a service into the web, you can try the following configuration

・ add a Mapper annotation on the StudentMapper interface generated by Mybatis reverse engineering

@Mapper effect: mybatis Automatic scanning of mapping file and method of data persistence layer DAO Interface relationship
@Mapper
public interface StudentMapper {

◆ note: by default, the xml Mapping file of Mybatis will not be compiled into the class directory of target

So we need to pom.xml Configuration in file resource
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
Beijing power node http://www.bjpowernode.com
</resource>
</resources>
(6) Start the Application application and access the test run through the browser
3.1.3 other development methods of Dao
(7) Add annotation package scan on the running main class
@MapperScan("com.abc.springboot.mapper")

Comment out the @ Mapper annotation on the StudentMapper interface

Add @ MapperScan("com.abc.springboot.mapper") to the running main class Application

@SpringBootApplication
@MapperScan("com.abc.springboot.mapper")
public class Application {

or

@SpringBootApplication
//Note provided by Mybatis: when scanning the Mapper mapping configuration file of the data persistence layer, you don't need to add @ Mapper on the DAO interface
//Base packages are usually specified to the data persistence layer package
@MapperScan(basePackages = "com.abc.springboot.mapper")
public class Application {

test run

Beijing power node http://www.bjpowernode.com
(8) Separate the interface from the mapping file
A. Project Name: 011 - springboot web mybatis
because SpringBoot Interface mapping cannot be compiled automatically xml File, also need to manually pom Specified in the document,

Therefore, some companies directly put the mapping file into the resources directory

➢ stay resources New directory under directory mapper Save the mapping file StudentMapper.xml File move
 reach resources/mapper Under the directory
➢ stay application.properties The location of the mapping file is specified in the configuration file. This configuration has only interfaces and mappings
 You only need to specify when the shooting file is not in the same package
# Specifies the path to the Mybatis mapping file
mybatis.mapper-locations=classpath:mapper/*.xml
3.2 Spring Boot transaction support
Spring Boot Using transactions is very simple, and the bottom layer still adopts Spring Transaction management provided by itself
➢ Using annotations in entry classes @EnableTransactionManagement Turn on transaction support
➢ When accessing the database Service Add annotation on method @Transactional that will do
Beijing power node http://www.bjpowernode.com
3.2.1 case ideas
adopt SpringBoot +MyBatis Realize the update operation of the student table in the database service Layer method

Exception. Check whether the transaction is effective

Project Name: 012 - springboot-web-mybatis-transacation

The project is to add new methods based on 011, and demonstrate cases in the new methods

3.2.2 implementation steps
(9) Add methods to update students in StudentController

@RequestMapping(value = "/springboot/modify")
public @ResponseBody Object modifyStudent() {

int count = 0 ;
try {
Student student = new Student();
student.setId( 1 );
student.setName("Jack");
student.setAge( 33 );
count = studentService.modifyStudentById(student);
} catch (Exception e) {
e.printStackTrace();
return "fail";
}

return count;
}

(10) Add and update student method in StudentService interface

/***Update student information according to student ID
*** @param student

Beijing power node http://www.bjpowernode.com

*** @return
*/
int modifyStudentById(Student student);

(11) Update the student method in the StudentServiceImpl interface implementation class
Line implementation, build an exception, and add @ Transactional annotation to the method

@Override
@Transactional / / add this annotation to describe the transaction management added by this method
public int update(Student student) {

int updateCount = studentMapper.updateByPrimaryKeySelective(student);

System. out .println("update result:" + updateCount);

//Construct an exception with divisor 0 here to test whether the transaction works
int a = 10 / 0 ;

return updateCount;
}

(12) Add @ EnableTransactionManagement to the Application class
Turn on transaction support

@EnableTransactionManagement is optional, but @ Transactional transaction must be added to the business method to take effect

@SpringBootApplication
@MapperScan(basePackages = "com.abc.springboot.mapper")
@EnableTransactionManagement / / enable transaction support (optional, but @ Transactional must be added)
public class Application {

Beijing power node http://www.bjpowernode.com
(13) Start the Application and access it through the browser for testing
browser
Console
Database table
Through the above results, it shows that the transaction works
(14) Comment out the @ Transactional test on StudentServiceImpl
The data in the database is updated
Beijing power node http://www.bjpowernode.com
3.3 Spring MVC under spring boot
Spring Boot Lower Spring MVC And before Spring MVC The usage is exactly the same, mainly with the following notes
3.3.1 @Controller

Annotation of Spring MVC to handle http requests

3.3.2 @RestController
Spring 4 Add a new comment after, yes@Controller Enhancement of annotation function
 yes @Controller And@ResponseBody Combined annotation

If @ RestController is added to a Controller class, all methods under the Controller class are equivalent

Added @ ResponseBody annotation on

Used to return string or json data

Case:
➢ establish MyRestController Class, demo@RestController replace@Controller + @ResponseBody
@RestController
public class MyRestController {
@Autowired
private StudentService studentService;
@RequestMapping("/boot/stu")
public Object stu(){
return studentService.getStudentById( 1 );
}
}
➢ Start the application and access the test browser
Beijing power node http://www.bjpowernode.com
3.3.3 @RequestMapping (common)
support Get Request and support Post request
3.3.4 @GetMapping

Combination of RequestMapping and Get request methods

Only Get requests are supported

Get request is mainly used for query operation

3.3.5 @PostMapping

Combination of request and Post mapping

Only Post requests are supported

Post requests main users to add data

3.3.6 @PutMapping

Combination of RequestMapping and Put request methods

Only support Put request

Put is usually used to modify data

3.3.7 @DeleteMapping

Combination of RequestMapping and Delete request methods

Only Delete requests are supported

Usually used to delete data

3.3.8 comprehensive cases

Project Name: 013 - springboot springmvc project integration springmvc

Project role: demonstrate common spring MVC annotations

Beijing power node http://www.bjpowernode.com
(15) Create an MVC controller that uses the annotations described above
Receive different requests

/***This case mainly demonstrates the use of different annotations provided by Spring to receive different types of requests
*/
//The RestController annotation is equivalent to adding @ ResponseBody annotation to the method, so you can't jump to the page,
Only string or json data can be returned
@RestController
public class MVCController {

@GetMapping(value = "/query")
public String get() {
return "@ GetMapping annotation, usually used in query";
}

@PostMapping(value = "/add")
public String add() {
return "@ PostMapping annotation, usually used when adding";
}

@PutMapping(value = "/modify")
public String modify() {
return "@ PutMapping annotation, usually used when updating data";
}

@DeleteMapping(value = "/remove")
public String remove() {
return "@ DeleteMapping annotation, usually used when deleting data";
}
}

Beijing power node http://www.bjpowernode.com
(16) Start the application and enter different requests in the browser for testing
(17) Introduction to Http interface request tool Postman
Because the address is entered through the browser, the default sending can only be get Request, pass Postman Tools that can simulate

Send different types of requests and query results. During installation, some machines may need to install Microsort NET

Framework

(18) Use Postman to test other request types
Beijing power node http://www.bjpowernode.com
3.4 Spring Boot to achieve RESTful
3.4.1 understanding RESTFul

REST (English: representative state transfer, referred to as REST)

A style of Internet software architecture design, but it is not a standard. It just puts forward a set of clients and servers

The architectural concept and design principle of interaction. The interface designed based on this concept and principle can be more concise, hierarchical and restful

This word was put forward by Roy Thomas Fielding in his doctoral thesis in 2000.

Any technology can realize this concept if an architecture conforms to REST Principle, call it RESTFul frame

structure

For example, we're going to visit a http Interface: http://localhost:8080/boot/order?id=1021&status=1
 use RESTFul Style rule http The address is: http://localhost:8080/boot/order/1021/1
3.4.2 Spring Boot development RESTFul
Spring boot development RESTFul It is mainly realized by several annotations
( 1 ) @PathVariable
obtain url Data in
 This annotation is an implementation RESTFul The most important note
( 2 ) @PostMapping
Receiving and processing Post Mode request
( 3 ) @DeleteMapping
receive delete Mode request, you can use GetMapping replace
Beijing power node http://www.bjpowernode.com
( 4 ) @PutMapping
receive put Mode request, you can use PostMapping replace
( 5 ) @GetMapping
receive get Mode request
3.4.3 case: use RESTful style simulation to add, delete, modify and check students

Project Name: 014 - springboot restful

The project integrates MyBatis, spring and spring MVC, and realizes the addition, deletion, modification and query of students through simulation

( 6 ) pom. Add the following contents to the XML file

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

org.mybatis.spring.boot mybatis-spring-boot-starter 2.0.1

mysql mysql-connector-java

Keywords: Java Spring Boot Back-end architecture

Added by wwfc_barmy_army on Thu, 24 Feb 2022 10:55:06 +0200