lombok入门

pom.xml加入依赖

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>

不用lombok的User.java

public class User {
    private Integer id;
    private String name;

    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 User() {
    }

    public User(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

}

用lombok的UserHaveLombok.java

@NoArgsConstructor
@AllArgsConstructor
@Data
public class UserHaveLombok {
    private String name;
    private double age;
}
其中
@AllArgsConstructor用来指定全参数构造器
@NoArgsConstructor用来指定无参数构造器
@Data包含了@Getter @Setter @ToString @EqualsAndHashCode RequiredArgsConstructor注解

UserController.java

@RestController
public class UserController {

    @GetMapping(value = "/")
    public List<User> UserTest()
    {
        User user=new User();
        user.setId(2);
        user.setName("pan");
        List<User>list=new ArrayList<>();
        list.add(user);
        User user1=new User(3,"chen");
        list.add(user1);
        return list;
    }

    @RequestMapping(value = "userHaveLombok")
    public List<UserHaveLombok> haveLomboks()
    {
        List<UserHaveLombok>lists=new ArrayList<>();
        UserHaveLombok userHaveLombok=new UserHaveLombok();
        userHaveLombok.setAge(9);
        userHaveLombok.setName("panbingwen");
        lists.add(userHaveLombok);
        UserHaveLombok userHaveLombok1=new UserHaveLombok("chen",8);
        lists.add(userHaveLombok1);
        return lists;
    }
}

个人网站

posted @ 2019-04-13 22:13  一无所有王健林  阅读(134)  评论(0编辑  收藏  举报