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);
}
});
}
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);
}
}
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;
}
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));
}
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));
}
Aggregations