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