mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-06 16:48:24 +08:00
feat: 添加 TableDef 别名缓存。
This commit is contained in:
parent
22fd37f004
commit
7ebd514436
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user