【Video & Communication Platform】
http://study.163.com/course/introduction.htm?courseId=1004329008&utm_campaign=commission&utm_source=400000000155061&utm_medium=share
http://study.163.com/course/introduction.htm?courseId=1004638001&utm_campaign=commission&utm_source=400000000155061&utm_medium=share
https://gitee.com/happyangellxq520/spring-boot
à Spring Boot communication platform
http://412887952-qq-com.iteye.com/blog/2321532
foreword
In the previous article, I installed Lombok . For those who haven't installed it yet, you need to install it first, otherwise you won't be able to experience the magical effects brought by Lombok . Installation is not the purpose, use is the last word. This article will introduce it from the perspective of use.
Imprint
(2)lombok:1.16.18;
1. Preparation
1.1 Install Lombok
For specific installation steps, see the previous article.
1.2 Prepare a simple spring boot project
Just take a spring boot project, if you don't know it yet, start with the hello world of spring boot .
2. Using Lombok in spring boot
2.1 New dependencies
Add the dependency of lombok to the pom.xml file of the project :
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency>
2.2 Add a Girl entity class
( 1 ) Code not using Lombok
public class Girl { private int id;//Primary key. private String name;//姓名. private int age;//年龄,女孩的年龄怎么能随便告诉你呢! private double weight;//体重. 这个问女孩,女孩会 ̄へ ̄的. public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { returnage; } public void setAge(int age) { this.age = age; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } }
在之前的代码中需要手动生成getter and setter代码。
(2)使用Lombok的代码
import lombok.Data; @Data public class Girl { private int id;//主键. private String name;//姓名. private int age;//年龄,女孩的年龄怎么能随便告诉你呢! private double weight;//体重. 这个问女孩,女孩会 ̄へ ̄的. }
这是什么鬼,好干净好简单哦,这就是Lombok带给我们的。这里在也看不到长长的getter and setter方法了。
2.3 Girl调用
那么使用Lombok之后的属性是否能被getter和setter呢?好想看看那个女孩的体重哦(你想看的不是体重吧,老实说是不是Cup Size),看下调用的代码:
Girl girl = new Girl(); girl.setId(1); girl.setName("Angelababy"); girl.setAge(29); girl.setWeight(45);//单位:千克 System.out.println("姓名:"+girl.getName()+",年龄:"+girl.getAge()+",体重:"+girl.getWeight());
可以获取到,这样使用Lombok之后,就会使得代码变得清爽很多。对于Lombok到这里就结束了,但是Lombok的还需要大家自己去挖掘,这艰巨的任务还是留给你们吧,我还是去new girl()。