lombok的@Builder创建或修改对象新玩法

pom依赖 

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>0.10.2</version>
</dependency>

创建或修改对象新玩法

import lombok.*;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;

@Getter
@Slf4j
@ToString
@Builder(toBuilder = true)
public class TestBuilderController {

    private String name;
    private int age;

    public static void main(String[] args) {
        TestBuilderController testBuilderController = TestBuilderController.builder().age(23).name("small").build();
        log.info("创建【{}】",JSON.toJSONString(testBuilderController));
        testBuilderController=  testBuilderController.toBuilder().age(24).name("sun").build();
        log.info("修改【{}】",JSON.toJSONString(testBuilderController));


    }
}

 执行结果:

创建【{"age":23,"name":"small"}】
修改【{"age":24,"name":"sun"}】
posted @ 2021-01-27 18:24  活出自己范儿  Views(549)  Comments(0Edit  收藏  举报