feat: 添加 TableDef 别名缓存。

This commit is contained in:
Suomm 2024-03-11 21:17:57 +08:00
parent 22fd37f004
commit 7ebd514436
3 changed files with 15 additions and 4 deletions

View File

@ -18,13 +18,16 @@ package com.mybatisflex.core.table;
import com.mybatisflex.core.query.QueryTable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
/**
* 表定义内包含字段
*
* @author 王帅
* @since 2024-03-11
*/
@Deprecated
public abstract class TableDef extends QueryTable {
protected TableDef(String schema, String tableName) {
@ -36,13 +39,19 @@ public abstract class TableDef extends QueryTable {
}
/**
* 使用 {@link #getName()} 替代
* 兼容方法 {@link #getName()} 相同
*
* @return 表名
*/
@Deprecated
public String getTableName() {
return name;
}
private static final Map<String, TableDef> CACHE = new ConcurrentHashMap<>();
@SuppressWarnings("unchecked")
protected static <V extends TableDef> V getCache(String key, Function<String, V> mappingFunction) {
return (V) CACHE.computeIfAbsent(key, mappingFunction);
}
}

View File

@ -64,6 +64,7 @@ public class AccountSqlTester {
" LEFT JOIN ` tb_account ` AS ` a1 ` ON ` a1 `.` id ` = ` ar `.` account_id `\n" +
" LEFT JOIN ` tb_account ` AS ` a2 ` ON ` a2 `.` id ` = ` ar `.` account_id `", sql);
System.out.println(sql);
System.out.println(a1 == ACCOUNT.as("a1"));
}
@Test

View File

@ -143,7 +143,8 @@ public class ContentBuilder {
.append(" }\n\n");
content.append(" public ").append(tableDefClassName).append(" as(String alias) {\n")
.append(" return new ").append(tableDefClassName).append("(\"").append(schema).append("\", \"").append(tableName).append("\", alias);\n")
.append(" String key = getNameWithSchema() + \".\" + alias;\n")
.append(" return getCache(key, k -> new ").append(tableDefClassName).append("(\"").append(schema).append("\", \"").append(tableName).append("\", alias));\n")
.append(" }\n\n}\n");
return content.toString();
}