当前位置: 首页 > news >正文

SpringBoot(Lombok + Spring Initailizr + yaml)

1.Lombok

1.基本介绍

image-20240313161718829

2.应用实例
1.pom.xml 引入Lombok,使用版本仲裁
    <!--导入springboot父工程--><parent><artifactId>spring-boot-starter-parent</artifactId><groupId>org.springframework.boot</groupId><version>2.5.3</version></parent><dependencies><!--配置maven项目场景启动器,自动导入和web相关的包--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--引入Lombok,使用版本仲裁--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies>
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注解说明(不常用)

image-20240313163329615

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

image-20240313170237189

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

image-20240313171249514

2.会在日志中输出

image-20240313171312777

2.Spring Initailizr(不推荐)

1.基本介绍

image-20240313171622129

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

image-20240313172411941

2.进行配置

image-20240313172738168

3.创建成功

image-20240313173041517

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

image-20240313173352413

2.配置完之后选择

image-20240313173437210

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

image-20240313173715463

3.yaml

1.基本说明

image-20240313174407089

2.yaml基本语法

image-20240313174915598

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

image-20240313175754697

2.对象

image-20240313175837930

3.数组

image-20240313175918900

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

image-20240313190536147

2.pom.xml引入依赖并刷新maven
    <!--导入springboot父工程--><parent><artifactId>spring-boot-starter-parent</artifactId><groupId>org.springframework.boot</groupId><version>2.5.3</version></parent><dependencies><!--配置maven项目场景启动器,自动导入和web相关的包--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--引入Lombok,使用版本仲裁--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies>
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<String> hobby;private Map<String, Object> wife;private Set<Double> salaries;private Map<String, List<Car>> 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 {@Resourceprivate 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.运行主程序(目前返回的值是空的)

image-20240313192941733

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

image-20240313194026901

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

image-20240313194047615

2.在pom.xml中添加依赖即可
    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><!--防止将该依赖传递到其他模块--><optional>true</optional></dependency>
3.运行主程序

image-20240313195153451

10.完整yml文件
monster: #前缀id: 100name: 牛魔王age: 500isMarried: falsebirth: 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<String, List<Car>>类型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.结果展示

image-20240313204905498

12.yaml注意事项和细节说明
1.注意事项
  • application.properties和application.yml如果有相同前缀值的绑定,则application.properties优先级高
  • 字符串无需加引号,但是加引号也没有问题
  • yaml配置文件如果不提示字段信息,则导入依赖即可
  • 如果添加依赖还不显示字段信息则安装YAML插件
2.细节说明
  • 其实不需要记住什么yaml的类型,只要能跟java对应上即可
  • 如果是对象或者map,则表示方式是
    • 换行key: value
    • {key1: value1, key2: value2}
  • 如果是数组或list,则表示方式是
    • 换行- value
    • [value1, value2]
http://www.lryc.cn/news/317453.html

相关文章:

  • 数据库基础知识超详细解析~‍(进阶/复习版)
  • 创建对象的方法有哪些
  • Oracle 10g字符编码
  • 掌握抽象基础之20个必备原则,看完你还不会,你打我
  • 设计模式 -- 2:策略模式
  • 【快速上手ProtoBuf】proto 3 语法详解
  • 人工智能的幽默“失误”
  • js的异步请求?
  • 华润对象存储(OBS)工具类
  • 强缓存和协商缓存的区别?
  • ChatGPT提问技巧——对抗性提示
  • openGauss使用BenchmarkSQL进行性能测试(上)
  • Java的线程池机制
  • EasyCode 插件的具体使用
  • Ypay源支付6.9无授权聚合免签系统可运营源码
  • SpringBoot+Vue项目报错(问题已解决)
  • DEAP 自定义交叉操作
  • ByText
  • Vcenter esxi web界面访问提示权限被拒绝
  • 掌握FilterOutputStream类!
  • YOLOv8改进 | 图像去雾 | 特征融合注意网络FFA-Net增强YOLOv8对于模糊图片检测能力(北大和北航联合提出)
  • Python (用户登录、身份归属地查询添加异常处理、绘制多角星、电影信息提取)
  • Set cancelled by MemoryScratchSinkOperator
  • Python 查找PDF中的指定文本并高亮显示
  • 岩土工程渗流问题之有限单元法:理论、模块化编程实现、开源程序应用
  • 解决 :nvrtc: error: invalid value for --gpu-architecture (-arch)
  • Rust教程:How to Rust-从开始之前到Hello World
  • 浅谈人工智能
  • OpenFeign服务接口调用
  • SQLiteC/C++接口详细介绍之sqlite3类(五)