Search in sources :

Example 16 with TableInfo

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);
}
Also used : BaseDo(com.albedo.java.common.core.basic.domain.BaseDo) Field(java.lang.reflect.Field) UidGenerator(com.baidu.fsg.uid.UidGenerator) MetaObject(org.apache.ibatis.reflection.MetaObject) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo)

Example 17 with TableInfo

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;
}
Also used : UpdateAllById(com.albedo.java.plugins.database.injector.method.UpdateAllById) List(java.util.List) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) InsertBatchSomeColumn(com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn) ArrayUtil(cn.hutool.core.util.ArrayUtil) FieldFill(com.baomidou.mybatisplus.annotation.FieldFill) AbstractMethod(com.baomidou.mybatisplus.core.injector.AbstractMethod) BaseDo(com.albedo.java.common.core.basic.domain.BaseDo) DefaultSqlInjector(com.baomidou.mybatisplus.core.injector.DefaultSqlInjector) UpdateAllById(com.albedo.java.plugins.database.injector.method.UpdateAllById) AbstractMethod(com.baomidou.mybatisplus.core.injector.AbstractMethod) InsertBatchSomeColumn(com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn)

Example 18 with TableInfo

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;
}
Also used : TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) SerializedLambda(com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda)

Example 19 with TableInfo

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);
}
Also used : MergeSegments(com.baomidou.mybatisplus.core.conditions.segments.MergeSegments) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) ApplySqlSegment(com.chao.cloud.common.extra.mybatis.common.ApplySql.ApplySqlSegment)

Example 20 with TableInfo

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