java实体类继承父类,部分字段不用

有个场景,实体类继承了公共的实体类,但是有些属性,我不需要,那么该如何处理呢,这里介绍2种办法供参考

@Data
@TableName("t_attachment")
public class AttachmentPo extends SuperEntity{

}

其中这个superEntity有个属性

@TableField(
value = "create_by_name",
fill = FieldFill.INSERT
)
private String createByName;

如果正常来说,我的AttachmentPo 对应的数据库表就应该添加一个这个create_by_name属性,但实际上我不需要这个字段,那数据库如果不想添加这个字段,可以在实体类中排除下,

方法一:

在AttachmentPo 中添加

public class AttachmentPo extends SuperEntity{

 @TableField(exist = false)
private String createByName;

}

主要就是这个注解起作用,将其排除

方法二:

@Data
@TableName(value = "t_attachment",excludeProperty = {"createByName","lastUpdateByName"})
public class AttachmentPo extends SuperEntity{

}

这个就好用些,排除一些不要的字段。

建议采用第二种方法

 

posted @ 2026-02-27 11:22  多多指教~  阅读(32)  评论(0)    收藏  举报