com.baomidou.mybatisplus.core.metadata.TableInfo Java Examples

The following examples show how to use com.baomidou.mybatisplus.core.metadata.TableInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SuperServiceImpl.java    From microservices-platform with Apache License 2.0 6 votes vote down vote up
@Override
public boolean saveOrUpdateIdempotency(T entity, DistributedLock lock, String lockKey, Wrapper<T> countWrapper, String msg) throws Exception {
    if (null != entity) {
        Class<?> cls = entity.getClass();
        TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
        if (null != tableInfo && StrUtil.isNotEmpty(tableInfo.getKeyProperty())) {
            Object idVal = ReflectionKit.getMethodValue(cls, entity, tableInfo.getKeyProperty());
            if (StringUtils.checkValNull(idVal) || Objects.isNull(getById((Serializable) idVal))) {
                if (StrUtil.isEmpty(msg)) {
                    msg = "已存在";
                }
                return this.saveIdempotency(entity, lock, lockKey, countWrapper, msg);
            } else {
                return updateById(entity);
            }
        } else {
            throw ExceptionUtils.mpe("Error:  Can not execute. Could not find @TableId.");
        }
    }
    return false;
}
 
Example #2
Source File: LambdaColumn.java    From spring-boot-plus with Apache License 2.0 6 votes vote down vote up
/**
 * 从mybatisplus的TableInfo类中获取列名map信息
 *
 * @param cls
 * @return
 */
private Map<String, String> getPropertyColumnMap(Class<?> cls) {
    TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
    List<TableFieldInfo> tableFieldInfos = tableInfo.getFieldList();
    if (CollectionUtils.isEmpty(tableFieldInfos)) {
        return null;
    }

    Map<String, String> map = new ConcurrentHashMap<>();
    String keyProperty = tableInfo.getKeyProperty();
    String keyColumn = tableInfo.getKeyColumn();
    map.put(keyProperty, keyColumn);

    for (TableFieldInfo tableFieldInfo : tableFieldInfos) {
        String column = tableFieldInfo.getColumn();
        String property = tableFieldInfo.getProperty();
        map.put(property, column);
    }
    return map;
}
 
Example #3
Source File: SqlInjectorUtil.java    From albedo with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static String parseSql(MapperBuilderAssistant builderAssistant,
							  SqlCustomMethod sqlMethod, Class<?> modelClass, TableInfo tableInfo, String sqlWhereEntityWrapper) {
	String tableNameAlias = StringUtil.lowerCase(modelClass.getSimpleName()), tempNameAlias;
	TableInfo tableAlias;
	PropertyDescriptor[] ps = BeanUtil.getPropertyDescriptors(modelClass);
	StringBuffer sbSelectCoumns = new StringBuffer(SqlInjectorUtil.sqlSelectColumns(tableInfo, false, tableNameAlias, null)),
		sbLeftJoin = new StringBuffer(tableInfo.getTableName()).append(" `").append(tableNameAlias).append("`");
	for (PropertyDescriptor p : ps) {

		ManyToOne annotation = ClassUtil.findAnnotation(modelClass, p.getName(), ManyToOne.class);
		if (annotation != null) {
			tableAlias = TableInfoHelper.initTableInfo(builderAssistant, p.getPropertyType());
			sbSelectCoumns.append(",")
				.append(SqlInjectorUtil.sqlSelectColumns(tableAlias, false, p.getName(), p.getName()));
			sbLeftJoin.append(" LEFT JOIN ").append(tableAlias.getTableName()).append(" `").append(p.getName())
				.append("` ON `").append(tableNameAlias).append("`.").append(annotation.name())
				.append(" = `").append(p.getName()).append("`.").append(TreeEntity.F_SQL_ID);
		}
	}

	String sql = String.format(sqlMethod.getSql(),
		sbSelectCoumns.toString(),
		sbLeftJoin.toString(),
		sqlWhereEntityWrapper);
	return sql;
}
 
Example #4
Source File: AbstractLogicCustomMethod.java    From albedo with GNU Lesser General Public License v3.0 6 votes vote down vote up
public String getAllSqlWhere(TableInfo table, boolean ignoreLogicDelFiled, boolean withId, String prefix, String columnPrefix) {
	String newPrefix = prefix == null ? "" : prefix;
	String filedSqlScript = table.getFieldList().stream().filter((i) -> {
		if (!ignoreLogicDelFiled) {
			return true;
		} else {
			return !table.isLogicDelete() || !i.isLogicDelete();
		}
	}).map((i) -> i.getSqlWhere(newPrefix)).collect(Collectors.joining("\n"));
	if (withId && !StringUtils.isEmpty(table.getKeyProperty())) {
		String newKeyProperty = newPrefix + table.getKeyProperty();
		String keySqlScript = columnPrefix + table.getKeyColumn() + "=" + SqlScriptUtils.safeParam(newKeyProperty);
		return SqlScriptUtils.convertIf(keySqlScript, String.format("%s != null", newKeyProperty), false) + "\n" + filedSqlScript;
	} else {
		return filedSqlScript;
	}
}
 
