Search in sources :

Example 11 with TableInfo

use of com.baomidou.mybatisplus.core.metadata.TableInfo in project onex-boot by zhangchaoxu.

the class EntityService method removeBatchByIds.

@Override
@Transactional(rollbackFor = Exception.class)
public boolean removeBatchByIds(Collection<?> list, int batchSize, boolean useFill) {
    String sqlStatement = getSqlStatement(SqlMethod.DELETE_BY_ID);
    TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
    return executeBatch(list, batchSize, (sqlSession, e) -> {
        if (useFill && tableInfo.isWithLogicDelete()) {
            if (entityClass.isAssignableFrom(e.getClass())) {
                sqlSession.update(sqlStatement, e);
            } else {
                T instance = tableInfo.newInstance();
                tableInfo.setPropertyValue(instance, tableInfo.getKeyProperty(), e);
                sqlSession.update(sqlStatement, instance);
            }
        } else {
            sqlSession.update(sqlStatement, e);
        }
    });
}
Also used : TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) Transactional(org.springframework.transaction.annotation.Transactional)

Example 12 with TableInfo

use of com.baomidou.mybatisplus.core.metadata.TableInfo in project RuoYi-Cloud-Plus by JavaLionLi.

the class BaseMapperPlus method insertOrUpdateBatch.

/**
 * 批量插入或更新(包含限制条数)
 */
default boolean insertOrUpdateBatch(Collection<T> entityList, int batchSize) {
    TableInfo tableInfo = TableInfoHelper.getTableInfo(this.currentModelClass());
    Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!");
    String keyProperty = tableInfo.getKeyProperty();
    Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!");
    return SqlHelper.saveOrUpdateBatch(this.currentModelClass(), this.currentMapperClass(), log, entityList, batchSize, (sqlSession, entity) -> {
        Object idVal = tableInfo.getPropertyValue(entity, keyProperty);
        String sqlStatement = SqlHelper.getSqlStatement(this.currentMapperClass(), SqlMethod.SELECT_BY_ID);
        return StringUtils.checkValNull(idVal) || CollectionUtils.isEmpty(sqlSession.selectList(sqlStatement, entity));
    }, (sqlSession, entity) -> {
        MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
        param.put(Constants.ENTITY, entity);
        String sqlStatement = SqlHelper.getSqlStatement(this.currentMapperClass(), SqlMethod.UPDATE_BY_ID);
        sqlSession.update(sqlStatement, param);
    });
}
Also used : MapperMethod(org.apache.ibatis.binding.MapperMethod) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo)

Example 13 with TableInfo

use of com.baomidou.mybatisplus.core.metadata.TableInfo 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 14 with TableInfo

use of com.baomidou.mybatisplus.core.metadata.TableInfo in project lamp-util by zuihou.

the class LampSqlInjector method getMethodList.

@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
    List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
    // 增加自定义方法
    methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE));
    methodList.add(new UpdateAllById(field -> !ArrayUtil.containsAny(new String[] { SuperEntity.CREATE_TIME_COLUMN, SuperEntity.CREATED_BY_COLUMN }, field.getColumn())));
    return methodList;
}
Also used : List(java.util.List) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) InsertBatchSomeColumn(com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn) ArrayUtil(cn.hutool.core.util.ArrayUtil) FieldFill(com.baomidou.mybatisplus.annotation.FieldFill) AbstractMethod(com.baomidou.mybatisplus.core.injector.AbstractMethod) SuperEntity(top.tangyh.basic.base.entity.SuperEntity) UpdateAllById(top.tangyh.basic.database.injector.method.UpdateAllById) DefaultSqlInjector(com.baomidou.mybatisplus.core.injector.DefaultSqlInjector) UpdateAllById(top.tangyh.basic.database.injector.method.UpdateAllById) AbstractMethod(com.baomidou.mybatisplus.core.injector.AbstractMethod) InsertBatchSomeColumn(com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn)

Example 15 with TableInfo

use of com.baomidou.mybatisplus.core.metadata.TableInfo 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)

Aggregations

TableInfo (com.baomidou.mybatisplus.core.metadata.TableInfo)53 MapperMethod (org.apache.ibatis.binding.MapperMethod)14 Transactional (org.springframework.transaction.annotation.Transactional)13 List (java.util.List)11 Serializable (java.io.Serializable)9 Field (java.lang.reflect.Field)8 TableInfoHelper (com.baomidou.mybatisplus.core.metadata.TableInfoHelper)7 AbstractMethod (com.baomidou.mybatisplus.core.injector.AbstractMethod)6 SqlMethod (com.baomidou.mybatisplus.core.enums.SqlMethod)5 InsertBatchSomeColumn (com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn)5 Collection (java.util.Collection)5 TableFieldInfo (com.baomidou.mybatisplus.core.metadata.TableFieldInfo)4 Constants (com.baomidou.mybatisplus.core.toolkit.Constants)4 ArrayList (java.util.ArrayList)4 Collectors (java.util.stream.Collectors)4 SqlSession (org.apache.ibatis.session.SqlSession)4 CollUtil (cn.hutool.core.collection.CollUtil)3 Convert (cn.hutool.core.convert.Convert)3 ReflectUtil (cn.hutool.core.util.ReflectUtil)3 FieldFill (com.baomidou.mybatisplus.annotation.FieldFill)3