效果图:
代码:
package com.jxc.util; import java.io.File; import java.io.FileWriter; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.persistence.Column; import javax.persistence.JoinColumn; import javax.persistence.Table; public class TableFieldHelper { /** * @param args * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException */ public static void main(String[] args) throws Exception { String path="C:\\work\\jxc\\target\\jxc-0.0.1-SNAPSHOT\\WEB-INF\\classes\\com\\jxc\\model\\po"; List<File> files=files(path); StringBuilder sb=new StringBuilder(); sb.append("<!DOCTYPE HTML><html><head>数据字典</head><meta content=\"text/html;charset=utf-8\" http-equiv=\"content-type\"></meta><body>"); sb.append("<style>table{border-collapse:collapse;} table td{border:#CCC 1px solid;padding:5px;}</style>"); Integer count=0; for (File file : files) { String className="com.jxc.model.po."+file.getName().replace(".class", ""); Class class1=Class.forName(className); if(class1.getModifiers()!=1025){ Object object=class1.newInstance(); sb.append(createTable(object)+"<div style=‘height:1px;border-bootom:#CCC 1px solid;margin:10px 0px;‘></div>"); } count++; } sb.append("<div>表总数:"+count+"</div>"); sb.append("<div>创建时间:"+new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date())+"</div>"); sb.append("</body></html>"); FileWriter fw = new FileWriter("c:\\table.html"); fw.write(sb.toString(), 0, sb.toString().length()); fw.flush(); fw.close(); } public static String createTable(Object object) throws Exception{ StringBuilder sb=new StringBuilder(); Method[] methods=object.getClass().getDeclaredMethods(); Table table=object.getClass().getAnnotation(Table.class); if(table!=null){ sb.append("<table>"); sb.append("<tr>"); sb.append("<td colspan=‘5‘ style=‘text-align: center;‘>"+"<a href=‘javascript:void();‘ name=‘"+object.getClass().getName()+"‘>"+object.getClass().getName()+"</a> = "+table.name().toLowerCase()+"</td>"); sb.append("</tr>"); sb.append("<tr>"); sb.append("<td>数据库字段</td>"); sb.append("<td>类字段</td>"); sb.append("<td>类型</td>"); sb.append("<td>外键表</td>"); sb.append("<td>外键类</td>"); sb.append("</tr>"); } System.out.println(object.getClass().getName()); for (Method method : methods) { //根据方法获取字段类型 String field=method.getName().substring("get".length()); field=field.substring(0,1).toLowerCase()+field.substring(1); Column column=method.getAnnotation(Column.class); if(column!=null){ sb.append("<tr>"); String fieldName=column.name(); Field field2=getField(field,object); String type=""; if(field2!=null){ field2.setAccessible(true); type=field2.getType().getSimpleName(); //外键 } sb.append("<td>"+fieldName+"</td>"); sb.append("<td>"+field+"</td>"); sb.append("<td>"+type+"</td>"); sb.append("<td> </td>"); sb.append("<td> </td>"); sb.append("</tr>"); } JoinColumn joinColumn=method.getAnnotation(JoinColumn.class); if(joinColumn!=null){ Class class1=Class.forName(method.getReturnType().getName()); Table table2=class1.newInstance().getClass().getAnnotation(Table.class); if(table2!=null){ sb.append("<tr>"); sb.append("<td>"+joinColumn.name()+"</td>"); sb.append("<td>"+field+"</td>"); Field field2=getField(method.getName().substring("get".length()),object); if(field2!=null){ sb.append("<td>"+field2.getType().getSimpleName()+"</td>"); } sb.append("<td>"+table2.name()+"</td>"); sb.append("<td><a href=‘#"+method.getReturnType().getName()+"‘>"+method.getReturnType().getName()+"</a></td>"); sb.append("</tr>"); } } } sb.append("</table>"); return sb.toString(); } public static Field getField(String key,Object object) throws Exception{ Field[] fields=object.getClass().getDeclaredFields(); for (Field field : fields) { if(field.getName().toLowerCase().equals(key.toLowerCase())){ return field; } } return null; } public static List<File> files(String path){ List<File> files=new ArrayList<File>(); File file=new File(path); File[] listFiles=file.listFiles(); for (File file2 : listFiles) { if(file2.isFile()){ files.add(file2); }else { files.addAll(files(file2.getPath())); } } return files; } }
只能是注解配置的对象才会生效!