Search in sources :

Example 31 with TableInfo

use of com.baomidou.mybatisplus.core.metadata.TableInfo in project albedo by somowhere.

the class AbstractCacheServiceImpl method saveOrUpdateBatch.

@Transactional(rollbackFor = Exception.class)
@Override
public boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize) {
    TableInfo tableInfo = TableInfoHelper.getTableInfo(getEntityClass());
    ArgumentAssert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!");
    String keyProperty = tableInfo.getKeyProperty();
    ArgumentAssert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!");
    BiPredicate<SqlSession, T> predicate = (sqlSession, entity) -> {
        Object idVal = ReflectionKit.getFieldValue(entity, keyProperty);
        return StringUtils.checkValNull(idVal) || CollectionUtils.isEmpty(sqlSession.selectList(getSqlStatement(SqlMethod.SELECT_BY_ID), entity));
    };
    BiConsumer<SqlSession, T> consumer = (sqlSession, entity) -> {
        MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
        param.put(Constants.ENTITY, entity);
        sqlSession.update(getSqlStatement(SqlMethod.UPDATE_BY_ID), param);
        // 清理缓存
        delCache(entity);
    };
    String sqlStatement = SqlHelper.getSqlStatement(this.mapperClass, SqlMethod.INSERT_ONE);
    return SqlHelper.executeBatch(getEntityClass(), log, entityList, batchSize, (sqlSession, entity) -> {
        if (predicate.test(sqlSession, entity)) {
            sqlSession.insert(sqlStatement, entity);
        // 设置缓存
        // setCache(entity);
        } else {
            consumer.accept(sqlSession, entity);
        }
    });
}
Also used : java.util(java.util) ArgumentAssert(com.albedo.java.common.core.util.ArgumentAssert) ReflectionKit(com.baomidou.mybatisplus.core.toolkit.ReflectionKit) Autowired(org.springframework.beans.factory.annotation.Autowired) IdDo(com.albedo.java.common.core.basic.domain.IdDo) CacheService(com.albedo.java.plugins.database.mybatis.service.CacheService) Constants(com.baomidou.mybatisplus.core.toolkit.Constants) Function(java.util.function.Function) StringUtils(com.baomidou.mybatisplus.core.toolkit.StringUtils) BiPredicate(java.util.function.BiPredicate) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) Lists(com.google.common.collect.Lists) CollectionUtils(com.baomidou.mybatisplus.core.toolkit.CollectionUtils) BiConsumer(java.util.function.BiConsumer) BaseRepository(com.albedo.java.plugins.database.mybatis.repository.BaseRepository) SqlSession(org.apache.ibatis.session.SqlSession) CacheKey(com.albedo.java.common.core.cache.model.CacheKey) CacheOps(com.albedo.java.plugins.cache.repository.CacheOps) SqlMethod(com.baomidou.mybatisplus.core.enums.SqlMethod) SqlHelper(com.baomidou.mybatisplus.extension.toolkit.SqlHelper) Field(java.lang.reflect.Field) CacheKeyBuilder(com.albedo.java.common.core.cache.model.CacheKeyBuilder) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Serializable(java.io.Serializable) TableInfoHelper(com.baomidou.mybatisplus.core.metadata.TableInfoHelper) CollUtil(cn.hutool.core.collection.CollUtil) ReflectUtil(cn.hutool.core.util.ReflectUtil) Convert(cn.hutool.core.convert.Convert) NonNull(org.springframework.lang.NonNull) MapperMethod(org.apache.ibatis.binding.MapperMethod) Transactional(org.springframework.transaction.annotation.Transactional) MapperMethod(org.apache.ibatis.binding.MapperMethod) SqlSession(org.apache.ibatis.session.SqlSession) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) Transactional(org.springframework.transaction.annotation.Transactional)

Example 32 with TableInfo

use of com.baomidou.mybatisplus.core.metadata.TableInfo in project albedo by somowhere.

the class AbstractCacheServiceImpl method getId.

