当前位置: 首页 > 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]

相关文章:

SpringBoot(Lombok + Spring Initailizr + yaml)

1.Lombok 1.基本介绍 2.应用实例 1.pom.xml 引入Lombok&#xff0c;使用版本仲裁 <!--导入springboot父工程--><parent><artifactId>spring-boot-starter-parent</artifactId><groupId>org.springframework.boot</groupId><version&g…...

数据库基础知识超详细解析~‍(进阶/复习版)

文章目录 前言一、数据库的操作1.登入数据库2.创建数据库3.显示当前数据库4.使用数据库5.删除数据库 二、常用数据类型三、数据库的约束1约束类型2NULL约束3UNIQUE:唯一约束4DEFAULT&#xff1a;默认值约束5 PRIMARY KEY&#xff1a;主键约束6 FOREIGN KEY&#xff1a;外键约束…...

创建对象的方法有哪些

创建对象的方法主要取决于你使用的编程语言和上下文。下面我将列出一些主流编程语言中创建对象的方法&#xff1a; Python: 使用类定义和__init__方法&#xff1a; pythonclass MyClass: def __init__(self, name): self.name nameobj MyClass("Alice") 1.使用工厂…...

Oracle 10g字符编码

pl/sql developer查询数据时出现乱码&#xff0c;主要检查如下&#xff1a; 1、检查服务器编码 select * from v$nls_parameters;select * from nls_database_parameters;select userenv(language) from dual; 2、查看数据库可用字符集参数设置 select * from v$nls_valid_val…...

掌握抽象基础之20个必备原则,看完你还不会,你打我

抽象基础之20个必备原则 1. 面向对象编程&#xff08;OOP&#xff09;中抽象原则背后的基本思想是什么&#xff1f;2.抽象和封装的区别&#xff1f;3.提供一个现实生活中说明抽象的例子4.在编程语言中如何实现抽象&#xff1f;5.定义抽象类6.提供一个抽象类的真实世界场景7.解释…...

设计模式 -- 2:策略模式

目录 总结部分&#xff1a;策略模式的优点部分代码部分 总结部分&#xff1a; 策略模式和简单工厂模式很像 区别在于 简单工厂模式 需求的是由工程创造的类 去给客户直接答案 而策略模式在于 我有主体 一个主体 根据策略的不同来进行不同的计算 我的主体就负责收钱 然后调度相…...

【快速上手ProtoBuf】proto 3 语法详解

1 &#x1f351;字段规则&#x1f351; 消息的字段可以⽤下⾯⼏种规则来修饰&#xff1a; singular &#xff1a;消息中可以包含该字段零次或⼀次&#xff08;不超过⼀次&#xff09;。 proto3 语法中&#xff0c;字段默认使⽤该规则。repeated &#xff1a;消息中可以包含该…...

人工智能的幽默“失误”

人工智能迷惑行为大赏 随着ChatGPT热度的攀升&#xff0c;越来越多的公司也相继推出了自己的AI大模型&#xff0c;如文心一言、通义千问等。各大应用也开始内置AI玩法&#xff0c;如抖音的AI特效&#xff5e;在使用过程中往往会遇到一些问题&#xff0c;让你不得不怀疑&#x…...

js的异步请求?

在 JavaScript 中&#xff0c;进行异步请求通常涉及到使用 XMLHttpRequest 对象或者更现代的 Fetch API 或 Axios 库。这些工具可以帮助我们向服务器发送请求并在后台获取数据&#xff0c;而不会阻塞页面的其他操作。 下面是一个简单的示例&#xff0c;演示如何使用原生的 XML…...

华润对象存储(OBS)工具类

目录 一、备注二、工具类三、对象存储放在内网&#xff0c;如何实现外网访问 一、备注 1、ObjectBasicInfo、ObjectDetailInfo、ResultBody这三个类可自行替换或者去掉 二、工具类 package com.xxx.util;import com.amazonaws.HttpMethod; import com.amazonaws.auth.AWSStat…...

