相关推荐recommended
SpringBoot(Lombok + Spring Initailizr + yaml)
作者:mmseoamin日期:2024-04-27

1.Lombok

1.基本介绍

SpringBoot(Lombok + Spring Initailizr + yaml),image-20240313161718829,第1张

2.应用实例
1.pom.xml 引入Lombok,使用版本仲裁
    
    
        spring-boot-starter-parent
        org.springframework.boot
        2.5.3
    
    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.projectlombok
            lombok
        
    
2.@Data注解说明
  • 相当于Getter, Setter, RequiredArgsConstructor, ToString, EqualsAndHashCode,Value这些注解的组合
  • 主要记住Getter, Setter,ToString

    package lombok;

    import java.lang.annotation.ElementType;

    import java.lang.annotation.Retention;

    import java.lang.annotation.RetentionPolicy;

    import java.lang.annotation.Target;

    /**

    * Generates getters for all fields, a useful toString method, and hashCode and equals implementations that check

    * all non-transient fields. Will also generate setters for all non-final fields, as well as a constructor.

    *

    * Equivalent to {@code @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode}.

    *

    * Complete documentation is found at the project lombok features page for @Data.

    *

    * @see Getter

    * @see Setter

    * @see RequiredArgsConstructor

    * @see ToString

    * @see EqualsAndHashCode

    * @see lombok.Value

    /

    @Target(ElementType.TYPE)

    @Retention(RetentionPolicy.SOURCE)

    public @interface Data {

    /*

    * If you specify a static constructor name, then the generated constructor will be private, and

    * instead a static factory method is created that other classes can use to create instances.

    * We suggest the name: “of”, like so:

    *

    *


    * public @Data(staticConstructor = “of”) class Point { final int x, y; }
    *

    *

    * Default: No static constructor, instead the normal constructor is public.

    *

    * @return Name of static ‘constructor’ method to generate (blank = generate a normal constructor).

    */

    String staticConstructor() default “”;

    }

    3.@RequiredArgsConstructor注解说明(不常用)

    SpringBoot(Lombok + Spring Initailizr + yaml),image-20240313163329615,第2张

    4.@NoArgsConstructor无参构造器
    5.@AllArgsConstructor全参构造器
    注意事项:
    • 当使用全参构造器时,默认的无参构造器会消失
    • 如果还想要无参构造器就需要使用无参构造器的注解
      6.两种使用Lombok的方式
      1.需要Getter, Setter,ToString,无参构造器
      • @Data
        2.需要使用Getter, Setter,ToString,无参构造器,全参构造器
        • @Data
        • @AllArgsConstructor
        • @NoArgsConstructor
          3.在IDEA中安装Lombok插件解锁扩展注解
          1.安装插件

          SpringBoot(Lombok + Spring Initailizr + yaml),image-20240313170237189,第3张

          2.扩展注解:日志输出
          1.代码实例

          SpringBoot(Lombok + Spring Initailizr + yaml),image-20240313171249514,第4张

          2.会在日志中输出

          SpringBoot(Lombok + Spring Initailizr + yaml),image-20240313171312777,第5张

          2.Spring Initailizr(不推荐)

          1.基本介绍

          SpringBoot(Lombok + Spring Initailizr + yaml),image-20240313171622129,第6张

          2.通过IDEA方式创建
          1.新创建一个项目

          SpringBoot(Lombok + Spring Initailizr + yaml),image-20240313172411941,第7张

          2.进行配置

          SpringBoot(Lombok + Spring Initailizr + yaml),image-20240313172738168,第8张

          3.创建成功

          SpringBoot(Lombok + Spring Initailizr + yaml),image-20240313173041517,第9张

          3.通过官网创建
          1.进入官网

          SpringBoot(Lombok + Spring Initailizr + yaml),image-20240313173352413,第10张

          2.配置完之后选择

          SpringBoot(Lombok + Spring Initailizr + yaml),image-20240313173437210,第11张

          3.最后会生成一个.zip文件,解压之后在IDEA中打开即可
          4.第一次使用自动配置爆红

          SpringBoot(Lombok + Spring Initailizr + yaml),image-20240313173715463,第12张

          3.yaml

          1.基本说明

          SpringBoot(Lombok + Spring Initailizr + yaml),image-20240313174407089,第13张

          2.yaml基本语法

          SpringBoot(Lombok + Spring Initailizr + yaml),image-20240313174915598,第14张

          3.yaml数据类型
          1.字面量

          SpringBoot(Lombok + Spring Initailizr + yaml),image-20240313175754697,第15张

          2.对象

          SpringBoot(Lombok + Spring Initailizr + yaml),image-20240313175837930,第16张

          3.数组

          SpringBoot(Lombok + Spring Initailizr + yaml),image-20240313175918900,第17张

          4.yaml应用实例
          1.创建一个新的maven项目

          SpringBoot(Lombok + Spring Initailizr + yaml),image-20240313190536147,第18张

          2.pom.xml引入依赖并刷新maven
              
              
                  spring-boot-starter-parent
                  org.springframework.boot
                  2.5.3
              
              
                  
                  
                      org.springframework.boot
                      spring-boot-starter-web
                  
                  
                  
                      org.projectlombok
                      lombok
                  
              
          
          3.编写两个bean
          1.Car.java
          package com.sun.springboot.bean;
          import lombok.Data;
          import org.springframework.stereotype.Component;
          /**
           * @author 孙显圣
           * @version 1.0
           */
          @Data //getter,setter,tostring,无参构造
          @Component
          public class Car {
              private String name;
              private Double price;
          }
          
          2.Monster.java
          package com.sun.springboot.bean;
          import lombok.Data;
          import org.springframework.stereotype.Component;
          import java.util.*;
          /**
           * @author 孙显圣
           * @version 1.0
           */
          @Data
          @Component
          public class Monster {
              private Integer id;
              private String name;
              private Integer age;
              private Boolean isMarried;
              private Date birth;
              private Car car;
              private String[] skill;
              private List hobby;
              private Map wife;
              private Set salaries;
              private Map> cars;
          }
          
          4.HiController.java 接受请求
          package com.sun.springboot.controller;
          import com.sun.springboot.bean.Monster;
          import org.springframework.web.bind.annotation.RequestMapping;
          import org.springframework.web.bind.annotation.RestController;
          import javax.annotation.Resource;
          /**
           * @author 孙显圣
           * @version 1.0
           */
          @RestController
          public class HiController {
              @Resource
              private Monster monster;
              @RequestMapping("/monster")
              public Monster monster() {
                  return monster;
              }
          }
          
          5.主程序Application.java
          package com.sun.springboot;
          import org.springframework.boot.SpringApplication;
          import org.springframework.boot.autoconfigure.SpringBootApplication;
          /**
           * @author 孙显圣
           * @version 1.0
           */
          @SpringBootApplication
          public class Application {
              public static void main(String[] args) {
                  SpringApplication.run(Application.class);
              }
          }
          
          6.运行主程序(目前返回的值是空的)

          SpringBoot(Lombok + Spring Initailizr + yaml),image-20240313192941733,第19张

          7.创建yaml文件(后缀也可以是yml) resources/application.yml
          monster: #前缀
            id: 100
            name: 牛魔王
            age: 500
            isMarried: false
            birth: 2000/11/11 
          
          8.绑定数据到Monster类

          SpringBoot(Lombok + Spring Initailizr + yaml),image-20240313194026901,第20张

          9.解决报错
          1.因为使用@Configuration注解导致的问题

          SpringBoot(Lombok + Spring Initailizr + yaml),image-20240313194047615,第21张

          2.在pom.xml中添加依赖即可
              
                org.springframework.boot
                spring-boot-configuration-processor
                
                true
              
          
          3.运行主程序

          SpringBoot(Lombok + Spring Initailizr + yaml),image-20240313195153451,第22张

          10.完整yml文件
          monster: #前缀
            id: 100
            name: 牛魔王
            age: 500
            isMarried: false
            birth: 2000/11/11
            #对象类型
          #  car: {name: 宝马, price: 1000} #行内格式
            car:
              name: 奔驰
              price: 3000
            #数组类型
          #  skill: [芭蕉扇, 牛魔拳] #行内格式
            skill:
              - 牛魔王
              - 芭蕉扇
            #list类型
          #  hobby: [白骨精, 美人鱼]
            hobby:
              - 白骨精
              - 牛魔王
            #map类型
          #  wife: {no1: 牛魔王, no2: 猪八戒}
            wife:
              no1: 白骨精
              no2: 铁扇公主
            #set类型
          #  salaries: [1, 2, 3]
            salaries:
              - 4
              - 5
              - 6
            #map>类型
            cars:
              car1: [
                {name: 奔驰, price: 400},
                {name: 奔驰, price: 400}
              ]
              car2: [
                {name: 奔驰, price: 400},
                {name: 奔驰, price: 400}
              ]
          #  cars: {car1: [{name: 奔驰, price: 200}, {name: 奔驰, price: 200}],
          #          car2: [{name: 奔驰, price: 200}, {name: 奔驰, price: 200}]}
          
          11.结果展示

          SpringBoot(Lombok + Spring Initailizr + yaml),image-20240313204905498,第23张

          12.yaml注意事项和细节说明
          1.注意事项