protected Object getId(T model) {
    if (model instanceof IdDo) {
        return ((IdDo) model).getId();
    } else {
        // 实体没有继承 IdEntity
        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) IdDo(com.albedo.java.common.core.basic.domain.IdDo) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo)

Example 33 with TableInfo

use of com.baomidou.mybatisplus.core.metadata.TableInfo in project citrus by Yiuman.

the class CrudMapper method saveEntity.

/**
 * 保存实体
 *
 * @param entity 实体对象
 * @return 保存成功返回true,否则false
 */
@Transactional(rollbackFor = Exception.class)
default boolean saveEntity(T entity) {
    if (null != entity) {
        Class<?> cls = entity.getClass();
        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();
        if (StringUtils.isBlank(keyProperty)) {
            return SqlHelper.retBool(insert(entity));
        }
        Object idVal = ReflectionKit.getFieldValue(entity, tableInfo.getKeyProperty());
        return StringUtils.checkValNull(idVal) || Objects.isNull(selectById((Serializable) idVal)) ? SqlHelper.retBool(insert(entity)) : SqlHelper.retBool(updateById(entity));
    }
    return false;
}
Also used : Serializable(java.io.Serializable) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) Transactional(org.springframework.transaction.annotation.Transactional)

Example 34 with TableInfo

use of com.baomidou.mybatisplus.core.metadata.TableInfo in project citrus by Yiuman.

the class CrudMapper method saveBatch.

/**
 * 批量保存实体
 *
 * @param entities 实体集合
 * @return 保存成功返回true,否则false
 */
@Transactional(rollbackFor = Exception.class)
default boolean saveBatch(Collection<T> entities) {
    if (Objects.isNull(entities) || entities.isEmpty()) {
        return true;
    }
    T entity = entities.stream().findAny().get();
    Class<?> realEntityClass = ClassUtils.getRealClass(entity.getClass());
    TableInfo tableInfo = TableInfoHelper.getTableInfo(realEntityClass);
    Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!");
    // 根据是否存在主键进行分组,有主键为true,没主键为false
    Map<Boolean, List<T>> collect = entities.stream().collect(Collectors.groupingBy((item) -> {
        Object keyFieldValue = ReflectionKit.getFieldValue(item, tableInfo.getKeyProperty());
        // 已经存在与数据库
        return StringUtils.checkValNull(keyFieldValue) || Objects.isNull(selectById((Serializable) keyFieldValue));
    }));
    return insertBatch(collect.get(true)) && updateBatch(collect.get(false));
}
Also used : SqlMethod(com.baomidou.mybatisplus.core.enums.SqlMethod) ReflectionKit(com.baomidou.mybatisplus.core.toolkit.ReflectionKit) LogFactory(org.apache.ibatis.logging.LogFactory) Collection(java.util.Collection) SqlHelper(com.baomidou.mybatisplus.extension.toolkit.SqlHelper) ClassUtils(com.github.yiuman.citrus.support.utils.ClassUtils) Constants(com.baomidou.mybatisplus.core.toolkit.Constants) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) TableInfoHelper(com.baomidou.mybatisplus.core.metadata.TableInfoHelper) Objects(java.util.Objects) StringUtils(com.baomidou.mybatisplus.core.toolkit.StringUtils) List(java.util.List) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) BaseMapper(com.baomidou.mybatisplus.core.mapper.BaseMapper) Assert(com.baomidou.mybatisplus.core.toolkit.Assert) CollectionUtils(org.springframework.util.CollectionUtils) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) MapperMethod(org.apache.ibatis.binding.MapperMethod) SqlSession(org.apache.ibatis.session.SqlSession) Transactional(org.springframework.transaction.annotation.Transactional) Serializable(java.io.Serializable) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) List(java.util.List) Transactional(org.springframework.transaction.annotation.Transactional)

Example 35 with TableInfo

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

the class EntityService method removeById.

@Override
public boolean removeById(Serializable id, boolean useFill) {
    TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
    if (useFill && tableInfo.isWithLogicDelete()) {
        if (!entityClass.isAssignableFrom(id.getClass())) {
            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)

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