Entity 实体 字段映射

Entity ComplexModel.java

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@SuperBuilder
@Table(value = "t_complex_model", comment = "测试")
public class ComplexModel extends BaseEntity {

    @NotBlank
    String name;

    /**
     * 查询字典表 sys_dict 中获取数据
     * 通过注解 @Sql 获取 label 值
     */
    @Embedded(columnPrefix="material_type_")
    @JsonAlias("materialType")
    @JsonDeserialize(using = EntityWithCodePropertyDeserializer.class)
    @Sql(value = "select name code, label from sys_dict WHERE type = 'MATERIAL_TYPE'  AND name = :name", params="id@materialType.code", nullWhenParamsIsNull="code")
    @DictType(type = "MATERIAL_TYPE") // 可以省略 从@Sql 获取label
    @DictValueCheck(type = "MATERIAL_TYPE")
    DictValue materialType;

    /**
     * DictUtils.fillDictLabel(this) 手动填充label
     */
    @Embedded(columnPrefix="unit_")
    @JsonAlias("unit")
    @JsonDeserialize(using = EntityWithCodePropertyDeserializer.class)
    @DictType(type = "UNIT")
    @DictValueCheck(type = "UNIT")
    DictValue unit;

    /**
     * 用于 BaseCodeEntity 的关联,拥有 id , 区别于DictValue
     */
//    @Embedded(columnPrefix="plant_")
//    @JsonAlias("plantCode")
//    @JsonDeserialize(using = EntityWithCodePropertyDeserializer.class)
//    @Sql(value = "SELECT id, code, description from core_plant WHERE id = :id", params="id@plant.id", nullWhenParamsIsNull="id")
//    private IdCodeValue plant;
    /**
     * 使用注解 @JsonFormat(shape = JsonFormat.Shape.OBJECT) 可以不使用 @JsonValue
     */
    CodeDescription.CategoryEnum category;

    @Column(comment = "状态")
    WorkStatusEnum workStatus;

    @Embedded(columnPrefix="category_")
    @JsonAlias("categoryDict")
    @JsonDeserialize(using = EntityWithCodePropertyDeserializer.class)
    @DictType(type = "CategoryEnum")
    @DictValueCheck(type = "CategoryEnum")
    DictValue categoryDict;

    @Column(comment = "年龄", nullable = false)
    private Integer age;

    @Column(comment = "出生时间")
    private LocalDate birthday;

    /**
     * 必须使用 @JsonValue, 如果使用 @JsonFormat 数据写入会有问题
     */
    @Column(comment = "分类", value = "category_list", nullable = false)
    private List<CodeDescription.CategoryEnum> categoryList;

    @Column(comment = "字典分类", value = "category_dict_list", nullable = false)
    @DictType(type = "CategoryEnum")
    private List<DictValue> categoryDictList;

    @Column(comment = "婚否", nullable = false)
    private Boolean marriage;

    @Column(comment = "附件", columnDefinition = "text", value = "attachment", nullable = false)
    private List<Map<String, Object>> attachmentList;

    @Column(comment = "学习经历", columnDefinition = "text", value = "school_experience")
    private List<List<String>> schoolExperienceList;

    @Column(columnDefinition = "text", value = "map", nullable = false)
    private Map<String, Object> map;

    EmbeddedValue embeddedValue;

    @ManyToOne(value = "packaging_group_id", parentTable = "core_group", comment = "包装组id")
    @JsonAlias("packagingGroupId")
    @JsonDeserialize(using = EntityWithLongIdPropertyDeserializer.class)
    private ObjectGroup packagingGroup;

    @Column(value = "packaging_group_code", comment = "包装组code")
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private String packagingGroupCode;

    @ManyToMany(thirdPartyTable = "pur_order_type_item_config",
            referenceTable = "pur_item_config", referenceColumnName = "item_id", columnDefinition = "type_id")
    private List<ItemConfig> itemConfigList;

    @ManyToOne(value = "core_code_group_id", parentTable = "core_code_group")
    @JsonAlias("codeGroupId")
    @JsonDeserialize(using = EntityWithLongIdPropertyDeserializer.class)
    private CodeGroup codeGroup;

