{"id":578,"date":"2019-06-01T00:02:37","date_gmt":"2019-05-31T16:02:37","guid":{"rendered":"https:\/\/fick707.com\/?p=578"},"modified":"2019-06-01T00:02:52","modified_gmt":"2019-05-31T16:02:52","slug":"spring-data-projection","status":"publish","type":"post","link":"https:\/\/fick707.com\/?p=578","title":{"rendered":"spring-data \u7684 projection"},"content":{"rendered":"<h1 id=\"questionTitle\" class=\"h2 post-topheader__info--title\" data-id=\"1010000016704304\"><a href=\"https:\/\/segmentfault.com\/q\/1010000016704304\">spring data jpa\u600e\u4e48\u505a\u5230\u53ea\u67e5\u8be2\u90e8\u5206\u5b57\u6bb5\u800c\u4e0d\u8fd4\u56de\u5168\u90e8\u5b57\u6bb5\uff1f<\/a><\/h1>\n<p>\u4f7f\u7528 spring-data \u7684 projection \u5373\u53ef\uff1a<\/p>\n<pre class=\"hljs java\"><code><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">interface<\/span> <span class=\"hljs-title\">UserDto<\/span> <\/span>{\n  <span class=\"hljs-function\">Integer <span class=\"hljs-title\">getId<\/span><span class=\"hljs-params\">()<\/span><\/span>;\n  <span class=\"hljs-function\">String <span class=\"hljs-title\">getUsername<\/span><span class=\"hljs-params\">()<\/span><\/span>;\n  <span class=\"hljs-function\">String <span class=\"hljs-title\">getPassword<\/span><span class=\"hljs-params\">()<\/span><\/span>;\n}\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">interface<\/span> <span class=\"hljs-title\">UserRepository<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">JpaRepository<\/span>&lt;<span class=\"hljs-title\">User<\/span>, <span class=\"hljs-title\">Integer<\/span>&gt; <\/span>{\n  <span class=\"hljs-comment\">\/\/ smart way<\/span>\n  List&lt;UserDto&gt; findBy...(...);\n\n  <span class=\"hljs-comment\">\/\/ \u6216\u8005\u4f7f\u7528\u81ea\u5b9a\u4e49\u67e5\u8be2<\/span>\n  <span class=\"hljs-meta\">@Query<\/span>(<span class=\"hljs-string\">\"select id, username, password from User where ...\"<\/span>)\n  <span class=\"hljs-function\">List&lt;UserDto&gt; <span class=\"hljs-title\">findUserDto<\/span><span class=\"hljs-params\">(...)<\/span><\/span>;\n}<\/code><\/pre>\n\n\n<p><a href=\"https:\/\/segmentfault.com\/q\/1010000016704304\/\">https:\/\/segmentfault.com\/q\/1010000016704304\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>spring data jpa\u600e\u4e48\u505a\u5230\u53ea\u67e5\u8be2\u90e8\u5206\u5b57\u6bb5\u800c\u4e0d\u8fd4\u56de\u5168\u90e8\u5b57\u6bb5\uff1f \u4f7f\u7528 spring-data &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":[],"series":[],"class_list":["post-578","post","type-post","status-publish","format-standard","hentry","category-programmers"],"_links":{"self":[{"href":"https:\/\/fick707.com\/index.php?rest_route=\/wp\/v2\/posts\/578"}],"collection":[{"href":"https:\/\/fick707.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fick707.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fick707.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fick707.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=578"}],"version-history":[{"count":1,"href":"https:\/\/fick707.com\/index.php?rest_route=\/wp\/v2\/posts\/578\/revisions"}],"predecessor-version":[{"id":579,"href":"https:\/\/fick707.com\/index.php?rest_route=\/wp\/v2\/posts\/578\/revisions\/579"}],"wp:attachment":[{"href":"https:\/\/fick707.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fick707.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fick707.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=578"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/fick707.com\/index.php?rest_route=%2Fwp%2Fv2%2Fseries&post=578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}