1.创建Maven项目,pom.xml文件内容如下
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.BUILD-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.huinongtx</groupId>
<artifactId>demolombok</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demolombok</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
2.创建实体类
包名:com.huinongtx.demolombok.demo
类名:DemoLombok
类内容:
package com.huinongtx.demolombok.demo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* Created by dengdashuai on 2018/12/13.
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DemoLombok {
int code;
String msg;
}
3.应用入口类
类名:DemolombokApplication
类内容:
package com.huinongtx.demolombok;
import com.huinongtx.demolombok.demo.DemoLombok;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@Slf4j
public class DemolombokApplication {
public static void main(String[] args) {
// @NoArgsConstructor生成无参构造
DemoLombok demoLombok1 = new DemoLombok();
// 通过@Data生成的set方法,进行赋值
demoLombok1.setCode(0);
demoLombok1.setMsg("success");
// 通过@Data生成的toString方法,输出demoLombok1对象时调用toString方法
System.out.println(demoLombok1);
// @AllArgsConstructor通过全部参数构造
DemoLombok demoLombok2 = new DemoLombok(1,"fail");
// 输出demoLombok2对象时调用toString方法
System.out.println(demoLombok2);
// @Builder构建实例对象
DemoLombok demoLombok3 = DemoLombok.builder().code(0).msg("通过@Builder").build();
System.out.println(demoLombok3);
// @Slf4j 注入log实例
log.info("测试@Slf4j");
SpringApplication.run(DemolombokApplication.class, args);
}
}
4.运行结果
re.png
QQ截图20181213101212.png
©著作权归作者所有:来自51CTO博客作者素颜猪的原创作品,如需转载,请注明出处,否则将追究法律责任