Search in sources :

Example 46 with TableInfo

use of com.baomidou.mybatisplus.core.metadata.TableInfo in project ballcat by ballcat-projects.

the class TableAliasHelper method tableAliasSelectSql.

/**
 * 带别名的查询字段sql
 * @param clazz 实体类class
 * @return sql片段
 */
public static String tableAliasSelectSql(Class<?> clazz) {
    String tableAliasSelectSql = TABLE_ALIAS_SELECT_COLUMNS_CACHE.get(clazz);
    if (tableAliasSelectSql == null) {
        String tableAlias = tableAlias(clazz);
        TableInfo tableInfo = TableInfoHelper.getTableInfo(clazz);
        String allSqlSelect = tableInfo.getAllSqlSelect();
        String[] split = allSqlSelect.split(COMMA);
        StringBuilder stringBuilder = new StringBuilder();
        for (String column : split) {
            stringBuilder.append(tableAlias).append(DOT).append(column).append(COMMA);
        }
        stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        tableAliasSelectSql = stringBuilder.toString();
        TABLE_ALIAS_SELECT_COLUMNS_CACHE.put(clazz, tableAliasSelectSql);
    }
    return tableAliasSelectSql;
}
Also used : TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo)

Example 47 with TableInfo

use of com.baomidou.mybatisplus.core.metadata.TableInfo in project ballcat by ballcat-projects.

the class InsertBatchSomeColumnByCollection method injectMappedStatement.

@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
    KeyGenerator keyGenerator = NoKeyGenerator.INSTANCE;
    SqlMethod sqlMethod = SqlMethod.INSERT_ONE;
    List<TableFieldInfo> fieldList = tableInfo.getFieldList();
    String insertSqlColumn = tableInfo.getKeyInsertSqlColumn(true, false) + this.filterTableFieldInfo(fieldList, predicate, TableFieldInfo::getInsertSqlColumn, EMPTY);
    String columnScript = LEFT_BRACKET + insertSqlColumn.substring(0, insertSqlColumn.length() - 1) + RIGHT_BRACKET;
    String insertSqlProperty = tableInfo.getKeyInsertSqlProperty(true, ENTITY_DOT, false) + this.filterTableFieldInfo(fieldList, predicate, i -> i.getInsertSqlProperty(ENTITY_DOT), EMPTY);
    insertSqlProperty = LEFT_BRACKET + insertSqlProperty.substring(0, insertSqlProperty.length() - 1) + RIGHT_BRACKET;
    // 从 list 改为 collection. 允许传入除 list外的参数类型
    String valuesScript = SqlScriptUtils.convertForeach(insertSqlProperty, "collection", null, ENTITY, COMMA);
    String keyProperty = null;
    String keyColumn = null;
    // 表包含主键处理逻辑,如果不包含主键当普通字段处理
    if (tableInfo.havePK()) {
        if (tableInfo.getIdType() == IdType.AUTO) {
            /* 自增主键 */
            keyGenerator = Jdbc3KeyGenerator.INSTANCE;
            keyProperty = tableInfo.getKeyProperty();
            keyColumn = tableInfo.getKeyColumn();
        } else {
            if (null != tableInfo.getKeySequence()) {
                keyGenerator = TableInfoHelper.genKeyGenerator(this.methodName, tableInfo, builderAssistant);
                keyProperty = tableInfo.getKeyProperty();
                keyColumn = tableInfo.getKeyColumn();
            }
        }
    }
    String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), columnScript, valuesScript);
    SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
    return this.addInsertMappedStatement(mapperClass, modelClass, this.methodName, sqlSource, keyGenerator, keyProperty, keyColumn);
}
Also used : SqlScriptUtils(com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils) SqlMethod(com.baomidou.mybatisplus.core.enums.SqlMethod) Setter(lombok.Setter) Accessors(lombok.experimental.Accessors) Predicate(java.util.function.Predicate) InsertBatchSomeColumn(com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn) NoKeyGenerator(org.apache.ibatis.executor.keygen.NoKeyGenerator) AbstractMethod(com.baomidou.mybatisplus.core.injector.AbstractMethod) Jdbc3KeyGenerator(org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator) KeyGenerator(org.apache.ibatis.executor.keygen.KeyGenerator) TableInfoHelper(com.baomidou.mybatisplus.core.metadata.TableInfoHelper) IdType(com.baomidou.mybatisplus.annotation.IdType) List(java.util.List) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) MappedStatement(org.apache.ibatis.mapping.MappedStatement) SqlSource(org.apache.ibatis.mapping.SqlSource) TableFieldInfo(com.baomidou.mybatisplus.core.metadata.TableFieldInfo) TableFieldInfo(com.baomidou.mybatisplus.core.metadata.TableFieldInfo) SqlSource(org.apache.ibatis.mapping.SqlSource) NoKeyGenerator(org.apache.ibatis.executor.keygen.NoKeyGenerator) Jdbc3KeyGenerator(org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator) KeyGenerator(org.apache.ibatis.executor.keygen.KeyGenerator) SqlMethod(com.baomidou.mybatisplus.core.enums.SqlMethod)

