|
@@ -144,6 +144,8 @@ public class TableInfoHelper {
|
|
}
|
|
}
|
|
return tableInfo;
|
|
return tableInfo;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /* 没有获取到缓存信息,则初始化 */
|
|
tableInfo = new TableInfo();
|
|
tableInfo = new TableInfo();
|
|
GlobalConfig globalConfig;
|
|
GlobalConfig globalConfig;
|
|
if (null != builderAssistant) {
|
|
if (null != builderAssistant) {
|
|
@@ -155,67 +157,19 @@ public class TableInfoHelper {
|
|
globalConfig = GlobalConfigUtils.defaults();
|
|
globalConfig = GlobalConfigUtils.defaults();
|
|
}
|
|
}
|
|
|
|
|
|
- /* 是否开启下划线转驼峰模式 */
|
|
|
|
|
|
+ /* 是否开启下划线转驼峰模式(开启后数据库不管大小写都能自动映射) */
|
|
boolean underCamel = builderAssistant.getConfiguration().isMapUnderscoreToCamelCase();
|
|
boolean underCamel = builderAssistant.getConfiguration().isMapUnderscoreToCamelCase();
|
|
- /* 数据库全局配置 */
|
|
|
|
- GlobalConfig.DbConfig dbConfig = globalConfig.getDbConfig();
|
|
|
|
- /* 初始化表名相关 */
|
|
|
|
- initTableName(clazz, dbConfig, tableInfo);
|
|
|
|
-
|
|
|
|
- // 开启了自定义 KEY 生成器
|
|
|
|
- if (null != dbConfig.getKeyGenerator()) {
|
|
|
|
- tableInfo.setKeySequence(clazz.getAnnotation(KeySequence.class));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
|
|
- List<TableFieldInfo> fieldList = new ArrayList<>();
|
|
|
|
- List<Field> list = getAllFields(clazz);
|
|
|
|
- // 标记是否读取到主键
|
|
|
|
- boolean isReadPK = false;
|
|
|
|
- boolean existTableId = isExistTableId(list);
|
|
|
|
- for (Field field : list) {
|
|
|
|
- /*
|
|
|
|
- * 主键ID 初始化
|
|
|
|
- */
|
|
|
|
- if (!isReadPK) {
|
|
|
|
- if (existTableId) {
|
|
|
|
- isReadPK = initTableId(underCamel, dbConfig, tableInfo, field, clazz);
|
|
|
|
- } else {
|
|
|
|
- isReadPK = initFieldId(dbConfig, tableInfo, field, clazz);
|
|
|
|
- }
|
|
|
|
- if (isReadPK) {
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- /*
|
|
|
|
- * 字段初始化
|
|
|
|
- */
|
|
|
|
- if (initTableField(underCamel, dbConfig, tableInfo, fieldList, field, clazz)) {
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- /*
|
|
|
|
- * 字段, 使用 camelToUnderline 转换驼峰写法为下划线分割法, 如果已指定 TableField , 便不会执行这里
|
|
|
|
- */
|
|
|
|
- fieldList.add(new TableFieldInfo(underCamel, dbConfig, tableInfo, field, field.getDeclaringClass()));
|
|
|
|
- }
|
|
|
|
|
|
+ /* 初始化表名相关 */
|
|
|
|
+ initTableName(clazz, globalConfig, tableInfo);
|
|
|
|
|
|
- /* 字段列表 */
|
|
|
|
- tableInfo.setFieldList(globalConfig, fieldList);
|
|
|
|
- /*
|
|
|
|
- * 未发现主键注解,提示警告信息
|
|
|
|
- */
|
|
|
|
- if (StringUtils.isEmpty(tableInfo.getKeyColumn())) {
|
|
|
|
- logger.warn(String.format("Warn: Could not find @TableId in Class: %s.", clazz.getName()));
|
|
|
|
- }
|
|
|
|
|
|
+ /* 初始化字段相关 */
|
|
|
|
+ initTableFields(clazz, underCamel, globalConfig, tableInfo);
|
|
|
|
|
|
- /*
|
|
|
|
- * 注入
|
|
|
|
- */
|
|
|
|
|
|
+ /* 放入缓存 */
|
|
TABLE_INFO_CACHE.put(clazz.getName(), tableInfo);
|
|
TABLE_INFO_CACHE.put(clazz.getName(), tableInfo);
|
|
|
|
|
|
- /*
|
|
|
|
- * 缓存 Lambda 映射关系
|
|
|
|
- */
|
|
|
|
|
|
+ /* 缓存 Lambda 映射关系 */
|
|
LambdaUtils.createCache(clazz, tableInfo);
|
|
LambdaUtils.createCache(clazz, tableInfo);
|
|
return tableInfo;
|
|
return tableInfo;
|
|
}
|
|
}
|
|
@@ -225,13 +179,16 @@ public class TableInfoHelper {
|
|
* 初始化 表数据库类型,表名,resultMap
|
|
* 初始化 表数据库类型,表名,resultMap
|
|
* </p>
|
|
* </p>
|
|
*
|
|
*
|
|
- * @param clazz class of entity
|
|
|
|
- * @param dbConfig 数据库全局配置
|
|
|
|
- * @param tableInfo 数据库表反射信息
|
|
|
|
|
|
+ * @param clazz class of entity
|
|
|
|
+ * @param globalConfig 全局配置
|
|
|
|
+ * @param tableInfo 数据库表反射信息
|
|
*/
|
|
*/
|
|
- public static void initTableName(Class<?> clazz, GlobalConfig.DbConfig dbConfig, TableInfo tableInfo) {
|
|
|
|
|
|
+ public static void initTableName(Class<?> clazz, GlobalConfig globalConfig, TableInfo tableInfo) {
|
|
|
|
+ /* 数据库全局配置 */
|
|
|
|
+ GlobalConfig.DbConfig dbConfig = globalConfig.getDbConfig();
|
|
/* 设置数据库类型 */
|
|
/* 设置数据库类型 */
|
|
tableInfo.setDbType(dbConfig.getDbType());
|
|
tableInfo.setDbType(dbConfig.getDbType());
|
|
|
|
+
|
|
/* 设置表名 */
|
|
/* 设置表名 */
|
|
TableName table = clazz.getAnnotation(TableName.class);
|
|
TableName table = clazz.getAnnotation(TableName.class);
|
|
String tableName = clazz.getSimpleName();
|
|
String tableName = clazz.getSimpleName();
|
|
@@ -260,6 +217,67 @@ public class TableInfoHelper {
|
|
if (table != null && StringUtils.isNotEmpty(table.resultMap())) {
|
|
if (table != null && StringUtils.isNotEmpty(table.resultMap())) {
|
|
tableInfo.setResultMap(table.resultMap());
|
|
tableInfo.setResultMap(table.resultMap());
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /* 开启了自定义 KEY 生成器 */
|
|
|
|
+ if (null != dbConfig.getKeyGenerator()) {
|
|
|
|
+ tableInfo.setKeySequence(clazz.getAnnotation(KeySequence.class));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * <p>
|
|
|
|
+ * 初始化 表主键,表字段
|
|
|
|
+ * </p>
|
|
|
|
+ *
|
|
|
|
+ * @param clazz class of entity
|
|
|
|
+ * @param underCamel 下划线转驼峰
|
|
|
|
+ * @param globalConfig 全局配置
|
|
|
|
+ * @param tableInfo 数据库表反射信息
|
|
|
|
+ */
|
|
|
|
+ public static void initTableFields(Class<?> clazz, boolean underCamel, GlobalConfig globalConfig,
|
|
|
|
+ TableInfo tableInfo) {
|
|
|
|
+ /* 数据库全局配置 */
|
|
|
|
+ GlobalConfig.DbConfig dbConfig = globalConfig.getDbConfig();
|
|
|
|
+ List<Field> list = getAllFields(clazz);
|
|
|
|
+ // 标记是否读取到主键
|
|
|
|
+ boolean isReadPK = false;
|
|
|
|
+ boolean existTableId = isExistTableId(list);
|
|
|
|
+
|
|
|
|
+ List<TableFieldInfo> fieldList = new ArrayList<>();
|
|
|
|
+ for (Field field : list) {
|
|
|
|
+ /*
|
|
|
|
+ * 主键ID 初始化
|
|
|
|
+ */
|
|
|
|
+ if (!isReadPK) {
|
|
|
|
+ if (existTableId) {
|
|
|
|
+ isReadPK = initTableId(underCamel, dbConfig, tableInfo, field, clazz);
|
|
|
|
+ } else {
|
|
|
|
+ isReadPK = initFieldId(dbConfig, tableInfo, field, clazz);
|
|
|
|
+ }
|
|
|
|
+ if (isReadPK) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ /*
|
|
|
|
+ * 字段初始化
|
|
|
|
+ */
|
|
|
|
+ if (initTableField(underCamel, dbConfig, tableInfo, fieldList, field, clazz)) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ /*
|
|
|
|
+ * 字段, 使用 camelToUnderline 转换驼峰写法为下划线分割法, 如果已指定 TableField , 便不会执行这里
|
|
|
|
+ */
|
|
|
|
+ fieldList.add(new TableFieldInfo(underCamel, dbConfig, tableInfo, field, field.getDeclaringClass()));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /* 字段列表 */
|
|
|
|
+ tableInfo.setFieldList(globalConfig, fieldList);
|
|
|
|
+ /*
|
|
|
|
+ * 未发现主键注解,提示警告信息
|
|
|
|
+ */
|
|
|
|
+ if (StringUtils.isEmpty(tableInfo.getKeyColumn())) {
|
|
|
|
+ logger.warn(String.format("Warn: Could not find @TableId in Class: %s.", clazz.getName()));
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|