lombok是什么?它是一个能帮我们消除那些必须要写但是重复的代码,比如setter,getter,构造函数之类的方法。
首先先简单说一下idea安装lombok,有2种方法:
1.直接从http://plugins.jetbrains.com/下载,然后放到idea安装文件下面的plugins,然后重启idea
2.在idea的settings(windows)或者preferences(mac),下找到plugins菜单,点击browse repositories,如图
然后搜索lombok,点击右侧下载,下载完成重启即可,如图
新建项目,在pom文件中加入lombok依赖,完整pom如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
<?xml version= "1.0" encoding= "utf-8" ?> <project xmlns= "http://maven.apache.org/pom/4.0.0" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation= "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion> 4.0 . 0 </modelversion> <groupid>com.dalaoyang</groupid> <artifactid>springboot_lombok</artifactid> <version> 0.0 . 1 -snapshot</version> <packaging>jar</packaging> <name>springboot_lombok</name> <description>springboot_lombok</description> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 1.5 . 9 .release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceencoding>utf- 8 </project.build.sourceencoding> <project.reporting.outputencoding>utf- 8 </project.reporting.outputencoding> <java.version> 1.8 </java.version> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-devtools</artifactid> <scope>runtime</scope> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <dependency> <groupid>org.projectlombok</groupid> <artifactid>lombok</artifactid> <version> 1.16 . 20 </version> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project> |
新建一个user类,这个是使用lombok的类,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package com.dalaoyang.entity; import lombok.allargsconstructor; import lombok.data; import lombok.noargsconstructor; /** * @author dalaoyang * @description * @project springboot_learn * @package com.dalaoyang * @email yangyang@dalaoyang.cn * @date 2018/5/7 */ @data @allargsconstructor @noargsconstructor public class user { private integer id; private string name; private string password; } |
新建一个usernolombok类,这个和原来一样,目的为了对比使用lombok的好处,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
package com.dalaoyang.entity; import java.util.objects; /** * @author dalaoyang * @description * @project springboot_learn * @package com.dalaoyang.entity * @email yangyang@dalaoyang.cn * @date 2018/5/7 */ public class usernolombok { private integer id; private string name; private string password; public integer getid() { return id; } public void setid(integer id) { this .id = id; } public string getname() { return name; } public void setname(string name) { this .name = name; } public string getpassword() { return password; } public void setpassword(string password) { this .password = password; } public usernolombok(integer id, string name, string password) { this .id = id; this .name = name; this .password = password; } public usernolombok() { } @override public boolean equals(object o) { if ( this == o) return true ; if (o == null || getclass() != o.getclass()) return false ; usernolombok that = (usernolombok) o; return objects.equals(id, that.id) && objects.equals(name, that.name) && objects.equals(password, that.password); } @override public int hashcode() { return objects.hash(id, name, password); } @override public string tostring() { return "usernolombok{" + "id=" + id + ", name='" + name + '\ '' + ", password='" + password + '\ '' + '}' ; } } |
然后新建一个usercontroller测试,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package com.dalaoyang.controller; import com.dalaoyang.entity.user; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.restcontroller; /** * @author dalaoyang * @description * @project springboot_learn * @package com.dalaoyang.controller * @email yangyang@dalaoyang.cn * @date 2018/5/7 */ @restcontroller public class usercontroller { @getmapping ( "/testuser" ) public user getuser(){ user user = new user(); user.setid( 1 ); user.setname( "dalaoyang" ); user.setpassword( "123" ); system.out.println(user.tostring()); return user; } } |
访问http://localhost:8080/testuser如图
lombok注解简介:
@data 标签,生成getter/setter tostring()等方法
@nonnull : 让你不在担忧并且爱上nullpointerexception
@cleanup : 自动资源管理:不用再在finally中添加资源的close方法
@setter/@getter : 自动生成set和get方法
@tostring : 自动生成tostring方法
@equalsandhashcode : 从对象的字段中生成hashcode和equals的实现
@noargsconstructor/@requiredargsconstructor/@allargsconstructor
自动生成构造方法
@data : 自动生成set/get方法,tostring方法,equals方法,hashcode方法,不带参数的构造方法
@value : 用于注解final类
@builder : 产生复杂的构建器api类
@sneakythrows : 异常处理(谨慎使用)
@synchronized : 同步方法安全的转化
@getter(lazy=true) :
@log : 支持各种logger对象,使用时用对应的注解,如:@log4j
源码下载 :大老杨码云
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.jianshu.com/p/df3705c54188