Jap+Oracles生成Comment注释替代方案
Jap+Oracles生成Comment替代方案
由于Jpa支持的mysql生成comment注解方式不支持Oracle,采取替代方案:
思路:通过注解获取表名,字段名,以及相关注释,生成Oracle添加comment的Sql语句,然后执行。
代码仅做记录,无法执行。
此方案不是最优解,欢迎提供更好的解决方案,相互交流。
public class CreateDBComment {
final static String tableName = "tableName";
final static String tableComment = "tableComment";
public static void main(String[] args) {
//entiry包路径
String packageDirName = "com.xxx.entity.table";
//生成脚本文件路径
String filePath = "d:\\user\\desktop\\sql.txt";
sqlAppend(filePath, sqlList(packageDirName));
}
public static ClassLoader getDefaultClassLoader() {
ClassLoader cl = null;
try {
cl = Thread.currentThread().getContextClassLoader();
} catch (Throwable ex) {
}
if (cl == null) {
cl = PropertiesUtil.class.getClassLoader();
if (cl == null) {
try {
cl = ClassLoader.getSystemClassLoader();
} catch (Throwable ex) {
}
}
}
return cl;
}
private static void sqlAppend(String filePath, List<Map<String, String>> list) {
list.forEach(map -> {
StringBuilder sb = new StringBuilder();
String tablename = map.get(tableName);
String tablecomment = map.get(tableComment);
if (StringUtils.isNotEmpty(tablecomment)) {
sb.append("COMMENT ON TABLE ").append(tablename).append(" IS ").append("'").append(tablecomment).append("';").append("\n");
}
map.forEach((k, v) -> {
if (!k.equals(tableName) && !k.equals(tableComment)) {
sb.append("COMMENT ON COLUMN ").append(tablename).append(".").append(k).append(" IS ").append("'").append(v).append("';").append("\n");
}
});
writeStringToFile(filePath, sb.toString());
});
}
private static List<Map<String, String>> sqlList(String packagePath) {
List<Map<String, String>> list = new ArrayList<>();
try {
String packageDirName = packagePath.replace('.', '/');
Enumeration<URL> resources = getDefaultClassLoader().getResources(packageDirName);
while (resources.hasMoreElements()) {
URL url = resources.nextElement();
// 得到协议的名称
String protocol = url.getProtocol();
if (StringUtils.equals(protocol, "file")) {
// 获取包的物理路径
String filePath = URLDecoder.decode(url.getFile(), "UTF-8");
File[] files = new File(filePath).listFiles(file -> (file.isFile() && file.getName().endsWith(".class")) || file.isDirectory());
for (int i = 0; i < files.length; i++) {
Map<String, String> table = new HashMap<>();
File file = files[i];
String fileName = file.getName();
String tablecomment = JavaDocReader.getTableComment(file.getCanonicalPath());
String classsName = fileName.substring(0, fileName.lastIndexOf("."));
if (!packagePath.isEmpty()) {
classsName = packagePath + "." + classsName;
}
Class<?> aClass = getDefaultClassLoader().loadClass(classsName);
Table annotation = aClass.getAnnotation(Table.class);
if (annotation == null) {
continue;
}
table.put(tableName, annotation.name());
table.put(tableComment, tablecomment);
Field[] fields = aClass.getDeclaredFields();
for (int j = 0; j < fields.length; j++) {
Field field = fields[j];
Comment declaredAnnotation = field.getAnnotation(Comment.class);
Column columnAnnotation = field.getAnnotation(Column.class);
if (declaredAnnotation != null && columnAnnotation != null) {
table.put(columnAnnotation.name(), declaredAnnotation.value());
}
}
list.add(table);
}
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return list;
}
public static void writeStringToFile(String filePath, String content) {
try {
// RandomAccessFile rf = new RandomAccessFile(filePath, "rw");
// rf.writeUTF(content);
// rf.close();
FileWriter fw = new FileWriter(filePath, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.append(content);
bw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}