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