Created
June 28, 2021 01:47
-
-
Save jerry80409/03595192df901496f96be9e51c2dda6f to your computer and use it in GitHub Desktop.
Table 欄位註解 comment 工具
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Autowired | |
private EntityManager entityManager; | |
@SneakyThrows | |
@GetMapping("/comment") | |
@Transactional(noRollbackFor = SQLGrammarException.class) | |
public ResponseEntity<String> comment(@RequestParam(required = false) String className) { | |
final ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); | |
provider.addIncludeFilter(new RegexPatternTypeFilter(Pattern.compile(".*"))); | |
final Set<BeanDefinition> classes = provider.findCandidateComponents("tw.com.softleader.jasmine.policy.entity"); | |
for (BeanDefinition bean: classes) { | |
final Class<?> clazz = Class.forName(bean.getBeanClassName()); | |
final String clazzName = clazz.getName().split("\\.")[6]; | |
if (clazzName.contains("Generic")) { | |
continue; | |
} | |
final Table table = clazz.getAnnotation(Table.class); | |
if (Objects.isNull(table)) { | |
continue; | |
} | |
// table Name | |
final String tableName = table.name(); | |
for (Field field : FieldUtils.getAllFields(clazz)) { | |
final ApiModelProperty apiModelProperty = field.getAnnotation(ApiModelProperty.class); | |
final Transient transientAnnotation = field.getAnnotation(Transient.class); | |
final Column columnAnnotation = field.getAnnotation(Column.class); | |
if (Objects.isNull(apiModelProperty) || Objects.nonNull(transientAnnotation)) { | |
continue; | |
} | |
// comment | |
final String comment = apiModelProperty.value(); | |
// column name | |
final String columnName; | |
if (Objects.nonNull(columnAnnotation)) { | |
columnName = columnAnnotation.name().toUpperCase(); | |
} else { | |
columnName = field.getName() | |
.replaceAll("([A-Z]+)([A-Z][a-z])", "$1_$2") | |
.replaceAll("([a-z])([A-Z])", "$1_$2") | |
.toUpperCase(); | |
} | |
log.info("{} - {} - {}", tableName, columnName, comment); | |
final String sql = StringUtils.format("COMMENT on COLUMN {}.{} IS '{}'", | |
tableName, columnName, comment).toUpperCase(); | |
log.info("sql: {}", sql); | |
try { | |
// execute statement | |
entityManager.createNativeQuery(sql).executeUpdate(); | |
entityManager.flush(); | |
} catch (Exception e) { | |
log.error(StringUtils.format("無法處理欄位 {}-{}-{}", tableName, columnName, comment), e); | |
} | |
} | |
} | |
return ResponseEntity.ok("down "); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
以下是改寫 Code
改為一次可以輸入多筆路徑 path 用 "," 隔開
增加判斷 Money 類別使用 Columns 會讀取不到 Column 導致無法產生註解的問題