    @OneToMany(joinValue = "business_partner_id",
            subTable = "core_business_partner_role",
            cascadeInsertOrUpdate = true, reversePropertyName = "businessPartnerId", cascadeDelete = false)
    private List<Role> roleList;

    @Select(table = "mm_classification", joinValue = "material_id", referencePropertyName = "materialId")
    private List<Classification> classificationList;

    @Select(table = "core_code_assign", joinValue = "core_code_group_id", referencePropertyName = "id", oneToOne= true)
    private CodeAssign codeAssign;

    @Sql(value = "select * from t_person where id = :id AND name like :title", params = "id@person.id, title@title")
    private Person p3;

    @AllArgsConstructor
    @Getter
    @JsonFormat(shape = JsonFormat.Shape.OBJECT)
    public enum WorkStatusEnum {
        UNFINISHED(0, "未完成"),
        FINISHED(1, "已完成");
        private static final Map<Integer, WorkStatusEnum> codeMap = new HashMap<>();

        static {
            for (WorkStatusEnum e : values()) {
                codeMap.put(e.code, e);
            }
        }

        private final int code;
        private final String label;

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

        /**
         * code枚举 必须重写toString()
         *
         * @return
         */
        @Override
        public String toString() {
            return String.valueOf(code);
        }

        public static WorkStatusEnum valueOfCode(int code) {
            return codeMap.get(code);
        }
    }
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class EmbeddedValue implements JsonStringToObjectConverterFactory.JsonValue {

    @DictType(type = "MATERIAL_TYPE") // 可以省略 从@Sql 获取label
    DictValue dictValue;

    String text;

}

value value.json

{
        "name": "Rick.Xu",
        "materialType": "HIBE",
        "unit": "EA",
        "category": "MATERIAL",
        "status": "0",
        "categoryDict": "SALES_ORG",
        "age": 34,
        "birthday": "2021-12-26",
        "categoryList": [
            "MATERIAL",
            "PURCHASING_ORG"
        ],
        "categoryDictList": [
            "MATERIAL",
            "PURCHASING_ORG"
        ],
        "marriage": true,
        "attachmentList": [
            {
                "name": "picture",
                "url": "baidu.com"
            }
        ],
        "schoolExperienceList": [
            [
                "2023-11-11",
                "苏州大学"
            ],
            [
                "2019-11-11",
                "苏州中学"
            ]
        ],
        "map": {
            "name": "picture",
            "url": "baidu.com"
        },
        "embeddedValue": {
            "dictValue": {
                "code": "HIBE",
                "label": "耗材用品"
            },
            "text": "text"
        }
}

response Json Value

{
    "success": true,
    "code": 200,
    "message": "OK",
    "data": {
        "id": "856929212655734784",
        "createBy": "1",
        "createTime": "2024-08-19 14:23:43",
        "updateBy": "1",
        "updateTime": "2024-08-19 14:23:43",
        "name": "Rick.Xu",
        "materialType": {
            "code": "HIBE",
            "label": "耗材用品"
        },
        "unit": {
            "code": "EA",
            "label": "个"
        },
        "category": "MATERIAL",
        "categoryDict": {
            "code": "SALES_ORG"
        },
        "age": 34,
        "birthday": "2021-12-26",
        "categoryList": [
            "MATERIAL",
            "PURCHASING_ORG"
        ],
        "categoryDictList": [
            {
                "code": "MATERIAL"
            },
            {
                "code": "PURCHASING_ORG"
            }
        ],
        "marriage": true,
        "attachmentList": [
            {
                "name": "picture",
                "url": "baidu.com"
            }
        ],
        "schoolExperienceList": [
            [
                "2023-11-11",
                "苏州大学"
            ],
            [
                "2019-11-11",
                "苏州中学"
            ]
        ],
        "map": {
            "name": "picture",
            "url": "baidu.com"
        },
        "embeddedValue": {
            "dictValue": {
                "code": "HIBE",
                "label": "耗材用品"
            },
            "text": "text"
        }
    }
}