SpringBoot learning (unfinished)

SpringBoot learning

Getting started

Run SpringBoot quickly

Import the corresponding dependencies in pom.xml



Create a main function of springboot

package com.xzh.boot;

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

 * @Author XuZhuHong
 * @CreateTime 2021/6/7 12:03
 * @SpringBootApplication 生命这是一个springboot应用
public class MainApplicationContext {
    public static void main(String[] args) {

Create a control layer

package com.xzh.boot.Controller;

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

 * @Author XuZhuHong
 * @CreateTime 2021/6/7 12:05
public class HelloController {

    public String handle01(){
        return "Hello, Spring Boot 2!";

Run springboot main method

Browser access to the corresponding address



Insert picture description here

How to pack

Import plugin


Insert picture description here

How to run:
Enter the folder of the jar package,
start cmd
and enter the command java -jar file name, how many IU can start the project

application.properties file configuration

What can be configured in the file can refer to the official document
All configurations are in this

Notes on the main program

@Import({User.class, DBHelper .class} )
//这个注解表示是不是说有容器都用一个实例  false表示不是  true表示是
@Configuration( proxyBeanMethods = false)
//这个是根据条件创建  bean
@ConditionalOnMissingBean(name = "tom" )
@ImportResource("classpath: beans . xm1" )

Automatic binding (similar to reading properties file)

 * 只有在容器中的组件,才会拥有SpringBoot提供的强大功能
@ConfigurationProperties(prefix = "mycar")
public class Car {
    private String brand;
    private Integer price;

Development tips

Lombok for javaBean

Import the corresponding package


Then add the lombok plug-in to the idea.

Insert picture description here

A few notes about him:

@ConfigurationProperties(prefix = "person")
public class Person {
    private String sex;
    private Integer age;

Hot update dependency dev-tools

If you rely on pom.xml, it's ok


How to use Ctrl+F9 to update after code update

Quickly create a spring boot artifact Spring Initailizr

In idea, a new porject
select Spring Initailizr on the far right

Insert picture description here

Choose the scene you need to use on this page,
such as web mysql mybatis

Insert picture description here

He will automatically introduce dependencies and create the initial project structure

Configuration file


Basic syntax:

Key: value; there is a space between kv
Case sensitive
Use indentation to indicate hierarchical relationship
Tabs are not allowed for indentation, only spaces are allowed
. The number of spaces for indentation is not important, as long as the elements of the same level are aligned to the left,
'#' means comment
The string does not need to be quoted, if you want to add, "and"" means that the string content will be escaped/unescaped

• Literal: A single, indivisible value. date, boolean, string, number, null

k: v

Object: A collection of key-value pairs. map, hash, set, object

行内写法:  k: {k1:v1,k2:v2,k3:v3}
  k1: v1
  k2: v2
  k3: v3

Array: A set of values ​​arranged in order. array, list, queue

行内写法:  k: [v1,v2,v3]
 - v1
 - v2
 - v3

Person class:

package com.xzh.boot12.bean;

import lombok.Data;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

 * @Author XuZhuHong
 * @CreateTime 2021/6/7 18:40
@ConfigurationProperties(prefix = "person")
public class Person {

    private String userName;
    private Boolean boss;
    private Date birth;
    private Integer age;
    private Pet pet;
    private String[] interests;
    private List<String> animal;
    private Map<String, Object> score;
    private Set<Double> salarys;
    private Map<String, List<Pet>> allPets;

application.yml configuration file:

# yaml表示以上对象
  userName: zhangsan
  boss: false
  birth: 2019/12/12 20:12:33
  age: 18
    name: tomcat
    weight: 23.4
  interests: [ 篮球,游泳 ]
    - jerry
    - mario
      first: 30
      second: 40
      third: 50
    math: [ 131,140,148 ]
    chinese: { first: 128,second: 136 }
  salarys: [ 3999,4999.98,5999.99 ]
      - { name: tom }
      - { name: jerry,weight: 47 }
    health: [ { name: mario,weight: 47 } ]

For the characters inside,
you can add double quotation marks and single quotation marks
. Double quotation marks have the same meaning as java.
Single quotation marks do not escape the string.

There is no prompt solution when configuring the file:

Import the following dependencies in pom.xml


                <!--这句话的意思是 让这个打包插件 不要把yaml的代码提示放进去-->

WEB development

Static resource access

As long as the static resources are placed in the classpath: called /static (or /public or /resources or /META-INF/resources
Access: current project root path/ + static resource name

Change the default static resource path

    static-path-pattern: /res/**
    #这个东西会影响 springboot的自定义欢迎页面失效

    static-locations: [classpath:/haha/]

redis style open

Open in the configuration file:
here it is turned on in yml

        enabled: true

Then set the corresponding request method in the controller

    //    @RequestMapping(value = "/user",method = RequestMethod.GET)
    public String getuser() {
        return "getuser user";

    //    @RequestMaping(value = "/user", method = RequestMethod.POST)
    @PostMapping ("/user")
    public String postuser() {
        return "postuser user";

    //    @RequestMapping(value = "/user", method = RequestMethod.DELETE)
    public String deleteuser() {
        return "deleteuser user";

    //    @RequestMapping(value = "/user", method = RequestMethod.PUT)
    public String putuser() {
        return "putuser user";

It should be noted that the request method must be post, and there needs to be a label in the form whose name is _method and the value is the submitted method. This can be set to the hidden control layer to call it correctly.

<form method="get" action="/user">
    <input type="submit" value="get">
<form method="post" action="/user">
    <input type="submit" value="post">
<form method="post" action="/user">
    <input name="_method" type="hidden" value="delete">
    <input type="submit" value="delete">
<form method="post" action="/user">
    <input name="_method" type="hidden" value="put">
    <input type="submit" value="put">

To change the value of name, you can do the following

@Configuration(proxyBeanMethods = false)
public class WebConfig {
    public HiddenHttpMethodFilter hiddenHttpMethodFilter(){
        HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
        return hiddenHttpMethodFilter ;