{"id":108193,"date":"2020-05-30T22:00:05","date_gmt":"2020-05-30T14:00:05","guid":{"rendered":"http:\/\/4563.org\/?p=108193"},"modified":"2020-05-30T22:00:05","modified_gmt":"2020-05-30T14:00:05","slug":"spring-data-jpa-nativequery-%e7%9a%84%e9%97%ae%e9%a2%98%ef%bc%8c%e5%9c%a8%e7%ba%bf%e8%b7%aa%e6%b1%82%e5%a4%a7%e4%bd%ac%e8%a7%a3%e7%ad%94%e3%80%82","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=108193","title":{"rendered":"Spring Data JPA nativeQuery \u7684\u95ee\u9898\uff0c\u5728\u7ebf\u8dea\u6c42\u5927\u4f6c\u89e3\u7b54\u3002"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Spring Data JPA nativeQuery \u7684\u95ee\u9898\uff0c\u5728\u7ebf\u8dea\u6c42\u5927\u4f6c\u89e3\u7b54\u3002               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : snailsir <\/span>  <span><i><\/i> 1<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u80cc\u666f\uff1a\u672c\u4eba\u4e0d\u4f1a java\uff0c\u4f46\u662f\u57fa\u672c\u4e0a\u80fd\u591f\u770b\u61c2\u903b\u8f91\uff0c\u7ef4\u62a4\u4e00\u4e2a\u8001 java \u9879\u76ee\uff0c\u73b0\u5728\u9047\u5230\u4e00\u4e2a\u6162\u67e5\u8be2\u7684\u95ee\u9898\uff08 mysql \u6700\u540e\u9009\u62e9\u4e86\u4e00\u4e2a\u9519\u8bef\u7684\u7d22\u5f15\uff09\uff0c\u4e0d\u8003\u8651\u6570\u636e\u5e93\u8868\u7684\u4fee\u6539\uff0c\u7d22\u5f15\u90fd\u662f\u6b63\u5e38\u7684\u3002<\/p>\n<p>spring-data-jpa \u7248\u672c\uff1a1.6.5 \u3002<\/p>\n<p>\u95ee\u9898\u63cf\u8ff0\u5982\u4e0b\uff1a<\/p>\n<p>\u9996\u5148 status \u662f\u4e00\u4e2a\u679a\u4e3e\u3002\u7136\u540e\u6709\u4e0b\u9762\u6709\u4e24\u4e2a\u57fa\u4e8e JPA \u67e5\u8be2\u6570\u636e\u7684\u65b9\u6cd5<\/p>\n<pre><code>public enum Status {     UPLOADING,  \/\/ 0     PROCESSING, \/\/ 1     PUBLISHED,  \/\/ 2     FAILED,     \/\/ 3     DELETED;    \/\/ 4 }      \/\/ \u65b9\u6cd5\u4e00\uff1a\u751f\u6210\u7684 sql \u5b58\u5728\u6162\u67e5\u8be2\u95ee\u9898\uff08 mysql \u9009\u62e9\u4e86\u4e00\u4e2a\u9519\u8bef\u7684\u7d22\u5f15\uff09 Slice&lt;Document&gt; findByUserIdAndStatusIn(String userId, Collection&lt;Status&gt; status, Pageable pageable);  \/\/ \u65b9\u6cd5\u4e8c\uff1a\u4f7f\u7528 nativeQuery\uff0c\u6307\u5b9a\u4f7f\u7528\u7684\u7d22\u5f15 @Query(value = \"select * from #{#entityName} use index (userId_createTime_idx) \"                 + \" where userId = :userId and status in :status \"                 + \" order by createTime desc \"                 + \" limit :offset,:size\",     nativeQuery = true) List&lt;Document&gt; findByUserIdAndStatusInUseIndex(@Param(\"userId\") String userId,                                                 @Param(\"status\") Collection&lt;Status&gt; status,                                                 @Param(\"offset\") int limitOffset,                                                 @Param(\"size\") int limitSize); <\/code><\/pre>\n<p>\u7b2c\u4e00\u4e2a\u65b9\u6cd5\u6700\u540e\u751f\u6210\u7684 sql \u8bed\u53e5\u662f\u6b63\u5e38\u7684\uff0c\u6bd4\u5982<\/p>\n<pre><code>status in (1,2,3) <\/code><\/pre>\n<p>\u7b2c\u4e8c\u4e2a\u65b9\u6cd5\u662f\u65b0\u589e\u7684\uff0c\u4f7f\u7528 nativeQuery \u6307\u5b9a\u4f7f\u7528\u7d22\u5f15\uff0c\u4f46\u57fa\u4e8e\u540c\u6837\u7684\u53c2\u6570<code>Collection&lt;Status&gt; status<\/code>\u751f\u6210\u7684 sql \u8bed\u53e5\uff0c\u5c31\u4e0d\u6b63\u5e38\uff0c\u6bd4\u5982<\/p>\n<pre><code>\/\/ \u91cc\u9762\u7684...\u7ffb\u8bd1\u540e\uff0c\u6d89\u53ca\u654f\u611f\u4fe1\u606f\uff0c\u6545\u7528...\u4ee3\u66ff status in (     x'...2E706C6174666F726D2E6D6F64656C2E646F63756D656E742E53746174757300000000000000001200007872000E6A6176612E6C616E672E456E756D0000000000000000120000787074000A50524F43455353494E47',      x'...2E706C6174666F726D2E6D6F64656C2E646F63756D656E742E53746174757300000000000000001200007872000E6A6176612E6C616E672E456E756D000000000000000012000078707400095055424C4953484544',      x'...2E706C6174666F726D2E6D6F64656C2E646F63756D656E742E53746174757300000000000000001200007872000E6A6176612E6C616E672E456E756D000000000000000012000078707400064641494C4544')   <\/code><\/pre>\n<p>\u8fd9\u51e0\u4e2a\u83ab\u540d\u7684\u5b57\u7b26\uff0c\u7ecf\u8fc7\u7ffb\u8bd1\u4e4b\u540e\uff0c\u662f\u8fd9\u6837\u7684<\/p>\n<pre><code>.platform.model.document.Status           xr  java.lang.Enum           xpt  PROCESSING  .platform.model.document.Status           xr  java.lang.Enum           xpt  PUBLISHED  .platform.model.document.Status           xr  java.lang.Enum           xpt  FAILED <\/code><\/pre>\n<p>\u6240\u4ee5\uff0c\u4f7f\u7528 nativeQuery \u4e4b\u540e\uff0c\u4e3a\u4ec0\u4e48\u540c\u6837\u7684\u53c2\u6570<code>Collection&lt;Status&gt; status<\/code>\u6700\u540e\u751f\u6210\u7684 sql \u8bed\u53e5\u5c31\u4e0d\u5bf9\u4e86\uff1f\u5982\u679c\u662f\u4f7f\u7528\u59ff\u52bf\u4e0d\u5bf9\uff0c\u90a3\u8be5\u600e\u4e48\u89e3\u51b3\u554a\uff1f[\u5feb\u54ed\u4e86]<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>11<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1803765\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : duwan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u628a\u53c2\u6570 Collection&lt;Status&gt;\u6362\u6210 Collection&lt;Integer&gt;\u4e0d\u5c31\u884c\u4e86\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1803766\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : duwan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7136\u540e\uff0c\u5148\u5c06 Collection&lt;Status&gt;\u8f6c\u6362\u6210 Collection&lt;Integer&gt;\uff0c\u518d\u8c03\u7528                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1803767\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : snailsir <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @duwan \u600e\u4e48\u8f6c\u6362\u554a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1803768\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lybcyd <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             JPA \u628a\u679a\u4e3e\u586b\u8fdb SQL \u4e0d\u4f1a\u81ea\u52a8\u8f6c\u6362\u5427\u3002\u4f60\u624b\u52a8\u8f6c\u6362\u6210 int \u5c31\u53ef\u4ee5\u4e86\u3002\u4e0d\u8fc7\u5efa\u8bae\u4f60\u770b\u770b jpa \u81ea\u52a8\u751f\u6210\u7684\u8bed\u53e5\u4e3a\u4ec0\u4e48\u4e0d\u4f1a\u7528\u5230\u6b63\u786e\u7684\u7d22\u5f15\uff0c\u8fd8\u9700\u8981\u624b\u52a8\u6307\u5b9a\u7d22\u5f15\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1803769\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hantsy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             nativeQuery \u57fa\u672c\u5bf9\u5e94 Jdbc \u8bed\u53e5\uff0c\u6267\u884c\u6570\u636e\u5e93\u539f\u59cb\u7684 SQL \u3002<\/p>\n<p>\u5728\u7528 JPA \u771f\u7684\u5f88\u5c11\u7528\u3002\u5fd8\u8bb0\u4e0d\u4e86 SQL\uff0c\u8fd8\u4e0d\u5982\u76f4\u63a5\u7528 jdbc \u3002Spring \u7684 Jdbc \u5c01\u88c5\u771f\u7684\u4e0d\u9519\uff0c\u53e6\u5916 Spring Data Jdbc \u76f8\u5f53\u4e8e\u4e00\u4e2a\u7b80\u5355\u7684 ORM \u6846\u67b6\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1803770\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : snailsir <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @lybcyd \u4f46\u662f\u7b2c\u4e00\u4e2a\u975e nativeQuery \u7684\u65b9\u5f0f\uff0c\u751f\u6210\u7684 `status in (1,2,3)` \u5c31\u662f\u6b63\u786e\u7684\u554a\u3002\u53ea\u662f\u4e0b\u9762\u4f7f\u7528 nativeQuery \u7684\u65f6\u5019\uff0c\u5c31\u4e0d\u6b63\u5e38\u4e86\u3002<\/p>\n<p>\u624b\u52a8\u8f6c\u6362\u6210 int \u600e\u4e48\u641e\u5440\uff0c\u672c\u4eba\u4e0d\u592a\u4f1a java [\u54ce]                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1803771\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : snailsir <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @hantsy \u8fd9\u4e2a\u662f\u8001\u9879\u76ee\uff0c\u4f7f\u7528\u7684 JPA\uff0c\u76ee\u524d\u90fd\u4e0d\u60f3\u52a8\u4e86\uff0c\u53ea\u662f\u7ef4\u62a4\uff0c\u6539\u6539 bug \u7684\u90a3\u79cd\u6837\u5b50\uff0c\u672c\u4eba\u4e5f\u4e0d\u5927\u4f1a java\uff0c\u6240\u4ee5\u6362\u57fa\u672c\u4e0a\u662f\u4e0d\u53ef\u80fd\u7684 [\u65e0\u5948]                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1803772\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : kevinWHX <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             UPLOADING.ordinal() =1                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1803773\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Jrue0011 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0d\u592a\u4e86\u89e3 SPRING DATA JPA\uff0c\u4f3c\u4e4e\u7528\u4e86 nativeQuery \u540e\u5c31\u4e0d\u652f\u6301\u679a\u4e3e\u7684\u5904\u7406\uff0c\u53ef\u80fd\u4f5c\u4e3a Object \u5e8f\u5217\u5316\u4e86\u3002<\/p>\n<p>\u89e3\u51b3\u529e\u6cd5\uff1a<\/p>\n<p>1 \u3001\u53c2\u6570\u6539\u6210 Collections&lt;Integer&gt;\uff0c\u7136\u540e\u8c03\u7528\u7684\u5730\u65b9\u624b\u52a8\u5c06 Collections&lt;Status&gt;\u8f6c\u6210 Collections&lt;Integer&gt;<\/p>\n<p>2 \u3001\u53c2\u8003\u8fd9\u4e2a\u5b9a\u4e49\u4e00\u4e2a\u63a5\u53e3\u548c\u5b9e\u73b0\uff0c\u5b9e\u73b0\u6ce8\u518c bean\uff0c\u53ef\u4ee5\u6ce8\u5165 jdbcTemplate \u505a\u67d0\u4e9b\u64cd\u4f5c\uff0c\u6700\u540e\u8ba9\u8fd9\u4e2a repository \u7ee7\u627f\u81ea\u5b9a\u4e49\u63a5\u53e3\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>https:\/\/docs.spring.io\/spring-data\/jpa\/docs\/1.6.5.RELEASE\/reference\/html\/repositories.html#repositories.single-repository-behaviour                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1803774\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : snailsir <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Jrue0011 \u55ef\uff0c\u6211\u4e5f\u662f\u611f\u89c9\u4f7f\u7528 nativeQuery \u4e4b\u540e\uff0c\u679a\u4e3e\u5c31\u4e0d\u884c\u4e86\u3002\u76ee\u524d\u5728\u8003\u8651\u5c06 Collections&lt;Status&gt; \u8f6c\u6210 Collections&lt;Integer&gt;\uff0c\u7136\u540e\u518d\u8bd5\u8bd5\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1803775\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : snailsir <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Jrue0011 \u4f46\u662f\u4e0d\u77e5\u9053\u4e3a\u5565\u4f7f\u7528 nativeQuery \u4e4b\u540e\uff0c\u679a\u4e3e\u5c31\u4e0d\u884c\u4e86\uff0c\u54ea\u91cc\u51fa\u4e86\u95ee\u9898\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Spring Data JPA n&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/108193"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=108193"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/108193\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=108193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=108193"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=108193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}