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