{"id":8540,"date":"2022-11-14T21:58:40","date_gmt":"2022-11-14T13:58:40","guid":{"rendered":"http:\/\/123.57.164.21\/?p=8540"},"modified":"2022-11-14T21:58:40","modified_gmt":"2022-11-14T13:58:40","slug":"spring-data-jpa-%e5%85%a5%e9%97%a8","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=8540","title":{"rendered":"Spring Data JPA \u5165\u95e8"},"content":{"rendered":"\n<p><strong>ORM<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5728\u89e3\u91ca\u4ec0\u4e48\u662fJPA\u548cSpring Data JPA\u4e4b\u524d\u6211\u4eec\u5e94\u8be5\u5148\u6765\u4e86\u89e3\u4e00\u4e0b\u4ec0\u4e48\u662fORM\u3002<strong>ORM(Object-Relational Mapping, \u5bf9\u8c61\u5173\u7cfb\u6620\u5c04)\uff0c\u662f\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u5bf9\u8c61\u548c\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u4e4b\u95f4\u7684\u6620\u5c04<\/strong>\u3002\u4f7f\u7528ORM\u5de5\u5177\u3001\u6846\u67b6\u53ef\u4ee5\u8ba9\u5e94\u7528\u7a0b\u5e8f\u64cd\u4f5c\u6570\u636e\u5e93\u3002<\/p>\n\n\n\n<p>\u5728\u8fc7\u53bb\uff0c\u6709\u5f88\u591a\u9488\u5bf9Java\u7684ORM\u6846\u67b6\uff0c\u4f46\u662f\u6bcf\u4e00\u5957\u6846\u67b6\u90fd\u6709\u81ea\u5df1\u7684\u4e00\u5957\u64cd\u4f5c\u65b9\u6cd5\u548c\u89c4\u8303\uff0c\u8fd9\u5c31\u4f7f\u5f97Java\u7a0b\u5e8f\u64cd\u4f5c\u4e0d\u540c\u6570\u636e\u5e93\u65f6\u663e\u5f97\u6742\u4e71\u65e0\u7ae0\u3002\u4e8e\u662f\u4e4e\uff0cSun\u516c\u53f8\u63a8\u51fa\u4e86\u4e00\u5957\u64cd\u4f5c\u6301\u4e45\u5c42\uff08\u6570\u636e\u5e93\uff09\u7684\u89c4\u8303(API)\u7528\u4e8e\u7ed3\u675f\u8fd9\u79cd\u4e71\u8c61\uff0c\u8fd9\u5957\u89c4\u8303\u4e5f\u5c31\u662f<strong>JPA<\/strong>\u3002<\/p>\n\n\n\n<p><strong>JPA<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>JPA(Java Persistence API\uff0cJava\u6301\u4e45\u5c42API) \u662fSun\u516c\u53f8\u5b9a\u4e49\u7684\u4e00\u5957<strong>\u57fa\u4e8eORM\u7684\u63a5\u53e3\u89c4\u8303<\/strong>\uff0c\u7528\u4e8e\u7ed9Java\u7a0b\u5e8f\u64cd\u4f5c\u6570\u636e\u5e93\u3002JPA \u901a\u8fc7 JDK 5.0 \u6ce8\u89e3\u63cf\u8ff0\u5bf9\u8c61\uff0d\u5173\u7cfb\u8868\u7684\u6620\u5c04\u5173\u7cfb\uff0c\u5e76\u5c06\u8fd0\u884c\u671f\u7684\u5b9e\u4f53\u5bf9\u8c61\u6301\u4e45\u5316\u5230\u6570\u636e\u5e93\u4e2d \u3002\u5728\u8fd9\u4e4b\u540e\u5f88\u591aORM\u6846\u67b6\u5b9e\u73b0\u4e86JPA\u89c4\u8303\uff0c\u5176\u4e2d\u6700\u6709\u540d\u7684\u6709<strong>Hibernate<\/strong>\u3001TopLink\u3001JDO\u7b49\u3002JPA \u548c Hibernate \u7684\u5173\u7cfb\u5c31\u50cf JDBC \u548c JDBC \u9a71\u52a8\u7684\u5173\u7cfb\uff0cJPA \u662f\u89c4\u8303\uff0cHibernate \u9664\u4e86\u4f5c\u4e3a ORM \u6846\u67b6\u4e4b\u5916\uff0c\u5b83\u4e5f\u662f\u4e00\u79cd JPA \u5b9e\u73b0\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-214-1024x507.png\" alt=\"\" class=\"wp-image-8541\" width=\"488\" height=\"241\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-214-1024x507.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-214-300x149.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-214-768x381.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-214-830x411.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-214-230x114.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-214-350x173.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-214-480x238.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-214.png 1324w\" sizes=\"(max-width: 488px) 100vw, 488px\" \/><\/figure><\/div>\n\n\n\n<p><strong>JPA\u7684\u4f18\u52bf<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ul><li>\u6807\u51c6\u5316<\/li><\/ul>\n\n\n\n<p>JPA \u662f JCP \u7ec4\u7ec7\u53d1\u5e03\u7684 Java EE \u6807\u51c6\u4e4b\u4e00\uff0c\u56e0\u6b64\u4efb\u4f55\u58f0\u79f0\u7b26\u5408 JPA \u6807\u51c6\u7684\u6846\u67b6\u90fd\u9075\u5faa\u540c<br>\u6837\u7684\u67b6\u6784\uff0c\u63d0\u4f9b\u76f8\u540c\u7684\u8bbf\u95eeAPI\uff0c\u8fd9\u4fdd\u8bc1\u4e86\u57fa\u4e8eJPA\u5f00\u53d1\u7684\u4f01\u4e1a\u5e94\u7528\u80fd\u591f\u7ecf\u8fc7\u5c11\u91cf\u7684\u4fee\u6539\u5c31\u80fd\u591f\u5728 \u4e0d\u540c\u7684 JPA \u6846\u67b6\u4e0b\u8fd0\u884c\u3002<\/p>\n\n\n\n<ul><li>\u5bb9\u5668\u7ea7\u7279\u6027\u7684\u652f\u6301<\/li><\/ul>\n\n\n\n<p>JPA \u6846\u67b6\u4e2d\u652f\u6301\u5927\u6570\u636e\u96c6\u3001\u4e8b\u52a1\u3001\u5e76\u53d1\u7b49\u5bb9\u5668\u7ea7\u4e8b\u52a1\uff0c\u8fd9\u4f7f\u5f97 JPA \u8d85\u8d8a\u4e86\u7b80\u5355\u6301\u4e45\u5316\u6846\u67b6\u7684 \u5c40\u9650\uff0c\u5728\u4f01\u4e1a\u5e94\u7528\u53d1\u6325\u66f4\u5927\u7684\u4f5c\u7528\u3002<\/p>\n\n\n\n<ul><li>\u7b80\u5355\u65b9\u4fbf<\/li><\/ul>\n\n\n\n<p>JPA \u7684\u4e3b\u8981\u76ee\u6807\u4e4b\u4e00\u5c31\u662f\u63d0\u4f9b\u66f4\u52a0\u7b80\u5355\u7684\u7f16\u7a0b\u6a21\u578b\uff1a\u5728 JPA \u6846\u67b6\u4e0b\u521b\u5efa\u5b9e\u4f53\u548c\u521b\u5efa Java \u7c7b\u4e00\u6837\u7b80\u5355\uff0c\u6ca1\u6709\u4efb\u4f55\u7684\u7ea6\u675f\u548c\u9650\u5236\uff0c\u53ea\u9700\u8981\u4f7f\u7528 javax.persistence.Entity \u8fdb\u884c\u6ce8\u91ca\uff0cJPA\u7684 \u6846\u67b6\u548c\u63a5\u53e3\u4e5f\u90fd\u975e\u5e38\u7b80\u5355\uff0c\u6ca1\u6709\u592a\u591a\u7279\u522b\u7684\u89c4\u5219\u548c\u8bbe\u8ba1\u6a21\u5f0f\u7684\u8981\u6c42\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u638c\u63e1\u3002 JPA \u57fa\u4e8e\u975e\u4fb5\u5165\u5f0f\u539f\u5219\u8bbe\u8ba1\uff0c\u56e0\u6b64\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u548c\u5176\u5b83\u6846\u67b6\u6216\u8005\u5bb9\u5668\u96c6\u6210<\/p>\n\n\n\n<ul><li>\u67e5\u8be2\u80fd\u529b<\/li><\/ul>\n\n\n\n<p>JPA \u7684\u67e5\u8be2\u8bed\u8a00\u662f\u9762\u5411\u5bf9\u8c61\u800c\u975e\u9762\u5411\u6570\u636e\u5e93\u7684\uff0c\u5b83\u4ee5\u9762\u5411\u5bf9\u8c61\u7684\u81ea\u7136\u8bed\u6cd5\u6784\u9020\u67e5\u8be2\u8bed\u53e5\uff0c\u53ef\u4ee5\u770b\u6210\u662f Hibernate HQL \u7684\u7b49\u4ef7\u7269\u3002JPA \u5b9a\u4e49\u4e86\u72ec\u7279\u7684 JPQL\uff08Java Persistence Query Language\uff09\uff0cJPQL \u662f EJB QL \u7684\u4e00\u79cd\u6269\u5c55\uff0c\u5b83\u662f\u9488\u5bf9\u5b9e\u4f53\u7684\u4e00\u79cd\u67e5\u8be2\u8bed\u8a00\uff0c\u64cd\u4f5c\u5bf9\u8c61\u662f\u5b9e\u4f53\uff0c\u800c \u4e0d\u662f\u5173\u7cfb\u6570\u636e\u5e93\u7684\u8868\uff0c\u800c\u4e14\u80fd\u591f\u652f\u6301\u6279\u91cf\u66f4\u65b0\u548c\u4fee\u6539\u3001JOIN\u3001GROUP BY\u3001HAVING \u7b49\u901a\u5e38\u53ea\u6709 SQL \u624d\u80fd\u591f\u63d0\u4f9b\u7684\u9ad8\u7ea7\u67e5\u8be2\u7279\u6027\uff0c\u751a\u81f3\u8fd8\u80fd\u591f\u652f\u6301\u5b50\u67e5\u8be2<\/p>\n\n\n\n<ul><li>\u9ad8\u7ea7\u7279\u6027<\/li><\/ul>\n\n\n\n<p>JPA \u4e2d\u80fd\u591f\u652f\u6301\u9762\u5411\u5bf9\u8c61\u7684\u9ad8\u7ea7\u7279\u6027\uff0c\u5982\u7c7b\u4e4b\u95f4\u7684\u7ee7\u627f\u3001\u591a\u6001\u548c\u7c7b\u4e4b\u95f4\u7684\u590d\u6742\u5173\u7cfb\u3002\u8fd9\u6837\u7684\u652f\u6301\u80fd\u591f\u8ba9\u5f00\u53d1\u8005\u6700\u5927\u9650\u5ea6\u7684\u4f7f\u7528\u9762\u5411\u5bf9\u8c61\u7684\u6a21\u578b\u8bbe\u8ba1\u4f01\u4e1a\u5e94\u7528\uff0c\u800c\u4e0d\u9700\u8981\u81ea\u884c\u5904\u7406\u8fd9\u4e9b\u7279\u6027\u3002<br>Spring Data JPA \u662f Spring \u57fa\u4e8e ORM \u6846\u67b6\u3001JPA \u89c4\u8303\u7684\u57fa\u7840\u4e0a\u5c01\u88c5\u7684\u4e00\u5957 JPA \u5e94\u7528\u6846\u67b6\uff0c\u53ef\u4f7f\u5f00\u53d1\u8005\u7528\u6781\u7b80\u7684\u4ee3\u7801\u5373\u53ef\u5b9e\u73b0\u5bf9\u6570\u636e\u5e93\u7684\u8bbf\u95ee\u548c\u64cd\u4f5c\u3002\u5b83\u63d0\u4f9b\u4e86\u5305\u62ec\u589e\u5220\u6539\u67e5\u7b49\u5728\u5185\u7684\u5e38\u7528\u529f\u80fd\uff0c\u4e14\u6613\u4e8e\u6269\u5c55\uff01\u5b66\u4e60\u5e76\u4f7f\u7528 Spring Data JPA \u53ef\u4ee5\u6781\u5927\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\uff01<br>Spring Data JPA \u8ba9\u6211\u4eec\u89e3\u8131\u4e86 DAO \u5c42\u7684\u64cd\u4f5c\uff0c\u57fa\u672c\u4e0a\u6240\u6709 CRUD \u90fd\u53ef\u4ee5\u4f9d\u8d56\u4e8e\u5b83\u6765\u5b9e\u73b0,\u5728\u5b9e\u9645\u7684\u5de5\u4f5c\u5de5\u7a0b\u4e2d\uff0c\u63a8\u8350\u4f7f\u7528 Spring Data JPA + ORM\uff08\u5982\uff1ahibernate\uff09\u5b8c\u6210\u64cd\u4f5c\uff0c\u8fd9\u6837\u5728\u5207\u6362\u4e0d\u540c\u7684 ORM \u6846\u67b6\u65f6\u63d0\u4f9b\u4e86\u6781\u5927\u7684\u65b9\u4fbf\uff0c\u540c\u65f6\u4e5f\u4f7f\u6570\u636e\u5e93\u5c42\u64cd\u4f5c\u66f4\u52a0\u7b80\u5355\uff0c\u65b9\u4fbf\u3002<\/p>\n\n\n\n<p><strong>Spring Data JPA<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Spring Data JPA \u662f Spring \u57fa\u4e8e ORM \u6846\u67b6\u3001JPA \u89c4\u8303\u7684\u57fa\u7840\u4e0a\u5c01\u88c5\u7684\u4e00\u5957 JPA \u5e94\u7528\u6846\u67b6\uff0c\u53ef\u4f7f\u5f00\u53d1\u8005\u7528\u6781\u7b80\u7684\u4ee3\u7801\u5373\u53ef\u5b9e\u73b0\u5bf9\u6570\u636e\u5e93\u7684\u8bbf\u95ee\u548c\u64cd\u4f5c\u3002\u5b83\u63d0\u4f9b\u4e86\u5305\u62ec\u589e\u5220\u6539\u67e5\u7b49\u5728\u5185\u7684\u5e38\u7528\u529f\u80fd\uff0c\u4e14\u6613\u4e8e\u6269\u5c55\uff01\u5b66\u4e60\u5e76\u4f7f\u7528 Spring Data JPA \u53ef\u4ee5\u6781\u5927\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\uff01<\/p>\n\n\n\n<p>Spring Data JPA \u8ba9\u6211\u4eec\u89e3\u8131\u4e86 DAO \u5c42\u7684\u64cd\u4f5c\uff0c\u57fa\u672c\u4e0a\u6240\u6709 CRUD \u90fd\u53ef\u4ee5\u4f9d\u8d56\u4e8e\u5b83\u6765\u5b9e\u73b0,\u5728\u5b9e\u9645\u7684 \u5de5\u4f5c\u5de5\u7a0b\u4e2d\uff0c\u63a8\u8350\u4f7f\u7528 Spring Data JPA + ORM\uff08\u5982\uff1ahibernate\uff09\u5b8c\u6210\u64cd\u4f5c\uff0c\u8fd9\u6837\u5728\u5207\u6362\u4e0d\u540c\u7684 ORM \u6846\u67b6\u65f6\u63d0\u4f9b\u4e86\u6781\u5927\u7684\u65b9\u4fbf\uff0c\u540c\u65f6\u4e5f\u4f7f\u6570\u636e\u5e93\u5c42\u64cd\u4f5c\u66f4\u52a0\u7b80\u5355\uff0c\u65b9\u4fbf\u89e3\u8026\u3002<\/p>\n\n\n\n<p><strong>\u7b2c\u4e00\u4e2aSpring Data JPA\u9879\u76ee<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>\u6b65\u9aa4\u5982\u4e0b<\/strong><\/p>\n\n\n\n<ul><li>\u5728pom.xml\u4e2d\u5bfc\u5165\u4f9d\u8d56<\/li><\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    &lt;dependencies>\n        &lt;dependency>\n            &lt;groupId>org.springframework.boot&lt;\/groupId>\n            &lt;artifactId>spring-boot-starter-data-jpa&lt;\/artifactId>\n        &lt;\/dependency>\n        &lt;dependency>\n            &lt;groupId>org.springframework.boot&lt;\/groupId>\n            &lt;artifactId>spring-boot-starter-jdbc&lt;\/artifactId>\n        &lt;\/dependency>\n        &lt;dependency>\n            &lt;groupId>org.springframework.boot&lt;\/groupId>\n            &lt;artifactId>spring-boot-starter-web&lt;\/artifactId>\n        &lt;\/dependency>\n\n        &lt;dependency>\n            &lt;groupId>mysql&lt;\/groupId>\n            &lt;artifactId>mysql-connector-java&lt;\/artifactId>\n            &lt;scope>runtime&lt;\/scope>\n        &lt;\/dependency>\n        &lt;dependency>\n            &lt;groupId>org.projectlombok&lt;\/groupId>\n            &lt;artifactId>lombok&lt;\/artifactId>\n            &lt;optional>true&lt;\/optional>\n        &lt;\/dependency>\n        &lt;dependency>\n            &lt;groupId>org.springframework.boot&lt;\/groupId>\n            &lt;artifactId>spring-boot-starter-test&lt;\/artifactId>\n            &lt;scope>test&lt;\/scope>\n        &lt;\/dependency>\n    &lt;\/dependencies>\n<\/pre>\n\n\n\n<ul><li>\u914d\u7f6eapplication.yml<\/li><\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">spring:\n  datasource:\n    driver-class-name: com.mysql.cj.jdbc.Driver\n    url: jdbc:mysql:\/\/localhost:3306\/jpa_study?useUnicode=true&amp;characterEncoding=utf8\n    username: root\n    password: 123456\n  data:\n    rest:\n      basePath: \/api\n  jpa:\n    hibernate:\n      ddl-auto: update\n    show-sql: true\n\nserver:\n  port: 8080\n<\/pre>\n\n\n\n<ul><li>\u7f16\u5199\u5b9e\u4f53\u7c7b<\/li><\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package com.soul.jpastudy.domain;\n\nimport lombok.AllArgsConstructor;\nimport lombok.Data;\nimport lombok.NoArgsConstructor;\n\nimport javax.persistence.*;\n\n@Data\n@NoArgsConstructor\n@AllArgsConstructor\n\n\/**\n * \u5ba2\u6237\u7684\u5b9e\u4f53\u7c7b\n *      \u914d\u7f6e\u6620\u5c04\u5173\u7cfb\n *          1. \u5b9e\u4f53\u7c7b\u548c\u8868\u7684\u6620\u5c04\u5173\u7cfb\n *          2. \u5b9e\u4f53\u7c7b\u4e2d\u5c5e\u6027\u548c\u8868\u4e2d\u5b57\u6bb5\u7684\u6620\u5c04\u5173\u7cfb\n *  @Entity:\u58f0\u660e\u5b9e\u4f53\u7c7b\n *  @Table: \u914d\u7f6e\u5b9e\u4f53\u7c7b\u548c\u8868\u7684\u6620\u5c04\u5173\u7cfb\n *      name: \u914d\u7f6e\u6570\u636e\u5e93\u8868\u7684\u540d\u79f0\n *\/\n\n@Entity\n@Table(name = \"cst_customer\")\npublic class Customer {\n\n    @Id \/\/ \u58f0\u660e\u4e3b\u952e\u7684\u914d\u7f6e\n    @GeneratedValue(strategy = GenerationType.IDENTITY) \/\/ \u81ea\u589e\n    @Column(name = \"cust_id\")   \/\/ \u6620\u5c04\u6570\u636e\u5e93\u8868\u4e2d\u7684\u5b57\u6bb5\n    private Long custId;\n\n    @Column(name = \"cust_name\")\n    private String custName;\n\n    @Column(name = \"cust_source\")\n    private String custSource;\n\n    @Column(name = \"cust_level\")\n    private String custLevel;\n\n    @Column(name = \"cust_industry\")\n    private String custIndustry;\n\n    @Column(name = \"cust_phone\")\n    private String custPhone;\n\n    @Column(name = \"cust_address\")\n    private String custAddress;\n\n}\n<\/pre>\n\n\n\n<ul><li>\u7f16\u5199Repository<\/li><\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package com.soul.jpastudy.repository;\n\nimport com.soul.jpastudy.domain.Customer;\nimport org.springframework.data.jpa.repository.JpaRepository;\nimport org.springframework.data.jpa.repository.Modifying;\nimport org.springframework.data.jpa.repository.Query;\nimport org.springframework.data.repository.query.Param;\nimport org.springframework.data.rest.core.annotation.RepositoryRestResource;\nimport org.springframework.transaction.annotation.Transactional;\n\nimport java.util.List;\n\n@RepositoryRestResource(collectionResourceRel = \"customers\", path = \"customers\")\npublic interface CustomerRepository extends JpaRepository&lt;Customer, Long>{\n\n    Customer findByCustName(String custName);\n\n    @Query(\"select c from Customer c\")\n    List&lt;Customer> findAll();\n\n    @Transactional\n    @Modifying\n    @Query(\"update Customer c set c.custName = ?1 where c.custId = ?2\")\n    int updateCustomer(@Param(\"custName\") String custName, @Param(\"custId\") Long custId);\n\n    @Transactional\n    @Modifying\n    @Query(\"delete from Customer c where c.custId = :custId and c.custIndustry = :custIndustry\")\n    int deleteCustomer(@Param(\"custId\") Long custId, @Param(\"custIndustry\") String custIndustry);\n\n}\n\n<\/pre>\n\n\n\n<ul><li>\u5355\u5143\u6d4b\u8bd5<\/li><\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@SpringBootTest\nclass JpaStudyApplicationTests {\n\n    @Autowired\n    private CustomerRepository customerRepository;\n\n    @Test\n    void testFindByCustName(){\n        Customer customer = customerRepository.findByCustName(\"testName\");\n        System.out.println(customer);\n    }\n\n    @Test\n    void testFindAll() {\n        List&lt;Customer> all = customerRepository.findAll();\n        for (Customer customer : all) {\n            System.out.println(customer);\n        }\n    }\n\n    @Test\n    void testUpdateCustomer() {\n        int row = customerRepository.updateCustomer(\"testUpdate\", 1L);\n        System.out.println(row);\n        testFindAll();\n    }\n\n    @Test\n    void testDeleteCustomer() {\n        int row = customerRepository.deleteCustomer(1L, \"testIndustry\");\n        System.out.println(row);\n        testFindAll();\n    }\n\n    @Test\n    void testSaveCustomer() {\n        Customer customer = new Customer();\n        customer.setCustName(\"save test\");\n        customer.setCustLevel(\"save level\");\n\/\/        customer.setCustId(1L);   \/\/ \u52a0\u4e0a\u662f\u66f4\u65b0\u67d0\u67d0\uff0c\u4e0d\u52a0\u662f\u6dfb\u52a0\u65b0\u7684\u4e00\u884c\u6570\u636e\n        customerRepository.save(customer);\n        testFindAll();\n    }\n<\/pre>\n\n\n\n<p><strong>JPQL<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u57fa\u4e8e\u9996\u6b21\u5728 EJB2.0 \u4e2d\u5f15\u5165\u7684 EJB \u67e5\u8be2\u8bed\u8a00(EJB QL),Java <strong>JPQL(Java Persistence Query Language)\u662f\u4e00\u79cd\u53ef\u79fb\u690d\u7684\u67e5\u8be2\u8bed\u8a00<\/strong>\uff0c\u65e8\u5728\u4ee5\u9762\u5411\u5bf9\u8c61\u8868\u8fbe\u5f0f\u8bed\u8a00\u7684\u8868\u8fbe\u5f0f\uff0c\u5c06 SQL \u8bed\u6cd5\u548c\u7b80\u5355\u67e5\u8be2\u8bed\u4e49\u7ed1\u5b9a\u5728\u4e00\u8d77\u4f7f\u7528\u8fd9\u79cd\u8bed\u8a00\u7f16\u5199\u7684\u67e5\u8be2\u662f\u53ef\u79fb\u690d\u7684\uff0c\u53ef\u4ee5\u88ab\u7f16\u8bd1\u6210\u6240\u6709\u4e3b\u6d41\u6570\u636e\u5e93\u670d\u52a1\u5668\u4e0a\u7684 SQL\u3002<\/p>\n\n\n\n<p>\u5176\u7279\u5f81\u4e0e\u539f\u751fSQL\u8bed\u53e5\u7c7b\u4f3c\uff0c\u5e76\u4e14\u5b8c\u5168\u9762\u5411\u5bf9\u8c61\uff0c\u901a\u8fc7\u7c7b\u540d\u548c\u5c5e\u6027\u8bbf\u95ee\uff0c\u800c\u4e0d\u662f\u8868\u540d\u548c\u8868\u7684\u5c5e\u6027\u3002<\/p>\n\n\n\n<p><strong>\u57fa\u672c\u8bed\u6cd5<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">select \u5b9e\u4f53\u522b\u540d.\u5c5e\u6027\u540d\uff0c\nfrom \u5b9e\u4f53\u540d as \u5b9e\u4f53\u522b\u540d \nwhere \u5b9e\u4f53\u522b\u540d.\u5b9e\u4f53\u5c5e\u6027 op \u6bd4\u8f83\u503c\n\n# example\nupdate Customer c set c.custName = ?1 where c.custId = ?2 # ?\u540e\u8ddf\u53d8\u91cf\u5e8f\u53f7\uff08\u8be6\u89c1\u7b2c\u4e00\u4e2aSpring Data JPA\u9879\u76ee\uff09\ndelete from Customer c where c.custId = :custId and c.custIndustry = :custIndustry # :\u540e\u8ddf\u53d8\u91cf\u540d\uff08\u8be6\u89c1\u7b2c\u4e00\u4e2aSpring Data JPA\u9879\u76ee\uff09\n\n<\/pre>\n\n\n\n<p><strong>\u52a8\u6001\u67e5\u8be2<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5728Spring Data JPA\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u533f\u540d\u5185\u90e8\u7c7b\u7684\u65b9\u5f0f\u6dfb\u52a0\u52a8\u6001\u6761\u4ef6\uff0c\u5b9e\u73b0\u52a8\u6001\u67e5\u8be2\u3002\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-215.png\" alt=\"\" class=\"wp-image-8542\" width=\"402\" height=\"177\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-215.png 918w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-215-300x133.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-215-768x340.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-215-830x367.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-215-230x102.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-215-350x155.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-215-480x212.png 480w\" sizes=\"(max-width: 402px) 100vw, 402px\" \/><\/figure><\/div>\n\n\n\n<p>\u793a\u4f8b\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    @Test\n    public void testDynamicQuery() {\n        List&lt;User> users = userRepository.findAll(new Specification&lt;User>() {\n            @Override\n            public Predicate toPredicate(Root&lt;User> root, CriteriaQuery&lt;?> query, CriteriaBuilder criteriaBuilder) {\n                Predicate predicate = null;\n\n                Path&lt;String> pwdPath = root.get(\"pwd\");\n                Predicate pwdPredicate = criteriaBuilder.equal(pwdPath, \"123\");\n                predicate = criteriaBuilder.and(predicate, pwdPredicate);\n                Path&lt;String> namePath = root.get(\"name\");\n                Predicate namePredicate = criteriaBuilder.like(namePath, \"%Jack%\");\n                predicate = criteriaBuilder.and(pwdPredicate, namePredicate);\n                Path&lt;Integer> idPath = root.get(\"id\");\n                Predicate idPredicate = criteriaBuilder.equal(idPath, 2);\n                predicate = criteriaBuilder.and(predicate, idPredicate);\n                return predicate;\n            }\n        });\n        \n        \/\/ print results\n        for (User user : users) {\n            System.out.println(user);\n        }\n    }\n<\/pre>\n\n\n\n<p><strong>Spring Data JPA \u5bf9\u6bd4 MyBatis<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ul><li>\u8868\u5173\u8054\u8f83\u591a\u7684\u9879\u76ee\uff0c\u4f18\u5148\u4f7f\u7528mybatis<\/li><li>\u6301\u7eed\u7ef4\u62a4\u5f00\u53d1\u8fed\u4ee3\u8f83\u5feb\u7684\u9879\u76ee\u5efa\u8bae\u4f7f\u7528mybatis\uff0c\u56e0\u4e3a\u4e00\u822c\u8fd9\u79cd\u9879\u76ee\u9700\u8981\u53d8\u5316\u5f88\u7075\u6d3b\uff0c\u5bf9sql\u7684\u7075\u6d3b\u4fee\u6539\u8981\u6c42\u8f83\u9ad8<\/li><li>\u5bf9\u4e8e\u4f20\u7edf\u9879\u76ee\u6216\u8005\u5173\u7cfb\u6a21\u578b\u8f83\u4e3a\u6e05\u6670\u7a33\u5b9a\u7684\u9879\u76ee\uff0c\u5efa\u8baeJPA\uff08\u6bd4\u5982DDD\u8bbe\u8ba1\u4e2d\u7684\u9886\u57df\u5c42\uff09<\/li><li>\u76ee\u524d\u5fae\u670d\u52a1\u6bd4\u8f83\u706b\uff0c\u57fa\u4e8e\u5176\u804c\u8d23\u7684\u72ec\u7acb\u6027\uff0c\u5982\u679c\u6a21\u578b\u6e05\u6670\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528JPA\uff0c\u4f46\u5982\u679c\u6570\u636e\u91cf\u8f83\u5927\u5168\u5b57\u6bb5\u8fd4\u56de\u6570\u636e\u91cf\u5927\u7684\u8bdd\u53ef\u80fd\u5728\u6027\u80fd\u6709\u5f71\u54cd\uff0c\u9700\u8981\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u5206\u6790<\/li><\/ul>\n\n\n\n<p>Spring Data JPA \u662fSpring Data\u4e2d\u4e00\u6b3e\u5f3a\u5927\u7684\u7528\u4e8e\u64cd\u4f5c\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7684\u6280\u672f\u3002\u5b83\u7684\u51fa\u73b0\uff0c\u7701\u53bb\u4e86\u6211\u4eec\u7f16\u5199Dao\u5c42\u7684\u6b65\u9aa4\u3002\u5728\u4e1a\u52a1\u903b\u8f91\u76f8\u5bf9\u7b80\u5355\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u5b83\u4f1a\u6781\u5927\u7684\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002\u4f46\u662f\u5f53\u4e1a\u52a1\u903b\u8f91\u592a\u8fc7\u590d\u6742\u65f6\uff0cSpring Data JPA\u7f3a\u4e4f\u7075\u6d3b\u6027\u7684\u95ee\u9898\u5c31\u4f1a\u66b4\u9732\u51fa\u6765\uff0c\u8fd9\u4e2a\u65f6\u5019MyBatis\u53cd\u800c\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>ORM \u5728\u89e3\u91ca\u4ec0\u4e48\u662fJPA\u548cSpring Data JPA\u4e4b\u524d\u6211\u4eec\u5e94\u8be5\u5148\u6765\u4e86\u89e3\u4e00\u4e0b\u4ec0\u4e48\u662fORM\u3002ORM(Obj [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,27],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/8540"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8540"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/8540\/revisions"}],"predecessor-version":[{"id":8544,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/8540\/revisions\/8544"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}