{"id":126,"date":"2022-02-23T14:20:41","date_gmt":"2022-02-23T06:20:41","guid":{"rendered":"https:\/\/blog.zjia.me\/?p=126"},"modified":"2026-06-19T21:54:35","modified_gmt":"2026-06-19T13:54:35","slug":"java%e5%ae%9e%e4%bd%93%e7%94%9f%e6%88%90sql","status":"publish","type":"post","link":"https:\/\/blog.ttwow.top\/?p=126","title":{"rendered":"Java\u5b9e\u4f53\u751f\u6210sql"},"content":{"rendered":"<p>\u53c2\u8003\uff1a<a href=\"https:\/\/www.cnblogs.com\/vmuu\/p\/15663273.html\">https:\/\/www.cnblogs.com\/vmuu\/p\/15663273.html<\/a><\/p>\n<pre><code class=\"language-java\">import javax.xml.bind.annotation.XmlElement;\nimport java.io.*;\nimport java.lang.annotation.Annotation;\nimport java.lang.reflect.Field;\n\npublic class GenerateSqlFromEntityUtil {\n\n    public static void main(String[] a) {\n        \/\/ \u5b9e\u4f53\u7c7b\u7684\u4f4d\u7f6e\n        Class klass = cn.ac.azure.model.User.class;\n        \/\/ \u751f\u6210\u7684sql\u8bed\u53e5\u7684\u4f4d\u7f6e\n        String outputPath = &quot;D:\/outSql\/User.txt&quot;;\n        generateTableSql(klass, outputPath, null);\n        System.out.println(&quot;\u751f\u6210\u7ed3\u675f&quot;);\n    }\n\n    public static void writeFile(String content, String outputPath) {\n        File file = new File(outputPath);\n        System.out.println(&quot;\u6587\u4ef6\u8def\u5f84: &quot; + file.getAbsolutePath());\n        \/\/ \u8f93\u51fa\u6587\u4ef6\u7684\u8def\u5f84\n        if (!file.getParentFile().exists()) {\n            file.getParentFile().mkdirs();\n        }\n        FileOutputStream fos = null;\n        OutputStreamWriter osw = null;\n        BufferedWriter out = null;\n\n        try {\n            \/\/ \u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u5c31\u5220\u9664\n            if (file.exists()) {\n                file.delete();\n            }\n            file.createNewFile();\n            fos = new FileOutputStream(file, true);\n            osw = new OutputStreamWriter(fos);\n            out = new BufferedWriter(osw);\n            out.write(content);\n            \/\/ \u6e05\u7a7a\u7f13\u51b2\u6d41\uff0c\u628a\u7f13\u51b2\u6d41\u91cc\u7684\u6587\u672c\u6570\u636e\u5199\u5165\u5230\u76ee\u6807\u6587\u4ef6\u91cc\n            out.flush();\n        } catch (FileNotFoundException e) {\n            e.printStackTrace();\n        } catch (IOException e) {\n            e.printStackTrace();\n        } finally {\n            try {\n                fos.close();\n            } catch (IOException e) {\n                e.printStackTrace();\n            }\n\n            try {\n                osw.close();\n            } catch (IOException e) {\n                e.printStackTrace();\n            }\n\n            try {\n                out.close();\n            } catch (IOException e) {\n                e.printStackTrace();\n            }\n        }\n    }\n\n    public static void generateTableSql(Class obj, String outputPath, String tableName) {\n        \/\/ tableName \u5982\u679c\u662f null\uff0c\u5c31\u7528\u7c7b\u540d\u505a\u8868\u540d\n        if (tableName == null || tableName.equals(&quot;&quot;)) {\n            tableName = obj.getName();\n            tableName = tableName.substring(tableName.lastIndexOf(&quot;.&quot;) + 1);\n        }\n        \/\/ \u8868\u540d\u7528\u5927\u5199\u5b57\u6bcd\n        tableName = tableName.toUpperCase();\n\n        Field[] fields = obj.getDeclaredFields();\n        Object param;\n        String column;\n\n        StringBuilder sb = new StringBuilder();\n\n        sb.append(&quot;drop table if exists &quot;).append(tableName).append(&quot;;\\r\\n&quot;);\n\n        sb.append(&quot;\\r\\n&quot;);\n\n        sb.append(&quot;create table &quot;).append(tableName).append(&quot;(\\r\\n&quot;);\n\n        System.out.println(tableName);\n\n        boolean firstId = true;\n\n        for (int i = 0; i &lt; fields.length; i++) {\n            Field f = fields[i];\n\n            column = f.getName();\n\n            System.out.println(column + &quot;, &quot; + f.getType().getSimpleName());\n\n            param = f.getType();\n            sb.append(column); \/\/ \u4e00\u822c\u7b2c\u4e00\u4e2a\u662f\u4e3b\u952e\n\n            if (param instanceof Integer) {\n                sb.append(&quot; INTEGER &quot;);\n            } else {\n                \/\/ \u6ce8\u610f\uff1a\u6839\u636e\u9700\u8981\uff0c\u81ea\u884c\u4fee\u6539 varchar \u7684\u957f\u5ea6\u3002\u8fd9\u91cc\u8bbe\u5b9a\u4e3a\u957f\u5ea6\u7b49\u4e8e 50\n                int length = 50;\n                sb.append(&quot; VARCHAR(&quot; + length + &quot;)&quot;);\n            }\n\n            if (firstId == true) {\n                sb.append(&quot; PRIMARY KEY &quot;);\n                firstId = false;\n            }\n\n            \/\/ \u83b7\u53d6\u5b57\u6bb5\u4e2d\u5305\u542b fieldMeta \u7684\u6ce8\u89e3\n\n            \/\/ \u83b7\u53d6\u5c5e\u6027\u7684\u6240\u6709\u6ce8\u91ca\n            Annotation[] allAnnotations = f.getAnnotations();\n\n            XmlElement xmlElement = null;\n            Class annotationType = null;\n\n            for (Annotation an : allAnnotations) {\n                sb.append(&quot; COMMIT &#039;&quot;);\n                xmlElement = (XmlElement) an;\n                annotationType = an.annotationType();\n                param = ((XmlElement) an).name();\n                System.out.println(&quot;\u5c5e\u6027 &quot; + f.getName() + &quot; ----- \u7684\u6ce8\u91ca\u7c7b\u578b\u6709: &quot; + param);\n                sb.append(param).append(&quot;&#039;&quot;);\n            }\n\n            if (i != fields.length - 1) { \/\/ \u6700\u540e\u4e00\u4e2a\u5c5e\u6027\u540e\u9762\u4e0d\u52a0\u9017\u53f7\n                sb.append(&quot;, &quot;);\n            }\n\n            sb.append(&quot;\\n&quot;);\n        }\n\n        String sql = sb.toString();\n\n        sql = sb.substring(0, sql.length() - 1) + &quot;\\n) &quot; + &quot;ENGINE = INNODB DEFAULT CHARSET = utf8;&quot;;\n\n        writeFile(sql, outputPath);\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u53c2\u8003\uff1ahttps:\/\/www.cnblogs.com\/vmuu\/p\/15663273.html import  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[135],"tags":[73],"class_list":["post-126","post","type-post","status-publish","format-standard","hentry","category-code-snippets","tag-sql"],"_links":{"self":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts\/126","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=126"}],"version-history":[{"count":1,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts\/126\/revisions"}],"predecessor-version":[{"id":2190,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts\/126\/revisions\/2190"}],"wp:attachment":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}