use of com.baomidou.mybatisplus.core.metadata.TableInfo in project albedo by somowhere.
the class EntityMetaObjectHandler method fillId.
private void fillId(MetaObject metaObject) {
if (uidGenerator == null) {
// 这里使用SpringUtils的方式"异步"获取对象,防止启动时,报循环注入的错
uidGenerator = SpringContextHolder.getBean(UidGenerator.class);
}
Long id = uidGenerator.getUid();
// 1. 继承了BaseEntity 若 ID 中有值,就不设置
if (metaObject.getOriginalObject() instanceof BaseDo) {
Object oldId = ((BaseDo) metaObject.getOriginalObject()).pkVal();
if (oldId != null) {
return;
}
Object idVal = StrPool.STRING_TYPE_NAME.equals(metaObject.getGetterType(BaseDo.F_ID).getName()) ? String.valueOf(id) : id;
this.setFieldValByName(BaseDo.F_ID, idVal, metaObject);
return;
}
// 2. 没有继承BaseEntity, 但主键的字段名为: id
if (metaObject.hasGetter(BaseDo.F_ID)) {
Object oldId = metaObject.getValue(BaseDo.F_ID);
if (oldId != null) {
return;
}
Object idVal = StrPool.STRING_TYPE_NAME.equals(metaObject.getGetterType(BaseDo.F_ID).getName()) ? String.valueOf(id) : id;
this.setFieldValByName(BaseDo.F_ID, idVal, metaObject);
return;
}
// 3. 实体没有继承 Entity 和 BaseEntity,且 主键名为其他字段
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 albedo by somowhere.
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[] { BaseDo.F_SQL_CREATED_DATE, BaseDo.F_SQL_CREATED_BY }, field.getColumn())));
return methodList;
}
use of com.baomidou.mybatisplus.core.metadata.TableInfo in project chao-cloud by chaojunzi.
the class FunctionUtil method getColumn.
static <T, R> String getColumn(SFunction<T, R> func) {
SerializedLambda lambda = LambdaUtils.resolve(func);
String fieldName = StrUtil.getGeneralField(lambda.getImplMethodName());
Class<?> beanClass = lambda.getImplClass();
TableInfo info = buildTableInfo(beanClass);
Assert.notNull(info, "无效的数据实体 beanClass={}", beanClass.getName());
Map<String, String> propColumnMap = info.getFieldList().stream().collect(Collectors.toMap(TableFieldInfo::getProperty, TableFieldInfo::getColumn));
String column = propColumnMap.get(fieldName);
Assert.notBlank(column, "无效的列名映射 fieldName={}", fieldName);
return column;
}
use of com.baomidou.mybatisplus.core.metadata.TableInfo in project chao-cloud by chaojunzi.
the class MybatisUtil method buildApplySql.
/**
* 构造applysql
*
* @param <T> 实体类类型
* @param wrapper 构造条件
* @param beanClass 实体类类型
* @return {@link ApplySql}
*/
static <T> ApplySql buildApplySql(LambdaQueryWrapper<T> wrapper, Class<T> beanClass) {
// sql片段
ApplySqlSegment normalSegment = null;
List<String> groupByList = Collections.emptyList();
ApplySqlSegment havingSegment = null;
List<String> orderByList = Collections.emptyList();
// 判断bean是否加载
TableInfo info = FunctionUtil.buildTableInfo(beanClass);
Assert.notNull(info, "无效的数据实体 beanClass={}", beanClass.getName());
// 获取groupBy和orderBy
MergeSegments segments = wrapper.getExpression();
if (segments == null) {
return null;
}
// 1.where之后的条件
normalSegment = buildApplySqlSegment(segments.getNormal(), wrapper.getParamNameValuePairs());
// 2.group by
groupByList = CollUtil.map(segments.getGroupBy(), ISqlSegment::getSqlSegment, true);
// 3.having 之后的条件
havingSegment = buildApplySqlSegment(segments.getHaving(), wrapper.getParamNameValuePairs());
// 4.order by
orderByList = CollUtil.map(segments.getOrderBy(), ISqlSegment::getSqlSegment, true);
return //
ApplySql.of().setNormalSegment(//
normalSegment).setGroupByList(//
groupByList).setHavingSegment(//
havingSegment).setOrderByList(orderByList);
}
use of com.baomidou.mybatisplus.core.metadata.TableInfo in project chao-cloud by chaojunzi.
the class MybatisUtil method buildCompleteSql.
/**
* 构造完全的sql 如: select * from table where a=1
*
* @param <T> 实体类型
* @param wrapper 条件
* @param beanClass bean类型
* @return sql语句
*/
static <T> String buildCompleteSql(LambdaQueryWrapper<T> wrapper, Class<T> beanClass) {
String where = buildSql(wrapper, beanClass);
// where 条件
where = StrUtil.isBlank(where) ? StrUtil.EMPTY : "WHERE " + where;
// 表名
TableInfo info = FunctionUtil.buildTableInfo(beanClass);
// 字段
String sqlSelect = StrUtil.isBlank(wrapper.getSqlSelect()) ? info.getAllSqlSelect() : wrapper.getSqlSelect();
return StrUtil.format(SELECT_SQL_TEMPLATE, sqlSelect, info.getTableName(), where);
}
Aggregations