关于链式加载@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;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值