Example #5
Source File: CustomMybatisPlusParameterHandler.java    From sqlhelper with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static Object populateKeys(MetaObjectHandler metaObjectHandler, TableInfo tableInfo, MappedStatement ms, Object parameterObject, boolean isInsert) {
    if (null == tableInfo) {
        return parameterObject;
    } else {
        MetaObject metaObject = ms.getConfiguration().newMetaObject(parameterObject);
        if (isInsert && !StringUtils.isEmpty(tableInfo.getKeyProperty()) && null != tableInfo.getIdType() && tableInfo.getIdType().getKey() >= 3) {
            Object idValue = metaObject.getValue(tableInfo.getKeyProperty());
            if (StringUtils.checkValNull(idValue)) {
                if (tableInfo.getIdType() == IdType.ID_WORKER) {
                    metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.getId());
                } else if (tableInfo.getIdType() == IdType.ID_WORKER_STR) {
                    metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.getIdStr());
                } else if (tableInfo.getIdType() == IdType.UUID) {
                    metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.get32UUID());
                }
            }
        }

        if (metaObjectHandler != null) {
            if (isInsert && metaObjectHandler.openInsertFill()) {
                metaObjectHandler.insertFill(metaObject);
            } else if (!isInsert) {
                metaObjectHandler.updateFill(metaObject);
            }
        }

        return metaObject.getOriginalObject();
    }
}
 
Example #6
Source File: PropertyColumnUtil.java    From spring-boot-plus with Apache License 2.0 5 votes vote down vote up
/**
 * 根据实体class,从mybatisplus中获取对应Table的属性列名Map
 *
 * @param clazz
 * @return
 */
private static Map<String, String> getTableFieldMap(Class<?> clazz) {
    TableInfo tableInfo = TableInfoHelper.getTableInfo(clazz);
    if (tableInfo == null) {
        return null;
    }
    List<TableFieldInfo> tableFieldInfos = tableInfo.getFieldList();
    if (CollectionUtils.isEmpty(tableFieldInfos)) {
        return null;
    }
    Map<String, String> cacheMap = tableFieldInfos.stream().collect(Collectors.toMap(TableFieldInfo::getProperty, TableFieldInfo::getColumn));
    return cacheMap;
}
 
Example #7
Source File: FindRelationPageLogic.java    From albedo with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
	String tableNameAlias = StringUtil.lowerCase(modelClass.getSimpleName());
	String sql = SqlInjectorUtil.parseSql(builderAssistant, SqlCustomMethod.FIND_RELATION_PAGE, modelClass, tableInfo,
		sqlWhereEntityWrapper(tableInfo, tableNameAlias));
	SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, modelClass);
	return this.addSelectMappedStatementForTable(mapperClass, SqlCustomMethod.FIND_RELATION_PAGE.getMethod(), sqlSource, tableInfo);
}
 
Example #8
Source File: FindRelationListLogic.java    From albedo with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
	String tableNameAlias = StringUtil.lowerCase(modelClass.getSimpleName());
	String sql = SqlInjectorUtil.parseSql(builderAssistant, SqlCustomMethod.FIND_RELATION_LIST, modelClass, tableInfo,
		sqlWhereEntityWrapper(tableInfo, tableNameAlias));
	SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, modelClass);
	return this.addSelectMappedStatementForTable(mapperClass, SqlCustomMethod.FIND_RELATION_LIST.getMethod(), sqlSource, tableInfo);
}
 
Example #9
Source File: SqlInjectorUtil.java    From albedo with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static String sqlSelectColumns(TableInfo table, boolean entityWrapper, String columnPrefix, String selectProfix) {
	StringBuilder columns = new StringBuilder();
	if (null != table.getResultMap()) {
		if (entityWrapper) {
			columns.append("<choose><when test=\"ew != null and ew.sqlSelect != null\">${ew.sqlSelect}</when><otherwise>");
		}

		columns.append("*");
		if (entityWrapper) {
			columns.append("</otherwise></choose>");
		}
	} else {
		if (entityWrapper) {
			columns.append("<choose><when test=\"ew != null and ew.sqlSelect != null\">${ew.sqlSelect}</when><otherwise>");
		}

		List<TableFieldInfo> fieldList = table.getFieldList();
		int size = 0;
		if (null != fieldList) {
			size = fieldList.size();
		}

		if (StringUtils.isNotEmpty(table.getKeyProperty())) {
			if (StringUtil.isNotEmpty(columnPrefix)) {
				columns.append('`').append(columnPrefix).append("`.");
			}
			String keyProperty = table.getKeyProperty();
			if (StringUtil.isNotEmpty(selectProfix)) {
				keyProperty = selectProfix + StringUtil.DOT + keyProperty;
			}
			columns.append(table.getKeyColumn()).append(" AS ").append(sqlWordConvert(keyProperty));

			if (size >= 1) {
				columns.append(",");
			}
		}

		if (size >= 1) {
			int i = 0;

			for (Iterator iterator = fieldList.iterator(); iterator.hasNext(); ++i) {
				TableFieldInfo fieldInfo = (TableFieldInfo) iterator.next();
				String property = fieldInfo.getProperty();
				if (StringUtil.isNotEmpty(selectProfix)) {
					property = selectProfix + StringUtil.DOT + property;
				}
				String wordConvert = sqlWordConvert(property);
				if (StringUtil.isNotEmpty(columnPrefix)) {
					columns.append('`').append(columnPrefix).append("`.");
				}
				columns.append(fieldInfo.getColumn());
				columns.append(" AS ").append(wordConvert);

				if (i + 1 < size) {
					columns.append(",");
				}
			}
		}

		if (entityWrapper) {
			columns.append("</otherwise></choose>");
		}
	}

	return columns.toString();
}
 
Example #10
Source File: AbstractLogicCustomMethod.java    From albedo with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected String getLogicDeleteSql(TableInfo table, boolean startWithAnd, boolean deleteValue, String columnPrefix) {
	return columnPrefix + StringUtil.DOT + table.getLogicDeleteSql(startWithAnd, deleteValue);
}