关于链式加载@Accessors

链式加载

chain为一个布尔值,如果为true生成的set方法返回this,为false生成的set方法是void类型。默认为false,除非当fluent为true时,chain默认则为true, chain = true 是,对对象设置时候可以使用Lambda表达式。

package com.jt.pojo;

import lombok.Data;
import lombok.experimental.Accessors;

@Data
@Accessors(chain=true)   //链式加载
public class User {
    private Integer id;
    private String name;
    private Integer age;
    private String sex;
}

链式加载底层实现

返回每个对象

public User setId(final Integer id) {
        this.id = id;
        return this;
    }

    public User setName(final String name) {
        this.name = name;
        return this;
    }

    public User setAge(final Integer age) {
        this.age = age;
        return this;
    }

    public User setSex(final String sex) {
        this.sex = sex;
        return this;
    }

启动链式加载和没启动链式加载区别

启动链式加载

	@Test
	public void insert() {
		User user =new User();
		user.setName("小小黑")
			.setAge(15)
			.setSex("女");
		userMapper.insert(user);
		System.out.println("入库成功");
	}

没启动链式加载

	@Test
	public void insert() {
		User user =new User();
		user.setName("小小黑")
		user.setAge(15)
		user.setSex("女");
		userMapper.insert(user);
		System.out.println("入库成功");
	}

@Accessors(fluent = “true/false”)
fluent为一个布尔值,中文含义是流畅的,默认为false, 如果为true 生成的get/set方法则没有set/get前缀,都是基础属性名, 且setter方法返回当前对象

@Data
@Accessors(fluent=true) //链式加载

public class User {
    private Integer id;
    private String name;
    //以下是加了注解之后的底层class
    public Integer id() {
        return this.id;
    }
    public String name() {
        return this.name;
    }
    public User id(final Integer id) {
        this.id = id;
        return this;
    }
    public User name(final String name) {
        this.name = name;
        return this;
    }

@Accessors(prefix = “s”)
使用prefix属性,getter和setter方法会忽视属性名的指定前缀(遵守驼峰命名)
prefix为一系列string类型,可以指定前缀,生成get/set方法时会去掉指定的前缀
()ps:没整明白,底层没有get/set方法了)

@Getter
@Accessors(prefix = "s")   //链式加载
public class User {
    private Integer id;
    private String name;
    private Integer age;
    private String sex;
}

猜你喜欢

转载自blog.csdn.net/SkyCloud_/article/details/108235864
今日推荐