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