{"id":6975,"date":"2024-11-14T15:11:26","date_gmt":"2024-11-14T07:11:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6975\/"},"modified":"2024-11-14T15:11:26","modified_gmt":"2024-11-14T07:11:26","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:\/\/fwq.ai\/blog\/6975\/","title":{"rendered":"\u5982\u4f55\u5728 Java \u4e2d\u83b7\u53d6\u5305\u542b\u5176\u8bc4\u8bba\u7684 MySQL \u6587\u7ae0\u6570\u636e\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173020710636413.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728 Java \u4e2d\u83b7\u53d6\u5305\u542b\u5176\u8bc4\u8bba\u7684 MySQL \u6587\u7ae0\u6570\u636e\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728 Java \u4e2d\u83b7\u53d6\u5305\u542b\u5176\u8bc4\u8bba\u7684 MySQL \u6587\u7ae0\u6570\u636e\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u83b7\u53d6\u5305\u542b\u5176\u8bc4\u8bba\u7684 java  \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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728 Java \u4e2d\u83b7\u53d6\u5305\u542b\u5176\u8bc4\u8bba\u7684 MySQL \u6587\u7ae0\u6570\u636e\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u83b7\u53d6\u5305\u542b\u5176\u8bc4\u8bba\u7684 java \u6570\u636e \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 \u4e3a\u6b64\uff0c\u53ef\u4ee5\u4f7f\u7528 sql left join \u67e5\u8be2\uff0c\u5982\u4e0b\u6240\u793a\uff1a select a.id, a.content, c.id as comment_id, c.comment from article as a left join comment as c on a.id = c.pid where c.id in ( select id from ( select c1.id from comment as c1 where [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-6975","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6975","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=6975"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6975\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6975"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}