Search in sources :

Example 1 with SuperEntity

use of top.tangyh.basic.base.entity.SuperEntity in project lamp-util by zuihou.

the class LampMetaObjectHandler method fillId.

private void fillId(MetaObject metaObject) {
    if (uidGenerator == null) {
        // 这里使用SpringUtils的方式"异步"获取对象,防止启动时,报循环注入的错
        uidGenerator = SpringUtils.getBean(UidGenerator.class);
    }
    Long id = uidGenerator.getUid();
    // 1. 继承了SuperEntity 若 ID 中有值,就不设置
    if (metaObject.getOriginalObject() instanceof SuperEntity) {
        Object oldId = ((SuperEntity) metaObject.getOriginalObject()).getId();
        if (oldId != null) {
            return;
        }
        Object idVal = StrPool.STRING_TYPE_NAME.equals(metaObject.getGetterType(SuperEntity.FIELD_ID).getName()) ? String.valueOf(id) : id;
        this.setFieldValByName(SuperEntity.FIELD_ID, idVal, metaObject);
        return;
    }
    // 2. 没有继承SuperEntity, 但主键的字段名为:  id
    if (metaObject.hasGetter(SuperEntity.FIELD_ID)) {
        Object oldId = metaObject.getValue(SuperEntity.FIELD_ID);
        if (oldId != null) {
            return;
        }
        Object idVal = StrPool.STRING_TYPE_NAME.equals(metaObject.getGetterType(SuperEntity.FIELD_ID).getName()) ? String.valueOf(id) : id;
        this.setFieldValByName(SuperEntity.FIELD_ID, idVal, metaObject);
        return;
    }
    // 3. 实体没有继承 Entity 和 SuperEntity,且 主键名为其他字段
    TableInfo tableInfo = TableInfoHelper.getTableInfo(metaObject.getOriginalObject().getClass());
    if (tableInfo == null) {
        return;
    }
    // 主键类型
    Class<?> keyType = tableInfo.getKeyType();
    if (keyType == null) {
        return;
    }
    // id 字段名
    String keyProperty = tableInfo.getKeyProperty();
    Object oldId = metaObject.getValue(keyProperty);
    if (oldId != null) {
        return;
    }
    // 反射得到 主键的值
    Field idField = ReflectUtil.getField(metaObject.getOriginalObject().getClass(), keyProperty);
    Object fieldValue = ReflectUtil.getFieldValue(metaObject.getOriginalObject(), idField);
    // 判断ID 是否有值,有值就不
    if (ObjectUtil.isNotEmpty(fieldValue)) {
        return;
    }
    Object idVal = keyType.getName().equalsIgnoreCase(StrPool.STRING_TYPE_NAME) ? String.valueOf(id) : id;
    this.setFieldValByName(keyProperty, idVal, metaObject);
}
Also used : Field(java.lang.reflect.Field) UidGenerator(com.baidu.fsg.uid.UidGenerator) SuperEntity(top.tangyh.basic.base.entity.SuperEntity) MetaObject(org.apache.ibatis.reflection.MetaObject) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo)

Example 2 with SuperEntity

use of top.tangyh.basic.base.entity.SuperEntity in project lamp-util by zuihou.

the class SuperCacheServiceImpl method getId.

protected Object getId(T model) {
    if (model instanceof SuperEntity) {
        return ((SuperEntity) model).getId();
    } else {
        // 实体没有继承 Entity 和 SuperEntity
        TableInfo tableInfo = TableInfoHelper.getTableInfo(getEntityClass());
        if (tableInfo == null) {
            return null;
        }
        // 主键类型
        Class<?> keyType = tableInfo.getKeyType();
        if (keyType == null) {
            return null;
        }
        // id 字段名
        String keyProperty = tableInfo.getKeyProperty();
        // 反射得到 主键的值
        Field idField = ReflectUtil.getField(getEntityClass(), keyProperty);
        return ReflectUtil.getFieldValue(model, idField);
    }
}
Also used : Field(java.lang.reflect.Field) SuperEntity(top.tangyh.basic.base.entity.SuperEntity) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo)

Example 3 with SuperEntity

use of top.tangyh.basic.base.entity.SuperEntity in project lamp-util by zuihou.

the class LampMetaObjectHandler method created.

private void created(MetaObject metaObject) {
    SuperEntity entity = (SuperEntity) metaObject.getOriginalObject();
    if (entity.getCreateTime() == null) {
        this.setFieldValByName(Entity.CREATE_TIME, LocalDateTime.now(), metaObject);
    }
    if (entity.getCreatedBy() == null || entity.getCreatedBy().equals(0)) {
        Object userIdVal = StrPool.STRING_TYPE_NAME.equals(metaObject.getGetterType(SuperEntity.CREATED_BY).getName()) ? String.valueOf(ContextUtil.getUserId()) : ContextUtil.getUserId();
        this.setFieldValByName(Entity.CREATED_BY, userIdVal, metaObject);
    }
}
Also used : SuperEntity(top.tangyh.basic.base.entity.SuperEntity) MetaObject(org.apache.ibatis.reflection.MetaObject)

Aggregations

SuperEntity (top.tangyh.basic.base.entity.SuperEntity)3 TableInfo (com.baomidou.mybatisplus.core.metadata.TableInfo)2 Field (java.lang.reflect.Field)2 MetaObject (org.apache.ibatis.reflection.MetaObject)2 UidGenerator (com.baidu.fsg.uid.UidGenerator)1