Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【建议】提取实体公共属性作为基类 #137

Closed
gwlvsjx opened this issue Sep 30, 2019 · 1 comment
Closed

【建议】提取实体公共属性作为基类 #137

gwlvsjx opened this issue Sep 30, 2019 · 1 comment

Comments

@gwlvsjx
Copy link

gwlvsjx commented Sep 30, 2019

Domain: BaseDomain (id create_time creator update_time updator version等),实体上用@DaTa需谨慎,重写的东西太多,建议Setter Getter
VO/DTO:BaseVo(Dto) 抽取公共属性
Repository: CustomJpaRepository 扩展jpa继承的方法,通过接口+Impl 实现单个接口个性化扩展
如:保存时,save 自动setCreateTime 更新的时候,自动setUpdateTime (通过继承,然后实现save方法实现)
自定义接口+Impl 中可以使用jdbcTemplate等(可以参考JPA文档)

为作者开源点赞!!以上仅为个人意见

@elunez
Copy link
Owner

elunez commented Oct 24, 2019

2.3dev分支版本已加入,通过注解自动更新时间

@Getter
@Setter
@MappedSuperclass
public class BaseEntity implements Serializable {

    // 删除标识
    @Column(name = "is_delete", columnDefinition = "bit default 0")
    private Boolean isDelete = false;

    @Column(name = "create_time")
    @CreationTimestamp
    private Timestamp createTime;

    @Column(name = "update_time")
    @UpdateTimestamp
    private Timestamp updateTime;

    public @interface New {}

    public @interface Update {}

    @Override
    public String toString() {
        ToStringBuilder builder = new ToStringBuilder(this);
        Field[] fields = this.getClass().getDeclaredFields();
        try {
            for (Field f : fields) {
                f.setAccessible(true);
                builder.append(f.getName(), f.get(this)).append("\n");
            }
        } catch (Exception e) {
            builder.append("toString builder encounter an error");
        }
        return builder.toString();
    }
}

@elunez elunez closed this as completed Oct 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants