在使用lombok @Builder注解遇到的问题

使用@Builder注解的时候可直接用对应类名点字段名最后.build() 这样构建出一个对应类的实例:

import java.util.Date;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;
import lombok.Builder;
import lombok.Data;

@Data
@Builder
@TableName("website")
public class Website{

    @TableId(value = "id",type = IdType.AUTO)
    private Integer id;

    /**
     * 网站名称
     */
    private String name;
    /**
     * url 域名
     */
    private String url;
    /**
     * 是否删除 1:删除,0:没有删除
     */
    @TableField("is_del")
    private Integer isDel;
    /**
     * 拼音缩写
     */
    private String py;
    /**
     * 创建人
     */
    @TableField("create_user")
    private Integer createUser;
    /**
     * 修改人
     */
    @TableField("update_user")
    private Integer updateUser;
    /**
     * 创建时间
     */
    @TableField("create_date")
    private Date createDate;
    /**
     * 修改时间
     */
    @TableField("update_date")
    private Date updateDate;
    
}

 

 

这样定义号一个类,在使用的时候就可以直接用以下方式构造实例:

public static void main(String[] args) {
    //构造一个网站名称为"XXXX网站"的实例,其他的都为null
    Website website1 = Website.builder()
            .name("XXXX网站")
            .build();
    System.out.println(website1.toString());

    //构造一个完整的实例
    Website website2 = Website.builder()
            .id(1)
            .name("XXXX网站")
            .url("www.baidu.com")
            .isDel(0)
            .py("XXXXWZ")
            .createDate(new Date())
            .createUser(1)
            .build();
    System.out.println(website2.toString());
}

输出结果:

website1==>>Website(id=null, name=XXXX网站, url=null, isDel=null, py=null, createUser=null, updateUser=null, createDate=null, updateDate=null)
website2==>>Website(id=1, name=XXXX网站, url=www.baidu.com, isDel=0, py=XXXXWZ, createUser=1, updateUser=null, createDate=Mon Jun 24 15:45:49 CST 2019, updateDate=null)
 

注意:

但是在使用@Builder注解时可能会遇到报错提示说没有找到对应构造方法的错误:

{"status":500,"message":"nested exception is org.apache.ibatis.executor.ExecutorException: No constructor found in com.qgs.service.contract.entity.Website matching [java.lang.Integer, java.lang.String,java.lang.String, java.lang.Integer, java.lang.String,java.sql.Timestamp, java.lang.Integer, java.lang.Integer, java.sql.Timestamp]"}

会提示我们说对应的类没有对应的有参构造函数;

解决方法:

1、首先我们可以在对应的类中添加构造方法(注意有参和无参都要):

public Website() {
}

public Website(Integer id, String name, String url, Integer isDel, String py, Integer createUser, Integer updateUser, Date createDate, Date updateDate) {
    this.id = id;
    this.name = name;
    this.url = url;
    this.isDel = isDel;
    this.py = py;
    this.createUser = createUser;
    this.updateUser = updateUser;
    this.createDate = createDate;
    this.updateDate = updateDate;
}

 

2、或是直接在类上加上以下注解:

@AllArgsConstructor
@NoArgsConstructor

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不喜欢吃猫的鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值