强缓存和协商缓存的区别?

协商缓存和强缓存是 HTTP 缓存机制中的两种不同的策略&#xff0c;用于减少网络请求并提高网页加载速度。它们之间的主要区别在于缓存的验证方式和服务器返回的响应头。 强缓存&#xff1a; 强缓存是基于过期时间&#xff08;Expires&#xff09;和缓存标识&#xff08;Cache…...

ChatGPT提问技巧——对抗性提示

ChatGPT提问技巧——对抗性提示 对抗性提示是一种允许模型生成能够抵御某些类型的攻击或偏差的文本的技术。这种技术可用于训练更健壮、更能抵御某些类型的攻击或偏差的模型。 要在 ChatGPT 中使用对抗性提示&#xff0c;应为模型提供一个提示&#xff0c;该提示的设计应使模…...

openGauss使用BenchmarkSQL进行性能测试(上)

一、前言 本文提供openGauss使用BenchmarkSQL进行性能测试的方法和测试数据报告。 BenchmarkSQL&#xff0c;一个JDBC基准测试工具&#xff0c;内嵌了TPC-C测试脚本&#xff0c;支持很多数据库&#xff0c;如PostgreSQL、Oracle和Mysql等。 TPC-C是专门针对联机交易处理系统…...

Java的线程池机制

Java的线程池机制是用来管理和调度多个线程的工具。通过线程池&#xff0c;可以避免频繁地创建和销毁线程&#xff0c;提高线程的复用率&#xff0c;减少资源消耗。 Java中提供了几种不同类型的线程池&#xff1a; 1、FixedThreadPool&#xff08;固定大小线程池&#xff09;…...

EasyCode 插件的具体使用

前言 EasyCode 是基于IntelliJ IDEA Ultimate版开发的一个代码生成插件&#xff0c;主要通过自定义模板&#xff08;基于velocity&#xff09;来生成各种你想要的代码。通常用于生成Entity、Dao、Service、Controller。如果你动手能力强还可以用于生成HTML、JS、PHP等代码。理…...

Ypay源支付6.9无授权聚合免签系统可运营源码

YPay是一款专为个人站长设计的聚合免签系统&#xff0c;YPay基于高性能的ThinkPHP 6.1.2 Layui PearAdmin架构&#xff0c;提供了实时监控和管理的功能&#xff0c;让您随时随地掌握系统运营情况。 说明 Ypay源支付6.9无授权聚合免签系统可运营源码 已搭建测试无加密版本…...

SpringBoot+Vue项目报错(问题已解决)

1、错误日志 2、分析原因&#xff1a; JWT strings must contain exactly 2 period characters. Found: 0 JWT字符串必须包含2个句号字符。发现:0 分析&#xff1a;可以判断出大概可能是token格式出现了问题 3、参考 http://t.csdnimg.cn/hfEiY 4、检查后端代码是否出现问…...

DEAP 自定义交叉操作

在遗传算法中&#xff0c;使用DEAP库来实现自定义的交叉操作可以非常灵活。如果你想模拟多个染色体的情况&#xff0c;通过在染色体的特定区间进行交叉&#xff0c;你需要自定义一个交叉函数。以下是一个示例&#xff0c;展示如何实现一个自定义的交叉函数&#xff0c;该函数允…...

ByText

