{"id":200468,"date":"2025-05-06T10:49:07","date_gmt":"2025-05-06T02:49:07","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200468\/"},"modified":"2025-05-06T10:49:07","modified_gmt":"2025-05-06T02:49:07","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-java-%e4%b8%ad%e8%8e%b7%e5%8f%96%e5%8c%85%e5%90%ab%e5%85%b6%e8%af%84%e8%ae%ba%e7%9a%84-mysql-%e6%96%87%e7%ab%a0%e6%95%b0%e6%8d%ae%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200468\/","title":{"rendered":"\u5982\u4f55\u5728 Java \u4e2d\u83b7\u53d6\u5305\u542b\u5176\u8bc4\u8bba\u7684 MySQL \u6587\u7ae0\u6570\u636e\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u5728 Java \u4e2d\u83b7\u53d6\u5305\u542b\u5176\u8bc4\u8bba\u7684 MySQL \u6587\u7ae0\u6570\u636e\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300a\u5982\u4f55\u5728 Java \u4e2d\u83b7\u53d6\u5305\u542b\u5176\u8bc4\u8bba\u7684 MySQL \u6587\u7ae0\u6570\u636e\uff1f\u300b\uff0c\u8986\u76d6\u4e86\u6570\u636e\u5e93\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241105\/1730814529672a2241ae9da.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u83b7\u53d6\u5305\u542b\u5176\u8bc4\u8bba\u7684 java mysql \u6570\u636e<\/strong><\/p>\n<p>\u5728 java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 mysql jdbc \u9a71\u52a8\u8fde\u63a5\u5230 mysql \u6570\u636e\u5e93\u5e76\u67e5\u8be2\u6570\u636e\u3002\u5bf9\u4e8e\u6b64\u7279\u5b9a\u95ee\u9898\uff0c\u6211\u4eec\u60f3\u4ece article \u8868\u4e2d\u83b7\u53d6\u6587\u7ae0\u5185\u5bb9\uff0c\u4ee5\u53ca\u6bcf\u7bc7\u6587\u7ae0\u4e0b\u7684\u524d 5 \u6761\u8bc4\u8bba\u3002<\/p>\n<p>\u4e3a\u6b64\uff0c\u53ef\u4ee5\u4f7f\u7528 sql left join \u67e5\u8be2\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>select\n    a.id,\n    a.content,\n    c.id as comment_id,\n    c.comment\nfrom\n    article as a\nleft join\n    comment as c on a.id = c.pid\nwhere\n    c.id in (\n        select id\n        from (\n            select\n                c1.id\n            from\n                comment as c1\n            where\n                c1.pid = a.id\n            order by\n                c1.id desc\n            limit 5\n        ) as subquery\n    )<\/pre>\n<p>\u6b64\u67e5\u8be2\u4f7f\u7528\u5d4c\u5957\u67e5\u8be2\u6765\u83b7\u53d6\u6bcf\u7bc7\u6587\u7ae0\u7684\u524d 5 \u6761\u8bc4\u8bba\u3002\u5d4c\u5957\u67e5\u8be2\u8fd4\u56de\u4e00\u4e2a\u5b50\u67e5\u8be2\u8868\uff0c\u5176\u4e2d\u5305\u542b\u6bcf\u7bc7\u6587\u7ae0\u6700\u65b0\u7684 5 \u6761\u8bc4\u8bba\u7684 id\u3002\u7136\u540e\uff0c\u5728\u4e3b\u67e5\u8be2\u4e2d\u4f7f\u7528\u5b83\u6765\u83b7\u53d6\u8bc4\u8bba\u5185\u5bb9\u3002<\/p>\n<p>\u5c06\u7ed3\u679c\u6620\u5c04\u5230 java pojo \u7c7b\u65f6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 @joincolumns \u548c @jointables \u6ce8\u89e3\u6765\u521b\u5efa\u5bf9\u8c61\u7684\u5c42\u6b21\u7ed3\u6784\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u6a21\u578b\uff1a<\/p>\n<pre>@Entity\npublic class Article {\n    @Id\n    private Long id;\n    private String content;\n\n    @OneToMany(cascade = CascadeType.ALL)\n    @JoinTables({\n            @JoinTable(name = \"comment\", joinColumns = @JoinColumn(name = \"pid\"))\n    })\n    private List&lt;Comment&gt; comments;\n}\n\n@Entity\npublic class Comment {\n    @Id\n    private Long id;\n    private String comment;\n}<\/pre>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5982\u4f55\u5728 Java \u4e2d\u83b7\u53d6\u5305\u542b\u5176\u8bc4\u8bba\u7684 MySQL \u6587\u7ae0\u6570\u636e\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728 Java \u4e2d\u83b7\u53d6\u5305\u542b\u5176\u8bc4&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200468","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200468","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=200468"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200468\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}