|
@@ -1,79 +0,0 @@
|
|
|
-package com.sd.framework.util.sql;
|
|
|
-
|
|
|
-import cn.hutool.core.util.StrUtil;
|
|
|
-import com.baomidou.mybatisplus.annotation.TableName;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.support.LambdaMeta;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
|
|
-import org.apache.ibatis.reflection.property.PropertyNamer;
|
|
|
-
|
|
|
-import java.lang.annotation.Annotation;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.concurrent.ConcurrentHashMap;
|
|
|
-
|
|
|
-public class FieldUtil {
|
|
|
-
|
|
|
- private static final Map<Class<?>, String> tableAliasMap = new ConcurrentHashMap<>();
|
|
|
- private static final Map<Class<?>, String> tableNameMap = new ConcurrentHashMap<>();
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取sql字段名
|
|
|
- * 例:getSqlFieldName(DemoEntity::getBaseId) -》 return "de.base_id"
|
|
|
- */
|
|
|
- public static <K, V> String getSqlFieldName(SFunction<K, V> function) {
|
|
|
- LambdaMeta meta = LambdaUtils.extract(function);
|
|
|
- String alias = getTableAlias(meta.getInstantiatedClass());
|
|
|
- String fieldName = StrUtil.toUnderlineCase(PropertyNamer.methodToProperty(meta.getImplMethodName()));
|
|
|
- return alias + StringPool.DOT + fieldName;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取sql字段名
|
|
|
- * 例:getSqlFieldName("s", DemoEntity::getBaseId) -》 return "s.base_id"
|
|
|
- */
|
|
|
- public static <K, V> String getSqlFieldName(String alias, SFunction<K, V> function) {
|
|
|
- LambdaMeta meta = LambdaUtils.extract(function);
|
|
|
- String fieldName = StrUtil.toUnderlineCase(PropertyNamer.methodToProperty(meta.getImplMethodName()));
|
|
|
- return alias + StringPool.DOT + fieldName;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取表别名
|
|
|
- */
|
|
|
- public static String getTableAlias(Class<?> cls) {
|
|
|
- return tableAliasMap.computeIfAbsent(cls, itemCls -> {
|
|
|
- String tableName = getTableName(cls);
|
|
|
- StringBuilder aliasBuilder = new StringBuilder();
|
|
|
- for (String item : tableName.split("_")) {
|
|
|
- aliasBuilder.append(item, 0, 1);
|
|
|
- }
|
|
|
- return aliasBuilder.toString();
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取表名
|
|
|
- */
|
|
|
- public static String getTableName(Class<?> cls) {
|
|
|
- return tableNameMap.computeIfAbsent(cls, itemCls -> {
|
|
|
- Annotation[] annotations = itemCls.getAnnotations();
|
|
|
- TableName tableName = null;
|
|
|
- for (Annotation annotation : annotations) {
|
|
|
- if (annotation instanceof TableName) {
|
|
|
- tableName = (TableName) annotation;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- String tableNameStr = null;
|
|
|
- if (tableName != null) {
|
|
|
- tableNameStr = tableName.value();
|
|
|
- }
|
|
|
- if (StrUtil.isBlank(tableNameStr)) {
|
|
|
- tableNameStr = StrUtil.toUnderlineCase(itemCls.getSimpleName());
|
|
|
- }
|
|
|
- return tableNameStr;
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
-}
|