optimize TableInfoFactory.java

This commit is contained in:
开源海哥 2023-04-14 10:41:43 +08:00
parent 576bd1ccae
commit b27760c969
4 changed files with 19 additions and 16 deletions

View File

@ -63,7 +63,7 @@ public class RowUtil {
public static <T> T toEntity(Row row, Class<T> entityClass) { public static <T> T toEntity(Row row, Class<T> entityClass) {
TableInfo tableInfo = TableInfoFactory.ofEntityClass(entityClass); TableInfo tableInfo = TableInfoFactory.getByEntityClass(entityClass);
return tableInfo.newInstanceByRow(row); return tableInfo.newInstanceByRow(row);
} }
@ -72,7 +72,7 @@ public class RowUtil {
if (rows == null || rows.isEmpty()) { if (rows == null || rows.isEmpty()) {
return Collections.emptyList(); return Collections.emptyList();
} else { } else {
TableInfo tableInfo = TableInfoFactory.ofEntityClass(entityClass); TableInfo tableInfo = TableInfoFactory.getByEntityClass(entityClass);
List<T> entityList = new ArrayList<>(); List<T> entityList = new ArrayList<>();
for (Row row : rows) { for (Row row : rows) {
T entity = tableInfo.newInstanceByRow(row); T entity = tableInfo.newInstanceByRow(row);

View File

@ -67,17 +67,17 @@ public class TableInfoFactory {
public static TableInfo ofMapperClass(Class<?> mapperClass) { public static TableInfo ofMapperClass(Class<?> mapperClass) {
return MapUtil.computeIfAbsent(tableInfoMap, mapperClass, key -> { return MapUtil.computeIfAbsent(tableInfoMap, mapperClass, key -> {
Class<?> entityClass = getEntityClass(mapperClass); Class<?> entityClass = getEntityClass(mapperClass);
// return entityClass != null ? ofEntityClass(entityClass) : null; if (entityClass == null){
return entityClass != null ? createTableInfo(entityClass) : null; return null;
}
TableInfo tableInfo = createTableInfo(entityClass);
tableInfoMap.put(entityClass,tableInfo);
return tableInfo;
}); });
} }
public static TableInfo ofEntityClass(Class<?> entityClass) {
return MapUtil.computeIfAbsent(tableInfoMap, entityClass, key -> createTableInfo(entityClass));
}
public static TableInfo getByEntityClass(Class<?> entityClass) { public static TableInfo getByEntityClass(Class<?> entityClass) {
return tableInfoMap.get(entityClass); return tableInfoMap.get(entityClass);
} }
@ -98,6 +98,9 @@ public class TableInfoFactory {
private static TableInfo createTableInfo(Class<?> entityClass) { private static TableInfo createTableInfo(Class<?> entityClass) {
TableInfo tableInfo = new TableInfo(); TableInfo tableInfo = new TableInfo();
tableInfo.setEntityClass(entityClass); tableInfo.setEntityClass(entityClass);
tableInfo.setReflector(new Reflector(entityClass)); tableInfo.setReflector(new Reflector(entityClass));

View File

@ -301,7 +301,7 @@ public class AccountSqlTester {
@Test @Test
public void testDeleteSql() { public void testDeleteSql() {
IDialect dialect = new CommonsDialectImpl(); IDialect dialect = new CommonsDialectImpl();
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Account.class); TableInfo tableInfo = TableInfoFactory.getByEntityClass(Account.class);
String sql = dialect.forDeleteEntityById(tableInfo); String sql = dialect.forDeleteEntityById(tableInfo);
System.out.println(sql); System.out.println(sql);
} }

View File

@ -31,7 +31,7 @@ public class ArticleSqlTester {
article.setContent("aaa"); article.setContent("aaa");
IDialect dialect = new CommonsDialectImpl(); IDialect dialect = new CommonsDialectImpl();
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class); TableInfo tableInfo = TableInfoFactory.getByEntityClass(Article.class);
String sql = dialect.forInsertEntity(tableInfo, article); String sql = dialect.forInsertEntity(tableInfo, article);
System.out.println(sql); System.out.println(sql);
} }
@ -47,7 +47,7 @@ public class ArticleSqlTester {
article2.setContent("bbb"); article2.setContent("bbb");
IDialect dialect = new CommonsDialectImpl(); IDialect dialect = new CommonsDialectImpl();
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class); TableInfo tableInfo = TableInfoFactory.getByEntityClass(Article.class);
String sql = dialect.forInsertEntityBatch(tableInfo, CollectionUtil.newArrayList(article1, article2)); String sql = dialect.forInsertEntityBatch(tableInfo, CollectionUtil.newArrayList(article1, article2));
System.out.println(sql); System.out.println(sql);
} }
@ -56,7 +56,7 @@ public class ArticleSqlTester {
@Test @Test
public void testDeleteSql() { public void testDeleteSql() {
IDialect dialect = new CommonsDialectImpl(); IDialect dialect = new CommonsDialectImpl();
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class); TableInfo tableInfo = TableInfoFactory.getByEntityClass(Article.class);
String sql = dialect.forDeleteEntityById(tableInfo); String sql = dialect.forDeleteEntityById(tableInfo);
System.out.println(sql); System.out.println(sql);
} }
@ -65,7 +65,7 @@ public class ArticleSqlTester {
@Test @Test
public void testDeleteByIdsSql() { public void testDeleteByIdsSql() {
IDialect dialect = new CommonsDialectImpl(); IDialect dialect = new CommonsDialectImpl();
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class); TableInfo tableInfo = TableInfoFactory.getByEntityClass(Article.class);
String sql = dialect.forDeleteEntityBatchByIds(tableInfo, new Object[]{1, 2, 3}); String sql = dialect.forDeleteEntityBatchByIds(tableInfo, new Object[]{1, 2, 3});
System.out.println(sql); System.out.println(sql);
} }
@ -79,7 +79,7 @@ public class ArticleSqlTester {
article.setVersion(1L); article.setVersion(1L);
IDialect dialect = new CommonsDialectImpl(); IDialect dialect = new CommonsDialectImpl();
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class); TableInfo tableInfo = TableInfoFactory.getByEntityClass(Article.class);
String sql = dialect.forUpdateEntity(tableInfo, article, true); String sql = dialect.forUpdateEntity(tableInfo, article, true);
System.out.println(sql); System.out.println(sql);
} }
@ -96,7 +96,7 @@ public class ArticleSqlTester {
.where(ARTICLE.ID.ge(100)); .where(ARTICLE.ID.ge(100));
IDialect dialect = new CommonsDialectImpl(); IDialect dialect = new CommonsDialectImpl();
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class); TableInfo tableInfo = TableInfoFactory.getByEntityClass(Article.class);
String sql = dialect.forUpdateEntityByQuery(tableInfo, article, true, queryWrapper); String sql = dialect.forUpdateEntityByQuery(tableInfo, article, true, queryWrapper);
System.out.println(sql); System.out.println(sql);
} }