# 从Java创建实体

上面章节我们谈到使用可视化的方式来创建实体,对于比较复杂的场景,有些情况下需要使用原生 Java 代码来实现。更多细节请参考实体开发规范 (opens new window)

以下是一个简单的实体定义:

查看实体代码
package com.app.entity;

import fly.core.data.annotation.*;
import fly.core.data.domains.*;
import fly.core.meta.annotation.*;
import mcore.data.annotation.*;
import mcore.data.enums.*;
import java.util.Date;

@Entity
@Summary("合同")
public class Contract {

    @Id
    @Summary("唯一ID")
    @Column(length = 36, filterable = true, sortable = true)
    private String id;

    @Summary("合同名称")
    @Column(length = 150, filterable = true, sortable = true)
    private String name;

    @Summary("合同编码")
    @Column(length = 50, filterable = true, sortable = true)
    private String code;

    @Summary("销售经理 ID")
    @Column(length = 36, filterable = true, sortable = true)
    private String salesId;

    @Summary("所属部门 ID")
    @Column(length = 36, filterable = true, sortable = true)
    private String deptId;

    @Summary("合同描述")
    @Column(length = 2000, filterable = true, sortable = true)
    private String description;

    @Summary("签约日期")
    @Column(filterable = true, sortable = true)
    private Date signDate;

    @Summary("合同状态")
    @Column(length = 15, filterable = true, sortable = true)
    private String status;

    @Summary("租户号")
    @Column(length = 60, filterable = true, sortable = true)
    private String tenantId;

    @Summary("是否删除")
    @Column(nullable = true, filterable = true)
    private boolean isDeleted;

    @Summary("创建人")
    @Column(length = 36, filterable = true, sortable = true)
    @CreatedBy(nullable = true)
    private String createdBy;

    @Summary("创建人姓名")
    @Column(length = 150, filterable = true, sortable = true)
    @CreatedByName(nullable = true)
    private String createdByName;

    @Summary("创建时间")
    @Column(filterable = true, sortable = true)
    @CreatedAt(nullable = true)
    private Date createdAt;

    @Summary("更新人")
    @Column(length = 36, filterable = true, sortable = true)
    @UpdatedBy(nullable = true)
    private String updatedBy;

    @Summary("更新人姓名")
    @Column(length = 150, filterable = true, sortable = true)
    @UpdatedByName(nullable = true)
    private String updatedByName;

    @Summary("更新时间")
    @Column(filterable = true, sortable = true)
    @UpdatedAt(nullable = true)
    private Date updatedAt;


    public String getId(){
        return this.id;
    }

    public void setId(String id){
        this.id=id;
    }

    public String getName(){
        return this.name;
    }

    public void setName(String name){
        this.name=name;
    }

    public String getCode(){
        return this.code;
    }

    public void setCode(String code){
        this.code=code;
    }

    public String getSalesId(){
        return this.salesId;
    }

    public void setSalesId(String salesId){
        this.salesId=salesId;
    }

    public String getDeptId(){
        return this.deptId;
    }

    public void setDeptId(String deptId){
        this.deptId=deptId;
    }

    public String getDescription(){
        return this.description;
    }

    public void setDescription(String description){
        this.description=description;
    }

    public Date getSignDate(){
        return this.signDate;
    }

    public void setSignDate(Date signDate){
        this.signDate=signDate;
    }

    public String getStatus(){
        return this.status;
    }

    public void setStatus(String status){
        this.status=status;
    }

    public String getTenantId(){
        return this.tenantId;
    }

    public void setTenantId(String tenantId){
        this.tenantId=tenantId;
    }

    public boolean getIsDeleted(){
        return this.isDeleted;
    }

    public void setIsDeleted(boolean isDeleted){
        this.isDeleted=isDeleted;
    }

    public String getCreatedBy(){
        return this.createdBy;
    }

    public void setCreatedBy(String createdBy){
        this.createdBy=createdBy;
    }

    public String getCreatedByName(){
        return this.createdByName;
    }

    public void setCreatedByName(String createdByName){
        this.createdByName=createdByName;
    }

    public Date getCreatedAt(){
        return this.createdAt;
    }

    public void setCreatedAt(Date createdAt){
        this.createdAt=createdAt;
    }

    public String getUpdatedBy(){
        return this.updatedBy;
    }

    public void setUpdatedBy(String updatedBy){
        this.updatedBy=updatedBy;
    }

    public String getUpdatedByName(){
        return this.updatedByName;
    }

    public void setUpdatedByName(String updatedByName){
        this.updatedByName=updatedByName;
    }

    public Date getUpdatedAt(){
        return this.updatedAt;
    }

    public void setUpdatedAt(Date updatedAt){
        this.updatedAt=updatedAt;
    }
}

注意几个关键点:

  • @Entity 注解表示这是一个实体类
  • @Id 注解表示这是一个主键字段
  • @Summary 注解表示字段的中文名称(生成Swagger文档时会用到)
  • @Column 注解表示字段的属性,如长度、是否可为空、是否可排序、是否可过滤等
  • @CreatedBy@CreatedByName@CreatedAt@UpdatedBy@UpdatedByName@UpdatedAt 注解表示这些字段是系统自动维护的字段,不需要手动维护
  • getset 方法是必须的,Fly框架会通过反射来调用这些方法

# 自动生成数据库表

在完成实体定义后,我们需要生成数据库表。Fly框架提供了一个注解来完成这个工作。在项目的 Application.java 文件中添加如下配置:

@EnableMigration
public class Application {
    ...
}

# JAVA代码使用JSON实体

在一些场景下,我们需要在 java 代码中引用已经使用JSON格式定义的实体。假如我们已经通过定义了一个合同实体(Contract.json),具体可以参考初始化实体章节。

在 dao 提供的实体操作方法中,部分方法可以直接传递实体名,源码参考:

CriteriaQuery<RowMap> createQuery(String entityName);

可见方法参数为 String 类型且参数名为 entityName,可以直接使用实体名:

List<RowMap> list = dao.createQuery("Contract").list();

但是这种查询操作一般返回类型都是 map 类型,为方便对属性进行set/get,可以创建一个实体对应的类进行实体映射,表示当前类映射到某个实体。

使用@MappingToEntity注解,注解中 name 指定实体名:

import fly.core.data.annotation.MappingToEntity;
import fly.core.data.model.AbstractExtensible;

// 表示映射到 Contract 实体
@MappingToEntity(name = "Contract")
public class Contract extend AbstractExtensible {
    
    private String id;

    public String getId(){
        return this.id;
    }

    public void setId(String id){
        this.id=id;
    }

}

实体继承AbstractExtensible类是可选的,我们可以像示例中的 id 一样明确定义所有字段属性,但由于我们是在 IDE 界面中增添字段,为了在 IDE 中添加后无需修改就能应用,继承AbstractExtensible类可以省略一些代码,其他未像 id 一样定义的字段属性会保存到父类的 properties 属性中。

在使用实体类时,可以直接使用实体类进行查询,例如:

List<Contract> list = dao.createQuery(Contract.class).list();
list.get(0).getId();

# JSON实体操作

假如不创建 Java 类的情况,我们也可以使用 JSON 格式定义实体。例如:

Map<String, Object> map = new HashMap<>();
map.put("name", "合同名称");
map.put("code", "HT-001");
// 构造 JSON 定义的 实体对象
EntityType entityType = dao.mustGetEntityType("Contract");
dao.insert(entityType, map );
// update, delete 同样适用
顶部