Example 48 with TableInfo

use of com.baomidou.mybatisplus.core.metadata.TableInfo in project JBM by numen06.

the class MasterDataCodeServiceImpl method saveOrUpdateBatchByCode.

@Transactional(rollbackFor = Exception.class)
private boolean saveOrUpdateBatchByCode(Collection<Entity> entityList, int batchSize) {
    if (CollectionUtils.isEmpty(entityList)) {
        throw new IllegalArgumentException("Error: entityList must not be empty");
    }
    Class<?> cls = null;
    TableInfo tableInfo = null;
    int i = 0;
    try (SqlSession batchSqlSession = sqlSessionBatch()) {
        for (Entity anEntityList : entityList) {
            if (i == 0) {
                cls = anEntityList.getClass();
                tableInfo = TableInfoHelper.getTableInfo(cls);
            }
            if (null != tableInfo && StringUtils.isNotEmpty(tableInfo.getKeyProperty())) {
                Object codeVal = ReflectionKit.getMethodValue(cls, anEntityList, IMasterDataCodeService.CODE_COLUMN);
                if (StringUtils.checkValNull(codeVal)) {
                    String sqlStatement = sqlStatement(SqlMethod.INSERT_ONE);
                    batchSqlSession.insert(sqlStatement, anEntityList);
                } else {
                    String sqlStatement = sqlStatement(MasterDataSqlInjector.UPDATE_BY_CODE);
                    MapperMethod.ParamMap<Entity> param = new MapperMethod.ParamMap<>();
                    param.put(Constants.ENTITY, anEntityList);
                    batchSqlSession.update(sqlStatement, param);
                // 不知道以后会不会有人说更新失败了还要执行插入 😂😂😂
                }
                if (i >= 1 && i % batchSize == 0) {
                    batchSqlSession.flushStatements();
                }
                i++;
            } else {
                throw ExceptionUtils.mpe("Error:  Can not execute. Could not find @TableId.");
            }
            batchSqlSession.flushStatements();
        }
    }
    return true;
}
Also used : MasterDataCodeEntity(com.jbm.framework.masterdata.usage.entity.MasterDataCodeEntity) MapperMethod(org.apache.ibatis.binding.MapperMethod) SqlSession(org.apache.ibatis.session.SqlSession) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) Transactional(org.springframework.transaction.annotation.Transactional)

Example 49 with TableInfo

use of com.baomidou.mybatisplus.core.metadata.TableInfo in project heifer by galaxy-sea.

the class ServiceImpl method saveOrUpdate.

@Override
public boolean saveOrUpdate(Collection<T> entityList, int batchSize) {
    TableInfo tableInfo = TableInfoHelper.getTableInfo(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!");
    return SqlHelper.saveOrUpdateBatch(this.entityClass, this.mapperClass, this.log, entityList, batchSize, (sqlSession, entity) -> {
        Object idVal = ReflectionKit.getFieldValue(entity, keyProperty);
        return StringUtils.checkValNull(idVal) || CollectionUtils.isEmpty(sqlSession.selectList(getSqlStatement(SqlMethod.SELECT_BY_ID), entity));
    }, (sqlSession, entity) -> {
        MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
        param.put(Constants.ENTITY, entity);
        sqlSession.update(getSqlStatement(SqlMethod.UPDATE_BY_ID), param);
    });
}
Also used : MapperMethod(org.apache.ibatis.binding.MapperMethod) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo)

Example 50 with TableInfo

use of com.baomidou.mybatisplus.core.metadata.TableInfo in project heifer by galaxy-sea.

the class ServiceImpl method saveOrUpdate.

@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 = ReflectionKit.getFieldValue(entity, tableInfo.getKeyProperty());
        // noinspection unchecked
        return StringUtils.checkValNull(idVal) || Objects.isNull(get((ID) idVal)) ? save(entity) : update(entity);
    }
    return false;
}
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