Search in sources :

Example 1 with TableInfo

use of com.baomidou.mybatisplus.core.metadata.TableInfo in project solon by noear.

the class SqlHelper method getMapper.

/**
 * 通过entityClass获取Mapper,记得要释放连接
 * 例: {@code
 * SqlSession sqlSession = SqlHelper.sqlSession(entityClass);
 * try {
 *     BaseMapper<User> userMapper = getMapper(User.class, sqlSession);
 * } finally {
 *     sqlSession.close();
 * }
 * }
 *
 * @param entityClass 实体
 * @param <T>         实体类型
 * @return Mapper
 */
@SuppressWarnings("unchecked")
public static <T> BaseMapper<T> getMapper(Class<T> entityClass, SqlSession sqlSession) {
    Optional.ofNullable(entityClass).orElseThrow(() -> ExceptionUtils.mpe("entityClass can't be null!"));
    TableInfo tableInfo = Optional.ofNullable(TableInfoHelper.getTableInfo(entityClass)).orElseThrow(() -> ExceptionUtils.mpe("Can not find TableInfo from Class: \"%s\".", entityClass.getName()));
    try {
        Configuration configuration = tableInfo.getConfiguration();
        return (BaseMapper<T>) configuration.getMapper(Class.forName(tableInfo.getCurrentNamespace()), sqlSession);
    } catch (ClassNotFoundException e) {
        throw ExceptionUtils.mpe(e);
    }
}
Also used : Configuration(org.apache.ibatis.session.Configuration) BaseMapper(com.baomidou.mybatisplus.core.mapper.BaseMapper) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo)

Example 2 with TableInfo

use of com.baomidou.mybatisplus.core.metadata.TableInfo in project solon by noear.

the class SqlHelper method getMapper.

/**
 * 通过entityClass获取Mapper
 *
 * @param entityClass 实体
 * @param <T>         实体类型
 * @return Mapper
 * @deprecated 使用后未释放连接 {@link com.baomidou.mybatisplus.solon.toolkit.SqlHelper#getMapper(Class, SqlSession)}
 */
@SuppressWarnings("unchecked")
@Deprecated
public static <T> BaseMapper<T> getMapper(Class<T> entityClass) {
    Optional.ofNullable(entityClass).orElseThrow(() -> ExceptionUtils.mpe("entityClass can't be null!"));
    TableInfo tableInfo = Optional.ofNullable(TableInfoHelper.getTableInfo(entityClass)).orElseThrow(() -> ExceptionUtils.mpe("Can not find TableInfo from Class: \"%s\".", entityClass.getName()));
    String namespace = tableInfo.getCurrentNamespace();
    Configuration configuration = tableInfo.getConfiguration();
    SqlSession sqlSession = sqlSession(entityClass);
    BaseMapper<T> mapper;
    try {
        mapper = (BaseMapper<T>) configuration.getMapper(Class.forName(namespace), sqlSession);
    } catch (ClassNotFoundException e) {
        throw ExceptionUtils.mpe(e);
    }
    return mapper;
}
Also used : Configuration(org.apache.ibatis.session.Configuration) SqlSession(org.apache.ibatis.session.SqlSession) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo)

Example 3 with TableInfo

use of com.baomidou.mybatisplus.core.metadata.TableInfo in project solon by noear.

the class ServiceImpl method removeById.

public boolean removeById(Serializable id, boolean useFill) {
    TableInfo tableInfo = TableInfoHelper.getTableInfo(this.entityClass);
    if (useFill && tableInfo.isWithLogicDelete() && !this.entityClass.isAssignableFrom(id.getClass())) {
        T instance = tableInfo.newInstance();
        tableInfo.setPropertyValue(instance, tableInfo.getKeyProperty(), new Object[] { id });
        return this.removeById(instance);
    } else {
        return SqlHelper.retBool(this.getBaseMapper().deleteById(id));
    }
}
Also used : TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo)

Example 4 with TableInfo

use of com.baomidou.mybatisplus.core.metadata.TableInfo in project solon by noear.

the class ServiceImpl method removeBatchByIds.

@Tran
public boolean removeBatchByIds(Collection<T> list, int batchSize, boolean useFill) {
    String sqlStatement = this.getSqlStatement(SqlMethod.DELETE_BY_ID);
    TableInfo tableInfo = TableInfoHelper.getTableInfo(this.entityClass);
    return this.executeBatch(list, batchSize, (sqlSession, e) -> {
        if (useFill && tableInfo.isWithLogicDelete()) {
            if (this.entityClass.isAssignableFrom(e.getClass())) {
                sqlSession.update(sqlStatement, e);
            } else {
                T instance = tableInfo.newInstance();
                tableInfo.setPropertyValue(instance, tableInfo.getKeyProperty(), new Object[] { e });
                sqlSession.update(sqlStatement, instance);
            }
        } else {
            sqlSession.update(sqlStatement, e);
        }
    });
}
Also used : TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) Tran(org.noear.solon.data.annotation.Tran)

Example 5 with TableInfo

use of com.baomidou.mybatisplus.core.metadata.TableInfo in project solon by noear.

the class InsertBatchSomeColumn method injectMappedStatement.

@SuppressWarnings("Duplicates")
@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;
    String valuesScript = SqlScriptUtils.convertForeach(insertSqlProperty, "list", 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, getMethod(sqlMethod), 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) 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)

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