Search in sources :

Example 6 with TableInfo

use of com.baomidou.mybatisplus.core.metadata.TableInfo in project ballcat by ballcat-projects.

the class ExtendServiceImpl method saveOrUpdate.

/**
 * TableId 注解存在更新记录,否插入一条记录
 * @param entity 实体对象
 * @return boolean
 */
@Transactional(rollbackFor = Exception.class)
@Override
public boolean saveOrUpdate(T entity) {
    if (null != entity) {
        TableInfo tableInfo = TableInfoHelper.getTableInfo(this.entityClass);
        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!");
        Object idVal = tableInfo.getPropertyValue(entity, tableInfo.getKeyProperty());
        return StringUtils.checkValNull(idVal) || Objects.isNull(getById((Serializable) idVal)) ? save(entity) : updateById(entity);
    }
    return false;
}
Also used : Serializable(java.io.Serializable) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with TableInfo

use of com.baomidou.mybatisplus.core.metadata.TableInfo in project ballcat by ballcat-projects.

the class ExtendServiceImpl 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 8 with TableInfo

use of com.baomidou.mybatisplus.core.metadata.TableInfo in project ballcat by ballcat-projects.

the class ExtendServiceImpl method removeById.

@Override
public boolean removeById(Serializable id, boolean useFill) {
    TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
    if (useFill && tableInfo.isWithLogicDelete()) {
        if (entityClass.isAssignableFrom(id.getClass())) {
            return SqlHelper.retBool(getBaseMapper().deleteById(id));
        }
        T instance = tableInfo.newInstance();
        tableInfo.setPropertyValue(instance, tableInfo.getKeyProperty(), id);
        return removeById(instance);
    }
    return SqlHelper.retBool(getBaseMapper().deleteById(id));
}
Also used : TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo)

Example 9 with TableInfo

use of com.baomidou.mybatisplus.core.metadata.TableInfo in project JBM by numen06.

the class MasterDataServiceImpl method saveOrUpdateBatch.

@Transactional(rollbackFor = Exception.class)
@Override
public boolean saveOrUpdateBatch(Collection<Entity> entityList, int batchSize) {
    Assert.notEmpty(entityList, "error: entityList must not be empty");
    Class<?> cls = currentModelClass();
    TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
    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!");
    int size = entityList.size();
    executeBatch(sqlSession -> {
        int i = 1;
        for (Entity entity : entityList) {
            Object idVal = ReflectionKit.getMethodValue(cls, entity, keyProperty);
            if (StringUtils.checkValNull(idVal) || Objects.isNull(getById((Serializable) idVal))) {
                sqlSession.insert(sqlStatement(SqlMethod.INSERT_ONE), entity);
            } else {
                MapperMethod.ParamMap<Entity> param = new MapperMethod.ParamMap<>();
                param.put(Constants.ENTITY, entity);
                sqlSession.update(sqlStatement(SqlMethod.UPDATE_BY_ID), param);
            }
            // 不知道以后会不会有人说更新失败了还要执行插入 😂😂😂
            if ((i % batchSize == 0) || i == size) {
                sqlSession.flushStatements();
            }
            i++;
        }
    });
    return true;
}
Also used : MasterDataEntity(com.jbm.framework.masterdata.usage.entity.MasterDataEntity) MapperMethod(org.apache.ibatis.binding.MapperMethod) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with TableInfo

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

the class EntityService method getIdVal.

/**
 * id是否只值,并且存在对应记录
 *
 * @param entity 查询实体
 * @return 结果
 */
@Transactional(rollbackFor = Exception.class)
public Object getIdVal(T entity) {
    if (null != entity) {
        TableInfo tableInfo = TableInfoHelper.getTableInfo(entity.getClass());
        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 ReflectionKit.getFieldValue(entity, tableInfo.getKeyProperty());
    }
    return null;
}
Also used : TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) Transactional(org.springframework.transaction.annotation.Transactional)

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