我有一些遗留的数据库表和列名,如:
XYZEMP:
- XYZCMP
- XYZUSR
- XYZNME
可以映射到人类可读的
员工
- 公司
- 用户名
- 名称
当我使用标准的JOOQ代码生成时,它会创建Table对象XYXEMP,XyzempRecord等,其中包含XYZCMP列和相应的getter和setter. 有没有什么方法可以将映射传递给生成器,以便生成Table对象Employee、AccesseRecord和人类可读的列名?
我的 idea 是用Custom generator strategy
public class AsInDatabaseStrategy extends DefaultGeneratorStrategy {
@Override
public String getJavaClassName(Definition definition, Mode mode) {
Map<String, String> tableNamesMapping = readTableNames();
if (mode == Mode.DEFAULT){ //for table class
String readableName = tableNamesMapping.get(definition.getOutputName());
if (readableName == null){
throw new IllegalStateException("Table " + definition.getOutputName() + " lacks mapping");
}
return readableName;
} else if (mode == Mode.RECORD) { // for record
// similar logic for record
return readableName + "Record";
}
return super.getJavaClassName(definition, mode);
}
@Override
public String getJavaIdentifier(Definition definition) {
// somehow similar code for table and and column
}
@Override
public String getJavaSetterName(Definition definition, Mode mode) {
// somehow similar code for table and and column
}
@Override
public String getJavaGetterName(Definition definition, Mode mode) {
// somehow similar code for table and and column
}
private Map<String, String> readTableNames(){
//read names mapping from file
}
private Map<String, Map<String, String>> readColumnNames(){
//read names mapping from file
}
}
映射文件可以用于表:
{
"XYZEMP" : "员工",
...
}
为列
{
"XYZEMP" : {
"XYZCMP" : "公司",
"XYZUSR: : "用户名",
"XYZNME" : "名称"
}
...
}
Do you see smarter way for doing that? In case of my approach - how to distinguish with "Definition" object is it table or column (or any other object) and 为列 which table it belongs to?