getByText, queryByText, getAllByText, queryAllByText, findByText, findAllByText API​ getByText(// If youre using screen, then skip the container argument:container: HTMLElement,text: TextMatch,options?: {selector?: string *,exact?: boolean true,igno…...

Vcenter esxi web界面访问提示权限被拒绝

一、问题现象 原因 应该是在vCenter中添加主机的时候&#xff0c;将锁定模式设置成了严格。 二、解决过程 2.1 方式一 BMC登录主机&#xff0c;连接显示器和键盘。 输入账号密码&#xff0c;按F2进行设置&#xff0c;将会打开一个界面&#xff0c;第一个选项是设置密码&…...

19c补丁后oracle属主变化,导致不能识别磁盘组

补丁后服务器重启&#xff0c;数据库再次无法启动 ORA01017: invalid username/password; logon denied Oracle 19c 在打上 19.23 或以上补丁版本后&#xff0c;存在与用户组权限相关的问题。具体表现为&#xff0c;Oracle 实例的运行用户&#xff08;oracle&#xff09;和集…...

java_网络服务相关_gateway_nacos_feign区别联系

1. spring-cloud-starter-gateway 作用&#xff1a;作为微服务架构的网关&#xff0c;统一入口&#xff0c;处理所有外部请求。 核心能力&#xff1a; 路由转发&#xff08;基于路径、服务名等&#xff09;过滤器&#xff08;鉴权、限流、日志、Header 处理&#xff09;支持负…...

条件运算符

C中的三目运算符&#xff08;也称条件运算符&#xff0c;英文&#xff1a;ternary operator&#xff09;是一种简洁的条件选择语句&#xff0c;语法如下&#xff1a; 条件表达式 ? 表达式1 : 表达式2• 如果“条件表达式”为true&#xff0c;则整个表达式的结果为“表达式1”…...

C++.OpenGL (10/64)基础光照(Basic Lighting)

基础光照(Basic Lighting) 冯氏光照模型(Phong Lighting Model) #mermaid-svg-GLdskXwWINxNGHso {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-GLdskXwWINxNGHso .error-icon{fill:#552222;}#mermaid-svg-GLd…...

深入解析C++中的extern关键字:跨文件共享变量与函数的终极指南

&#x1f680; C extern 关键字深度解析&#xff1a;跨文件编程的终极指南 &#x1f4c5; 更新时间&#xff1a;2025年6月5日 &#x1f3f7;️ 标签&#xff1a;C | extern关键字 | 多文件编程 | 链接与声明 | 现代C 文章目录 前言&#x1f525;一、extern 是什么&#xff1f;&…...

大数据学习(132)-HIve数据分析

​​​​&#x1f34b;&#x1f34b;大数据学习&#x1f34b;&#x1f34b; &#x1f525;系列专栏&#xff1a; &#x1f451;哲学语录: 用力所能及&#xff0c;改变世界。 &#x1f496;如果觉得博主的文章还不错的话&#xff0c;请点赞&#x1f44d;收藏⭐️留言&#x1f4…...

Java线上CPU飙高问题排查全指南

一、引言 在Java应用的线上运行环境中&#xff0c;CPU飙高是一个常见且棘手的性能问题。当系统出现CPU飙高时&#xff0c;通常会导致应用响应缓慢&#xff0c;甚至服务不可用&#xff0c;严重影响用户体验和业务运行。因此&#xff0c;掌握一套科学有效的CPU飙高问题排查方法&…...

佰力博科技与您探讨热释电测量的几种方法

热释电的测量主要涉及热释电系数的测定&#xff0c;这是表征热释电材料性能的重要参数。热释电系数的测量方法主要包括静态法、动态法和积分电荷法。其中&#xff0c;积分电荷法最为常用&#xff0c;其原理是通过测量在电容器上积累的热释电电荷&#xff0c;从而确定热释电系数…...

在Ubuntu24上采用Wine打开SourceInsight

1. 安装wine sudo apt install wine 2. 安装32位库支持,SourceInsight是32位程序 sudo dpkg --add-architecture i386 sudo apt update sudo apt install wine32:i386 3. 验证安装 wine --version 4. 安装必要的字体和库(解决显示问题) sudo apt install fonts-wqy…...

【Nginx】使用 Nginx+Lua 实现基于 IP 的访问频率限制

使用 NginxLua 实现基于 IP 的访问频率限制 在高并发场景下&#xff0c;限制某个 IP 的访问频率是非常重要的&#xff0c;可以有效防止恶意攻击或错误配置导致的服务宕机。以下是一个详细的实现方案&#xff0c;使用 Nginx 和 Lua 脚本结合 Redis 来实现基于 IP 的访问频率限制…...