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

View File

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

View File

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

View File

@ -31,7 +31,7 @@ public class ArticleSqlTester {
article.setContent("aaa");
IDialect dialect = new CommonsDialectImpl();
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class);
TableInfo tableInfo = TableInfoFactory.getByEntityClass(Article.class);
String sql = dialect.forInsertEntity(tableInfo, article);
System.out.println(sql);
}
@ -47,7 +47,7 @@ public class ArticleSqlTester {
article2.setContent("bbb");
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));
System.out.println(sql);
}
@ -56,7 +56,7 @@ public class ArticleSqlTester {
@Test
public void testDeleteSql() {
IDialect dialect = new CommonsDialectImpl();
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class);
TableInfo tableInfo = TableInfoFactory.getByEntityClass(Article.class);
String sql = dialect.forDeleteEntityById(tableInfo);
System.out.println(sql);
}
@ -65,7 +65,7 @@ public class ArticleSqlTester {
@Test
public void testDeleteByIdsSql() {
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});
System.out.println(sql);
}
@ -79,7 +79,7 @@ public class ArticleSqlTester {
article.setVersion(1L);
IDialect dialect = new CommonsDialectImpl();
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class);
TableInfo tableInfo = TableInfoFactory.getByEntityClass(Article.class);
String sql = dialect.forUpdateEntity(tableInfo, article, true);
System.out.println(sql);
}
@ -96,7 +96,7 @@ public class ArticleSqlTester {
.where(ARTICLE.ID.ge(100));
IDialect dialect = new CommonsDialectImpl();
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class);
TableInfo tableInfo = TableInfoFactory.getByEntityClass(Article.class);
String sql = dialect.forUpdateEntityByQuery(tableInfo, article, true, queryWrapper);
System.out.println(sql);
}