use of com.baomidou.mybatisplus.core.metadata.TableFieldInfo in project diboot by dibo-software.
the class ServiceAdaptor method optimizeSelect.
/**
* 基于VO提取最小集select字段
* @param queryWrapper
* @param voClass
*/
public static <T> Wrapper<T> optimizeSelect(Wrapper<T> queryWrapper, Class<T> entityClass, Class<?> voClass) {
if (!(queryWrapper instanceof QueryWrapper) || queryWrapper.getSqlSelect() != null) {
return queryWrapper;
}
List<TableFieldInfo> allColumns = TableInfoHelper.getTableInfo(entityClass).getFieldList();
if (V.isEmpty(allColumns)) {
return queryWrapper;
}
List<String> columns = new ArrayList<>();
String pk = ContextHelper.getIdColumnName(entityClass);
if (V.notEmpty(pk)) {
columns.add(pk);
}
Map<String, Field> fieldsMap = BindingCacheManager.getFieldsMap(voClass);
for (TableFieldInfo col : allColumns) {
if (fieldsMap.containsKey(col.getField().getName()) && V.notEmpty(col.getColumn()) && !col.isLogicDelete()) {
columns.add(col.getSqlSelect());
}
}
// select全部列,不特殊处理
if (allColumns.size() <= columns.size()) {
return queryWrapper;
}
return ((QueryWrapper) queryWrapper).select(S.toStringArray(columns));
}
use of com.baomidou.mybatisplus.core.metadata.TableFieldInfo in project solon by noear.
the class OptimisticLockerInnerInterceptor method setVersionByWrapper.
private void setVersionByWrapper(Map<String, Object> map, String msId) {
Object ew = map.get(Constants.WRAPPER);
if (null != ew && ew instanceof AbstractWrapper && ew instanceof Update) {
Class<?> entityClass = ENTITY_CLASS_CACHE.get(msId);
if (null == entityClass) {
try {
final String className = msId.substring(0, msId.lastIndexOf('.'));
entityClass = GenericTypeUtil.getSuperClassGenericType(Class.forName(className), Mapper.class, 0);
ENTITY_CLASS_CACHE.put(msId, entityClass);
} catch (ClassNotFoundException e) {
throw ExceptionUtils.mpe(e);
}
}
final TableFieldInfo versionField = getVersionFieldInfo(entityClass);
if (null == versionField) {
return;
}
final String versionColumn = versionField.getColumn();
final FieldEqFinder fieldEqFinder = new FieldEqFinder(versionColumn, (Wrapper<?>) ew);
if (!fieldEqFinder.isPresent()) {
return;
}
final Map<String, Object> paramNameValuePairs = ((AbstractWrapper<?, ?, ?>) ew).getParamNameValuePairs();
final Object originalVersionValue = paramNameValuePairs.get(fieldEqFinder.valueKey);
if (originalVersionValue == null) {
return;
}
final Object updatedVersionVal = getUpdatedVersionVal(originalVersionValue.getClass(), originalVersionValue);
if (originalVersionValue == updatedVersionVal) {
return;
}
// 拼接新的version值
paramNameValuePairs.put(UPDATED_VERSION_VAL_KEY, updatedVersionVal);
((Update<?, ?>) ew).setSql(String.format("%s = #{%s.%s}", versionColumn, "ew.paramNameValuePairs", UPDATED_VERSION_VAL_KEY));
}
}
use of com.baomidou.mybatisplus.core.metadata.TableFieldInfo in project solon by noear.
the class OptimisticLockerInnerInterceptor method doOptimisticLocker.
protected void doOptimisticLocker(Map<String, Object> map, String msId) {
// updateById(et), update(et, wrapper);
Object et = map.getOrDefault(Constants.ENTITY, null);
if (Objects.nonNull(et)) {
// version field
TableFieldInfo fieldInfo = this.getVersionFieldInfo(et.getClass());
if (null == fieldInfo) {
return;
}
try {
Field versionField = fieldInfo.getField();
// 旧的 version 值
Object originalVersionVal = versionField.get(et);
if (originalVersionVal == null) {
if (null != exception) {
/**
* 自定义异常处理
*/
throw exception;
}
return;
}
String versionColumn = fieldInfo.getColumn();
// 新的 version 值
Object updatedVersionVal = this.getUpdatedVersionVal(fieldInfo.getPropertyType(), originalVersionVal);
String methodName = msId.substring(msId.lastIndexOf(StringPool.DOT) + 1);
if ("update".equals(methodName)) {
AbstractWrapper<?, ?, ?> aw = (AbstractWrapper<?, ?, ?>) map.getOrDefault(Constants.WRAPPER, null);
if (aw == null) {
UpdateWrapper<?> uw = new UpdateWrapper<>();
uw.eq(versionColumn, originalVersionVal);
map.put(Constants.WRAPPER, uw);
} else {
aw.apply(versionColumn + " = {0}", originalVersionVal);
}
} else {
map.put(Constants.MP_OPTLOCK_VERSION_ORIGINAL, originalVersionVal);
}
versionField.set(et, updatedVersionVal);
} catch (IllegalAccessException e) {
throw ExceptionUtils.mpe(e);
}
} else // update(LambdaUpdateWrapper) or update(UpdateWrapper)
if (wrapperMode && map.entrySet().stream().anyMatch(t -> Objects.equals(t.getKey(), Constants.WRAPPER))) {
setVersionByWrapper(map, msId);
}
}
use of com.baomidou.mybatisplus.core.metadata.TableFieldInfo 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);
}
use of com.baomidou.mybatisplus.core.metadata.TableFieldInfo in project mybaits-plus-join by Createsequence.
the class AbstractDynamicResultWrapper method initLogicDelete.
/**
* 若主表不为逻辑表,则获取其逻辑删除字段并添加到条件中
*
* @author huangchengxing
* @date 2022/2/10 9:21
*/
protected void initLogicDelete() {
if (isLogic || !tableInfo.isWithLogicDelete()) {
return;
}
TableFieldInfo logicDeleteFieldInfo = tableInfo.getLogicDeleteFieldInfo();
String notDeleteValue = tableInfo.getLogicDeleteFieldInfo().getLogicDeleteValue();
whereIfNotNull(new TableColumn(this, logicDeleteFieldInfo.getColumn()), Condition.EQ, notDeleteValue);
}
Aggregations