{"id":115,"date":"2024-10-30T17:44:23","date_gmt":"2024-10-30T09:44:23","guid":{"rendered":"https:\/\/zhoujibin.com\/?p=115"},"modified":"2024-10-30T17:44:24","modified_gmt":"2024-10-30T09:44:24","slug":"jpa%e5%a4%9a%e8%a1%a8%e5%85%b3%e8%81%94%e6%9f%a5%e8%af%a2%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/zhoujibin.com\/?p=115","title":{"rendered":"JPA\u591a\u8868\u5173\u8054\u67e5\u8be2\u7528\u6cd5"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">\u7b80\u5355\u793a\u4f8b\u5206\u6790<\/h1>\n\n\n\n<p>\u73b0\u5728\u6709\u5b66\u751f\u8868(student)\u548c\u73ed\u7ea7\u8868(school_class)\uff0c\u8981\u6c42\u540c\u65f6\u67e5\u8be2\u51fa\u5b66\u751f\u59d3\u540d\u4fe1\u606f\u53ca\u5bf9\u73ed\u7ea7\u4fe1\u606f\uff0c\u53ef\u6839\u636e\u5b66\u751f\u59d3\u540d\u6216\u73ed\u7ea7\u59d3\u540d\u67e5\u8be2\u6570\u636e\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b57\u6bb5\u793a\u4f8b<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b66\u751f\u8868<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5b57\u6bb5\u540d<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>id<\/td><td>\u4e3b\u952eid<\/td><\/tr><tr><td>student_name<\/td><td>\u5b66\u751f\u59d3\u540d<\/td><\/tr><tr><td>class_id<\/td><td>\u73ed\u7ea7id<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u73ed\u7ea7\u8868<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5b57\u6bb5\u540d<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>id<\/td><td>\u4e3b\u952eid<\/td><\/tr><tr><td>class_name<\/td><td>\u73ed\u7ea7\u540d\u79f0<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">SQL\u5b9e\u73b0<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>select s.student_name ,c.class_name from student s join school_class c on s.class_id = c.id where s.student_name like '%keywords%' or c.class_name like '%keywords%'<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">JPA \u5b9e\u73b0<\/h3>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b66\u751f\u5b9e\u4f53\u7c7b<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>@Data\n@Entity\n@Table(name=\"student\")\npublic class StudentPO  implements Serializable {\n    private static final long serialVersionUID = 1L;\n\n    @Id\n    @Column\n    @GeneratedValue(generator = \"generator\")\n       @GenericGenerator(name = \"generator\", strategy = \"uuid.hex\")\n    private String id;\n\n    @Column\n    private String studentName;\n\n    @OneToOne\n       @NotFound(action = NotFoundAction.IGNORE)\n    @JoinColumn(name = \"class_id\", referencedColumnName = \"id\",insertable = false, updatable = false)\n    private SchoolClass schoolClass;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u73ed\u7ea7\u5b9e\u4f53\u7c7b<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>@Data\n@Entity\n@Table(name=\"school_class\")\npublic class SchoolClass  implements Serializable {\n    private static final long serialVersionUID = 1L;\n\n    @Id\n    @Column\n    @GeneratedValue(generator = \"generator\")\n       @GenericGenerator(name = \"generator\", strategy = \"uuid.hex\")\n    private String id;\n\n    @Column\n    private String className;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u6784\u5efa\u9ad8\u7ea7\u67e5\u8be2<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>    \/**\n     * \u6839\u636e\u5173\u952e\u5b57\u6a21\u7cca\u5339\u914d\u6570\u636e\n     * @param keywords\n     * @return\n     *\/\npublic static Specification&lt;StudentPO&gt; keywordsLike(String keywords) {\n        if (StrUtil.isBlank(keywords)) {\n            return null;\n        }\n        return (root, query, cb) -&gt; {\n            List&lt;Predicate&gt; conditionList = new ArrayList&lt;&gt;();\n            conditionList.add(cb.like(root.get(\"studentName\"), \"%\" + keywords + \"%\"));\n            Join&lt;StudentPO, SchoolClass&gt; join = root.join(\"schoolClass\", JoinType.INNER);\n            conditionList.add(cb.like(join.get(\"className\"), \"%\" + keywords + \"%\"));\n            return cb.and(conditionList.toArray(new Predicate&#91;conditionList.size()]));\n        };\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u5355\u793a\u4f8b\u5206\u6790 \u73b0\u5728\u6709\u5b66\u751f\u8868(student)\u548c\u73ed\u7ea7\u8868(school_class)\uff0c\u8981\u6c42\u540c\u65f6\u67e5\u8be2\u51fa\u5b66\u751f\u59d3\u540d\u4fe1\u606f\u53ca [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[21],"class_list":["post-115","post","type-post","status-publish","format-standard","hentry","category-java","tag-jpa"],"_links":{"self":[{"href":"https:\/\/zhoujibin.com\/index.php?rest_route=\/wp\/v2\/posts\/115"}],"collection":[{"href":"https:\/\/zhoujibin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhoujibin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhoujibin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhoujibin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=115"}],"version-history":[{"count":1,"href":"https:\/\/zhoujibin.com\/index.php?rest_route=\/wp\/v2\/posts\/115\/revisions"}],"predecessor-version":[{"id":116,"href":"https:\/\/zhoujibin.com\/index.php?rest_route=\/wp\/v2\/posts\/115\/revisions\/116"}],"wp:attachment":[{"href":"https:\/\/zhoujibin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhoujibin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhoujibin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}