{"id":591,"date":"2023-03-23T21:28:47","date_gmt":"2023-03-23T13:28:47","guid":{"rendered":""},"modified":"2023-03-23T21:28:47","modified_gmt":"2023-03-23T13:28:47","slug":"Spring Boot JPA","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/591.html","title":{"rendered":"Spring Boot JPA"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Spring Boot JPA<\/h1>\n<\/p><\/div>\n

\u4ec0\u4e48\u662fJPA\uff1f<\/h2>\n
\n Spring Boot JPA <\/strong>\u662f\u7528\u4e8e\u7ba1\u7406\u7684Java\u89c4\u8303Java\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684
\n \u5173\u7cfb<\/strong>\u6570\u636e\u3002\u5b83\u5141\u8bb8\u6211\u4eec\u8bbf\u95ee\u548c\u6301\u4e45\u5316Java\u5bf9\u8c61\/\u7c7b\u4e0e\u5173\u7cfb\u6570\u636e\u5e93\u4e4b\u95f4\u7684\u6570\u636e\u3002 JPA\u9075\u5faa
\n \u5bf9\u8c61\u5173\u7cfb\u6620\u5c04<\/strong>(ORM)\u3002\u5b83\u662f\u4e00\u7ec4\u63a5\u53e3\u3002\u5b83\u8fd8\u63d0\u4f9b\u4e86\u8fd0\u884c\u65f6
\n EntityManager <\/strong> API\uff0c\u7528\u4e8e\u5904\u7406\u9488\u5bf9\u6570\u636e\u5e93\u5bf9\u8c61\u7684\u67e5\u8be2\u548c\u4e8b\u52a1\u3002\u5b83\u4f7f\u7528\u4e0e\u5e73\u53f0\u65e0\u5173\u7684\u9762\u5411\u5bf9\u8c61\u7684\u67e5\u8be2\u8bed\u8a00JPQL(Java\u6301\u4e45\u67e5\u8be2\u8bed\u8a00)\u3002\n <\/div>\n
\n \u5728\u6301\u4e45\u6027\u65b9\u9762\uff0c\u5b83\u6db5\u76d6\u4e86\u4e09\u4e2a\u9886\u57df:\n <\/div>\n

Java\u6301\u4e45\u6027API <\/span>
\n Object-Relational<\/strong>\u5143\u6570\u636e<\/span>
\n \u5728 persistence <\/strong>\u5305\u4e2d\u5b9a\u4e49\u7684API\u672c\u8eab<\/span> <\/p>\n

\n JPA\u4e0d\u662f\u6846\u67b6\u3002\u5b83\u5b9a\u4e49\u4e86\u53ef\u4ee5\u5728\u4efb\u4f55\u6846\u67b6\u4e2d\u5b9e\u73b0\u7684\u6982\u5ff5\u3002\n <\/div>\n

\u4e3a\u4ec0\u4e48\u6211\u4eec\u5e94\u8be5\u4f7f\u7528JPA\uff1f<\/h2>\n
\n \u4e0eJDBC\u76f8\u6bd4\uff0cJPA\u66f4\u7b80\u5355\uff0c\u66f4\u6e05\u6d01\u4e14\u52b3\u52a8\u5f3a\u5ea6\u66f4\u4f4e\uff0cSQL\u548c\u624b\u5199\u6620\u5c04\u3002 JPA\u9002\u7528\u4e8e\u975e\u6027\u80fd\u5bfc\u5411\u7684\u590d\u6742\u5e94\u7528\u7a0b\u5e8f\u3002\u4e0eJDBC\u76f8\u6bd4\uff0cJPA\u7684\u4e3b\u8981\u4f18\u70b9\u5728\u4e8e\uff0c\u5728JPA\u4e2d\uff0c\u6570\u636e\u7531\u5bf9\u8c61\u548c\u7c7b\u8868\u793a\uff0c\u800c\u5728JDBC\u4e2d\uff0c\u6570\u636e\u7531\u8868\u548c\u8bb0\u5f55\u8868\u793a\u3002\u5b83\u4f7f\u7528POJO\u8868\u793a\u6301\u4e45\u6570\u636e\uff0c\u4ece\u800c\u7b80\u5316\u4e86\u6570\u636e\u5e93\u7f16\u7a0b\u3002 JPA\u8fd8\u6709\u5176\u4ed6\u4f18\u70b9:\n <\/div>\n

JPA\u907f\u514d\u4f7f\u7528SQL\u7684\u7279\u5b9a\u4e8e\u6570\u636e\u5e93\u7684\u65b9\u8a00\u7f16\u5199DDL\u3002\u53d6\u800c\u4ee3\u4e4b\u7684\u662f\uff0c\u5b83\u5141\u8bb8\u4ee5XML\u6216\u4f7f\u7528Java\u6ce8\u89e3\u8fdb\u884c\u6620\u5c04\u3002<\/span>
\n JPA\u5141\u8bb8\u6211\u4eec\u907f\u514d\u7528SQL\u7684\u6570\u636e\u5e93\u4e13\u7528\u65b9\u8a00\u7f16\u5199DML\u3002<\/span>
\n JPA\u5141\u8bb8\u6211\u4eec\u5b8c\u5168\u4e0d\u4f7f\u7528\u4efb\u4f55DML\u8bed\u8a00\u6765\u4fdd\u5b58\u548c\u52a0\u8f7dJava\u5bf9\u8c61\u548c\u56fe\u5f62\u3002<\/span>
\n \u5f53\u6211\u4eec\u9700\u8981\u6267\u884c\u67e5\u8be2JPQL\u65f6\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u7528Java\u5b9e\u4f53\u800c\u4e0d\u662f(\u672c\u673a)SQL\u8868\u548c\u5217\u6765\u8868\u8fbe\u67e5\u8be2\u3002<\/span> <\/p>\n

JPA\u529f\u80fd<\/h2>\n
\n JPA\u5177\u6709\u4ee5\u4e0b\u529f\u80fd:\n <\/div>\n

\u8fd9\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u5b58\u50a8\u5e93\u548c\u81ea\u5b9a\u4e49\u7684\u5bf9\u8c61\u6620\u5c04\u62bd\u8c61\u3002<\/strong> <\/span>
\n \u5b83\u652f\u6301\u8de8\u5546\u5e97\u6301\u4e45\u6027<\/strong>\u3002\u8fd9\u610f\u5473\u7740\u4e00\u4e2a\u5b9e\u4f53\u53ef\u4ee5\u90e8\u5206\u5b58\u50a8\u5728MySQL\u548cNeo4j(\u56fe\u5f62\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf)\u4e2d\u3002<\/span>
\n \u5b83\u4ece\u67e5\u8be2\u65b9\u6cd5\u540d\u79f0\u52a8\u6001\u751f\u6210\u67e5\u8be2\u3002<\/span>
\n \u57df\u57fa\u7c7b\u63d0\u4f9b\u57fa\u672c\u5c5e\u6027\u3002<\/span>
\n \u5b83\u652f\u6301\u900f\u660e\u5ba1\u6838\u3002<\/span>
\n \u96c6\u6210\u81ea\u5b9a\u4e49\u5b58\u50a8\u5e93\u4ee3\u7801\u7684\u53ef\u80fd\u6027\u3002<\/span>
\n \u901a\u8fc7\u81ea\u5b9a\u4e49\u540d\u79f0\u7a7a\u95f4\u5f88\u5bb9\u6613\u4e0eSpring Framework\u96c6\u6210\u3002<\/span> <\/p>\n

JPA\u4f53\u7cfb\u7ed3\u6784<\/h2>\n
\n JPA\u662f\u5c06\u4e1a\u52a1\u5b9e\u4f53\u5b58\u50a8\u4e3a\u5173\u7cfb\u5b9e\u4f53\u7684\u6e90\u3002\u5b83\u663e\u793a\u4e86\u5982\u4f55\u5c06POJO\u5b9a\u4e49\u4e3a\u5b9e\u4f53\u4ee5\u53ca\u5982\u4f55\u901a\u8fc7\u5173\u7cfb\u7ba1\u7406\u5b9e\u4f53\u3002\n <\/div>\n
\n \u4e0b\u56fe\u63cf\u8ff0\u4e86JPA\u7684\u7c7b\u7ea7\u522b\u4f53\u7cfb\u7ed3\u6784\uff0c\u8be5\u4f53\u7cfb\u7ed3\u6784\u63cf\u8ff0\u4e86JPA\u7684\u6838\u5fc3\u7c7b\u548c\u63a5\u53e3\u3002
\n javax\u6301\u4e45\u6027<\/strong>\u5305\u3002 JPA\u4f53\u7cfb\u7ed3\u6784\u5305\u542b\u4ee5\u4e0b\u5355\u5143:\n <\/div>\n

Persistence: <\/strong>\u8fd9\u662f\u4e00\u4e2a\u5305\u542b\u7528\u4e8e\u83b7\u53d6EntityManagerFactory\u5b9e\u4f8b\u7684\u9759\u6001\u65b9\u6cd5\u7684\u7c7b\u3002<\/span>
\n EntityManagerFactory: <\/strong>\u662fEntityManager\u7684\u5de5\u5382\u7c7b\u3002\u5b83\u521b\u5efa\u548c\u7ba1\u7406EntityManager\u7684\u591a\u4e2a\u5b9e\u4f8b\u3002<\/span>
\n EntityManager: <\/strong>\u3002\u5b83\u662f\u4e00\u4e2a\u754c\u9762\u3002\u5b83\u63a7\u5236\u5bf9\u5bf9\u8c61\u7684\u6301\u4e45\u6027\u64cd\u4f5c\u3002\u5b83\u9002\u7528\u4e8eQuery\u5b9e\u4f8b\u3002<\/span>
\n Entity<\/strong>: \u5b9e\u4f53\u662f\u4f5c\u4e3a\u8bb0\u5f55\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\u7684\u6301\u4e45\u6027\u5bf9\u8c61\u3002<\/span>
\n Persistence Unit: <\/strong>\u5b83\u5b9a\u4e49\u4e86\u4e00\u7ec4\u6240\u6709\u5b9e\u4f53\u7c7b\u3002\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0cEntityManager\u5b9e\u4f8b\u5bf9\u5176\u8fdb\u884c\u7ba1\u7406\u3002\u4e00\u7ec4\u5b9e\u4f53\u7c7b\u8868\u793a\u5305\u542b\u5728\u5355\u4e2a\u6570\u636e\u5b58\u50a8\u4e2d\u7684\u6570\u636e\u3002<\/span>
\n EntityTransaction: <\/strong>\u5b83\u4e0eEntityManager\u7c7b\u5177\u6709\u4e00\u5bf9\u4e00<\/strong>\u5173\u7cfb\u3002\u5bf9\u4e8e\u6bcf\u4e2aEntityManager\uff0c\u64cd\u4f5c\u90fd\u7531EntityTransaction\u7c7b\u7ef4\u62a4\u3002<\/span>
\n Query: <\/strong>\u8be5\u63a5\u53e3\u7531\u6bcf\u4e2aJPA\u4f9b\u5e94\u5546\u5b9e\u73b0\uff0c\u4ee5\u83b7\u53d6\u7b26\u5408\u6761\u4ef6\u7684\u5173\u7cfb\u5bf9\u8c61\u3002<\/span>
\n Spring Boot JPA_https:\/\/bianchenghao6.com_\u3010SpringBoot \u6559\u7a0b\u3011_\u7b2c1\u5f20 <\/p>\n

JPA\u7c7b\u5173\u7cfb<\/h2>\n
\n \u6211\u4eec\u4e0a\u9762\u8ba8\u8bba\u7684\u7c7b\u548c\u63a5\u53e3\u4fdd\u6301\u4e00\u79cd\u5173\u7cfb\u3002\u4e0b\u56fe\u663e\u793a\u4e86\u7c7b\u548c\u63a5\u53e3\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\n <\/div>\n

Spring Boot JPA_https:\/\/bianchenghao6.com_\u3010SpringBoot \u6559\u7a0b\u3011_\u7b2c2\u5f20
\n EntityManager\u548cEntiyTransaction\u4e4b\u95f4\u7684\u5173\u7cfb\u662f\u4e00\u5bf9\u4e00<\/strong>\u3002\u6bcf\u4e2aEntityManager\u64cd\u4f5c\u90fd\u6709\u4e00\u4e2aEntityTransaction\u5b9e\u4f8b\u3002<\/span>
\n EntityManageFactory\u548cEntiyManager\u4e4b\u95f4\u7684\u5173\u7cfb\u662f\u4e00\u5bf9\u591a<\/strong>\u3002\u8fd9\u662fEntityManager\u5b9e\u4f8b\u7684\u5de5\u5382\u7c7b\u3002<\/span>
\n EntityManager\u548cQuery\u4e4b\u95f4\u7684\u5173\u7cfb\u662f\u4e00\u5bf9\u591a<\/strong>\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528EntityManager\u7c7b\u7684\u5b9e\u4f8b\u6267\u884c\u4efb\u610f\u6570\u91cf\u7684\u67e5\u8be2\u3002<\/span>
\n EntityManager\u548cEntity\u4e4b\u95f4\u7684\u5173\u7cfb\u662f\u4e00\u5bf9\u591a<\/strong>\u3002 EntityManager\u5b9e\u4f8b\u53ef\u4ee5\u7ba1\u7406\u591a\u4e2a\u5b9e\u4f53\u3002<\/span> <\/p>\n

JPA\u5b9e\u73b0<\/h2>\n
\n JPA\u662f\u4e00\u79cd\u5f00\u6e90API\u3002\u6709\u5404\u79cd\u4f01\u4e1a\u4f9b\u5e94\u5546\uff0c\u4f8b\u5982Eclipse\uff0cRedHat\uff0cOracle\u7b49\uff0c\u901a\u8fc7\u5728\u5176\u4e2d\u6dfb\u52a0JPA\u6765\u63d0\u4f9b\u65b0\u4ea7\u54c1\u3002\u6709\u4e00\u4e9b\u6d41\u884c\u7684JPA\u5b9e\u73b0\u6846\u67b6\uff0c\u4f8b\u5982
\n Hibernate\uff0cEclipseLink\uff0cDataNucleus <\/strong>\u7b49\u3002\u5b83\u4e5f\u79f0\u4e3a
\n \u5bf9\u8c61\u5173\u7cfb\u6620\u5c04<\/strong>(ORM)\u5de5\u5177\u3002\n <\/div>\n

\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04(ORM)<\/h2>\n
\n \u5728ORM\u4e2d\uff0cJava\u5bf9\u8c61\u5230\u6570\u636e\u5e93\u8868\u7684\u6620\u5c04(\u53cd\u4e4b\u4ea6\u7136)\u88ab\u79f0\u4e3a
\n \u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\u3002<\/strong> ORM\u6620\u5c04\u5145\u5f53
\n \u5173\u7cfb\u6570\u636e\u5e93<\/strong>(\u8868\u548c\u8bb0\u5f55)\u548c
\n Java\u5e94\u7528\u7a0b\u5e8f<\/strong>(\u7c7b\u548c\u5bf9\u8c61)\u4e4b\u95f4\u7684\u6865\u6881\u3002\n <\/div>\n
\n \u5728\u4e0b\u9762\u5982\u56fe\u6240\u793a\uff0cORM\u5c42\u662f\u9002\u914d\u5668\u5c42\u3002\u5b83\u5c06\u5bf9\u8c61\u56fe\u7684\u8bed\u8a00\u9002\u5e94SQL\u548c\u5173\u7cfb\u8868\u7684\u8bed\u8a00\u3002\n <\/div>\n

Spring Boot JPA_https:\/\/bianchenghao6.com_\u3010SpringBoot \u6559\u7a0b\u3011_\u7b2c3\u5f20 <\/p>\n

\n ORM\u5c42\u5b58\u5728\u4e8e\u5e94\u7528\u7a0b\u5e8f\u548c\u6570\u636e\u5e93\u4e4b\u95f4\u3002\u5b83\u8f6c\u6362Java\u7c7b\u548c\u5bf9\u8c61\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u5728\u5173\u7cfb\u6570\u636e\u5e93\u4e2d\u5b58\u50a8\u548c\u7ba1\u7406\u5b83\u4eec\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6301\u4e45\u5316\u7684\u540d\u79f0\u5c06\u6210\u4e3a\u8868\u7684\u540d\u79f0\uff0c\u800c\u5b57\u6bb5\u5c06\u6210\u4e3a\u5217\u3002\u4e00\u65e6\u5e94\u7528\u7a0b\u5e8f\u5efa\u7acb\uff0c\u6bcf\u4e2a\u8868\u884c\u90fd\u5bf9\u5e94\u4e00\u4e2a\u5bf9\u8c61\u3002\n <\/div>\n

JPA\u7248\u672c<\/h2>\n
\n EJB\u7684\u65e9\u671f\u7248\u672c\u5b9a\u4e49\u4e86\u4e0e\u4e1a\u52a1\u903b\u8f91\u5c42\u7ed3\u5408\u7684\u6301\u4e45\u5c42\u3002\u4f7f\u7528
\n javax.ejb.EntityBean <\/strong>\u63a5\u53e3\u3002 EJB\u89c4\u8303\u5305\u62ecJPA\u7684\u5b9a\u4e49\u3002\n <\/div>\n
\n \u5728\u5f15\u5165EJB 3.0\u65f6\uff0c\u6301\u4e45\u5c42\u88ab\u5206\u79bb\u5e76\u6307\u5b9a\u4e3aJPA 1.0(Java Persistence API)\u3002\u8be5API\u7684\u89c4\u8303\u5df2\u4e0e2006\u5e745\u670811\u65e5\u4f7f\u7528JSR 220\u7684JAVA EE5\u89c4\u8303\u4e00\u8d77\u53d1\u5e03\u3002\n <\/div>\n
\n 2019\u5e74\uff0cJPA\u91cd\u547d\u540d\u4e3a
\n Jakarta Persistence <\/strong>\u3002 JPA\u7684\u6700\u65b0\u7248\u672c\u662f
\n 2.2 <\/strong>\u3002\u5b83\u652f\u6301\u4ee5\u4e0b\u529f\u80fd:\n <\/div>\n

Java 8\uff0c\u6570\u636e\u548c\u65f6\u95f4API <\/span>
\n AttributeConvertes\u4e2d\u7684CDI\u6ce8\u5165<\/span>
\n \u5b83\u4f7f\u6ce8\u89e3@Repeatable <\/span> <\/p>\n

JPA\u548cHibernate\u4e4b\u95f4\u7684\u533a\u522b<\/h2>\n
\n JPA: <\/strong> JPA\u662f\u4e00\u79cdJava\u89c4\u8303\uff0c\u7528\u4e8e\u8bbf\u95ee\uff0c\u7ba1\u7406\u548c\u6301\u4e45\u5316\u4e4b\u95f4\u7684\u6570\u636eJava\u5bf9\u8c61\u548c\u5173\u7cfb\u6570\u636e\u5e93\u3002\u8fd9\u662fORM\u7684\u6807\u51c6\u65b9\u6cd5\u3002\n <\/div>\n
\n Hibernate: <\/strong>\u8fd9\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u5f00\u6e90ORM\u5de5\u5177\uff0c\u7528\u4e8e\u5728\u5173\u7cfb\u6570\u636e\u5e93\u7cfb\u7edf\u4e2d\u5b58\u50a8Java\u5bf9\u8c61\u3002\u5b83\u662fJPA\u7684\u63d0\u4f9b\u8005\u3002\u5b83\u9075\u5faaJPA\u63d0\u4f9b\u7684\u4e00\u79cd\u901a\u7528\u65b9\u6cd5\u3002\n <\/div>\n
\n \u4e0b\u8868\u63cf\u8ff0\u4e86JPA\u548cHibernate\u4e4b\u95f4\u7684\u533a\u522b\u3002\n <\/div>\n\n\n\n\n\n\n\n\n\n
JPA <\/td>\nHibernate<\/td>\n<\/tr>\n
JPA\u662f\u7528\u4e8e\u5728Java\u5e94\u7528\u7a0b\u5e8f\u4e2d\u6620\u5c04\u5173\u7cfb\u6570\u636e\u7684 Java\u89c4\u8303<\/strong>\u3002<\/td>\n Hibernate\u662f\u4e00\u4e2a\u7528\u4e8e\u5904\u7406\u6570\u636e\u6301\u4e45\u6027\u7684 ORM\u6846\u67b6<\/strong>\u3002<\/td>\n<\/tr>\n
JPA\u4e0d\u63d0\u4f9b\u4efb\u4f55\u5b9e\u73b0\u7c7b\u3002<\/td>\n\u5b83\u63d0\u4f9b\u4e86\u5b9e\u73b0\u7c7b\u3002<\/td>\n<\/tr>\n
\u5b83\u4f7f\u7528\u79f0\u4e3a JPQL <\/strong>(Java\u6301\u4e45\u6027\u67e5\u8be2\u8bed\u8a00)\u7684\u5e73\u53f0\u65e0\u5173\u7684\u67e5\u8be2\u8bed\u8a00\u3002<\/td>\n\u5b83\u4f7f\u7528\u81ea\u5df1\u7684\u79f0\u4e3a HQL <\/strong>\u7684\u67e5\u8be2\u8bed\u8a00(Hibernate\u67e5\u8be2\u8bed\u8a00)\u3002<\/td>\n<\/tr>\n
\u5b83\u5728 javax.persistence <\/strong>\u5305\u4e2d\u5b9a\u4e49\u3002<\/td>\n\u5b83\u5728 org.hibernate <\/strong>\u5305\u4e2d\u5b9a\u4e49\u3002<\/td>\n<\/tr>\n
\u5b83\u5728 Hibernate\uff0cEclipseLink <\/strong>\u7b49\u5404\u79cdORM\u5de5\u5177\u4e2d\u5b9e\u73b0\u3002<\/td>\nHibernate\u662fJPA\u7684\u63d0\u4f9b\u8005<\/strong>\u3002<\/td>\n<\/tr>\n
JPA\u4f7f\u7528 EntityManager <\/strong>\u5904\u7406\u6570\u636e\u7684\u6301\u4e45\u6027\u3002<\/td>\n\u5728Hibernate\u4e2d\uff0c\u4f7f\u7528 Session <\/strong>\u5904\u7406\u6570\u636e\u7684\u6301\u4e45\u6027\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

Spring Boot\u542f\u52a8\u7a0b\u5e8f\u6570\u636eJPA <\/h2>\n
\n Spring Boot\u63d0\u4f9b\u4e86\u542f\u52a8\u7a0b\u5e8f\u4f9d\u8d56\u9879
\n spring-boot-starter-data-jpa <\/strong>\u4ee5\u8fde\u63a5Spring Boot\u5e94\u7528\u7a0b\u5e8f\u9ad8\u6548\u5730\u4f7f\u7528\u5173\u7cfb\u6570\u636e\u5e93\u3002 spring-boot-starter-data-jpa\u5728\u5185\u90e8\u4f7f\u7528spring-boot-jpa\u4f9d\u8d56\u9879\u3002\n <\/div>\n
\n
\r\n<dependency>  \r\n<groupId>org.springframework.boot<\/groupId>  \r\n<artifactId>spring-boot-starter-data-jpa<\/artifactId>  \r\n<version>2.2.2.RELEASE<\/version>  \r\n<\/dependency>\r\n<\/pre>\n

<\/code>\n <\/div>\n

Spring Boot JPA\u793a\u4f8b<\/h2>\n
\n \u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u4f7f\u7528JPA\u8fde\u63a5\u5230\u6570\u636e\u5e93\u7684Spring Boot\u5e94\u7528\u7a0b\u5e8f\u3002\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u5185\u5b58\u6570\u636e\u5e93
\n Apache Derby\u3002<\/strong>\n <\/div>\n
\n Apache Derby <\/strong>: \u8fd9\u662f\u4e00\u4e2a\u5d4c\u5165\u5f0f\u7684
\n \u5f00\u6e90\u8f6f\u4ef6<\/strong>\u5173\u7cfb\u6570\u636e\u5e93\u5b8c\u5168\u7528Java\u5b9e\u73b0\u3002\u5b83\u5728Apache License 2.0\u4e0b\u53ef\u7528\u3002 Apache Derby\u5177\u6709\u4ee5\u4e0b\u4f18\u70b9:\n <\/div>\n

\u6613\u4e8e\u5b89\u88c5\uff0c\u90e8\u7f72\u548c\u4f7f\u7528\u3002<\/span>
\n \u5b83\u57fa\u4e8eJava\uff0cJDBC\u548cSQL\u6807\u51c6\u3002<\/span>
\n \u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5d4c\u5165\u5f0fJDBC\u9a71\u52a8\u7a0b\u5e8f\uff0c\u53ef\u8ba9\u6211\u4eec\u5c06Derby\u5d4c\u5165\u4efb\u4f55\u57fa\u4e8eJava\u7684\u89e3\u51b3\u65b9\u6848\u4e2d\u3002<\/span>
\n \u5b83\u8fd8\u901a\u8fc7Derby Network Client JDBC\u9a71\u52a8\u7a0b\u5e8f\u548cDerby Network Server\u652f\u6301\u5ba2\u6237\u673a\/\u670d\u52a1\u5668\u6a21\u5f0f\u3002<\/span> <\/p>\n

\n Spring Boot\u53ef\u4ee5\u81ea\u52a8\u914d\u7f6e\u5d4c\u5165\u5f0f\u6570\u636e\u5e93\uff0c\u4f8b\u5982
\n H2\uff0cHSQL\uff0c<\/strong>\u548c
\n Derbydatabases <\/strong>\u3002\u6211\u4eec\u4e0d\u9700\u8981\u63d0\u4f9b\u4efb\u4f55\u8fde\u63a5URL\u3002\u6211\u4eec\u53ea\u9700\u8981\u5305\u542b\u8981\u4f7f\u7528\u7684\u5d4c\u5165\u5f0f\u6570\u636e\u5e93\u7684\u6784\u5efa\u4f9d\u8d56\u9879\u5373\u53ef\u3002\n <\/div>\n
\n \u5728Spring Boot\u4e2d\uff0c\u53ea\u9700\u5728pom\u4e2d\u6dfb\u52a0
\n Derby <\/strong>\u4f9d\u8d56\u9879\u5373\u53ef\u8f7b\u677e\u96c6\u6210Apache Derby\u6570\u636e\u5e93\u3002 xml\u6587\u4ef6\u3002\n <\/div>\n
\n
\r\n<dependency>\r\n<groupId>org.apache.derby<\/groupId>\r\n<artifactId>derby<\/artifactId>\r\n<scope>runtime<\/scope>\r\n<\/dependency>\r\n<\/pre>\n

<\/code>\n <\/div>\n

\n \u6b65\u9aa41: <\/strong>\u6253\u5f00Spring Initializr https:\/\/start.spring.io\/\u3002\n <\/div>\n
\n \u6b65\u9aa42: <\/strong>\u9009\u62e9\u6700\u65b0\u7248\u672c\u7684Spring Boot
\n 2.3.0(SNAPSHOT)<\/strong>\n <\/div>\n
\n \u6b65\u9aa43: <\/strong>\u63d0\u4f9b
\n Group<\/strong>\u540d\u79f0\u3002\u6211\u4eec\u5df2\u7ecf\u63d0\u4f9b\u4e86
\n com.lidihuo\u3002<\/strong>\n <\/div>\n
\n \u6b65\u9aa44: <\/strong>\u63d0\u4f9b
\n Artifact<\/strong> ID\u3002\u6211\u4eec\u63d0\u4f9b\u4e86
\n apache-derby-example <\/strong>\u3002\n <\/div>\n
\n \u6b65\u9aa45: <\/strong>\u6dfb\u52a0\u4f9d\u8d56\u9879:
\n Spring Web\uff0cSpring Data JPA<\/strong>\u548c
\n Apache Derby\u6570\u636e\u5e93<\/strong>\u3002\n <\/div>\n
\n \u6b65\u9aa46: <\/strong>\u5355\u51fb
\n Generate <\/strong>(\u751f\u6210)\u6309\u94ae\u3002\u5f53\u6211\u4eec\u5355\u51fb\"\u751f\u6210\"\u6309\u94ae\u65f6\uff0c\u5b83\u4f1a\u5c06\u9879\u76ee\u5305\u88c5\u5230\u4e00\u4e2aJar\u6587\u4ef6\u4e2d\uff0c\u5e76\u5c06\u5176\u4e0b\u8f7d\u5230\u672c\u5730\u7cfb\u7edf\u3002\n <\/div>\n

Spring Boot JPA_https:\/\/bianchenghao6.com_\u3010SpringBoot \u6559\u7a0b\u3011_\u7b2c4\u5f20 <\/p>\n

\n \u6b65\u9aa47: \u63d0\u53d6<\/strong> Jar\u6587\u4ef6\u5e76\u5c06\u5176\u7c98\u8d34\u5230STS\u5de5\u4f5c\u533a\u4e2d\u3002\n <\/div>\n
\n \u6b65\u9aa48: \u5bfc\u5165<\/strong>\n <\/div>\n
\n \u6587\u4ef6->\u5bfc\u5165->\u73b0\u6709Maven\u9879\u76ee->\u6d4f\u89c8->\u9009\u62e9\u6587\u4ef6\u5939apache-derby-example->\u5b8c\u6210\n <\/div>\n
\n \u5bfc\u5165\u9700\u8981\u4e00\u4e9b\u65f6\u95f4\u3002\n <\/div>\n
\n \u6b65\u9aa49: <\/strong>\u5728\u6587\u4ef6\u5939
\n src\/main\/java<\/strong>\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u79f0\u4e3a
\n com.lidihuo.model <\/strong>\u7684\u5305\u3002\n <\/div>\n
\n \u6b65\u9aa410: <\/strong>\u5728\u5305
\n com.lidihuo.model <\/strong>\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u79f0\u4e3a
\n UserRecord <\/strong>\u7684\u7c7b\uff0c\u7136\u540e\u6267\u884c\u4ee5\u4e0b:\n <\/div>\n

\u5b9a\u4e49\u4e09\u4e2a\u53d8\u91cf id, name, and email<\/strong>\u3002<\/span>
\n \u751f\u6210Getter\u548cSetter\u3002
\u53f3\u952e\u5355\u51fb\u6587\u4ef6-> Source-> Generate Getter\u548cSetters <\/span>
\n \u5b9a\u4e49\u9ed8\u8ba4\u7684\u6784\u9020\u51fd\u6570\u3002<\/span>
\n \u4f7f\u7528\u6ce8\u89e3 @Entity\u5c06\u7c7b\u6807\u8bb0\u4e3aEntity<\/strong>\u3002<\/strong> <\/span>
\n \u4f7f\u7528\u6ce8\u89e3 @Id\u5c06 Id <\/strong>\u6807\u8bb0\u4e3a\u4e3b\u952e\u3002<\/strong> <\/span> <\/p>\n

\n UserRecord.java <\/strong>\n <\/div>\n
\n
\r\npackage <\/span>com.lidihuo.model;\r\nimport javax.persistence.Entity;\r\nimport javax.persistence.Id;\r\n@Entity  \r\npublic <\/span>class <\/span>UserRecord \r\n{ \r\n@Id  \r\nprivate <\/span>int <\/span>id;  \r\nprivate <\/span>String name;  \r\nprivate <\/span>String email;\r\n\/\/default <\/span>conatructor  \r\npublic <\/span>UserRecord()\r\n{\r\n}  \r\npublic <\/span>int <\/span>getId() \r\n{  \r\nreturn <\/span>id;  \r\n}  \r\npublic void <\/span>setId(int <\/span>id) \r\n{  \r\nthis<\/span>.id = id;  \r\n}  \r\npublic <\/span>String getName() \r\n{  \r\nreturn <\/span>name;  \r\n}  \r\npublic void <\/span>setName(String name) \r\n{  \r\nthis<\/span>.name = name;  \r\n}  \r\npublic <\/span>String getEmail() \r\n{  \r\nreturn <\/span>email;  \r\n}  \r\npublic void <\/span>setEmail(String email) \r\n{\r\nthis<\/span>.email = email;  \r\n}  \r\n}  \r\n<\/pre>\n

<\/code>\n <\/div>\n

\n \u6b65\u9aa411: <\/strong>\u5728
\n src\/main\/java\u6587\u4ef6\u5939\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u79f0\u4e3a com.lidihuo.controller <\/strong>\u7684\u5305\u3002<\/strong>\n <\/div>\n
\n \u6b65\u9aa412: <\/strong>\u5728\u5305
\n com.lidihuo.controller <\/strong>\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u79f0\u4e3a
\n UserController <\/strong>\u7684Controller\u7c7b\uff0c\u5e76\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c:\n <\/div>\n

\u4f7f\u7528\u6ce8\u89e3 @RestController <\/strong> \u5c06\u7c7b\u6807\u8bb0\u4e3a\u63a7\u5236\u5668\u3002<\/span>
\n \u4f7f\u7528\u6ce8\u89e3 @Autowired <\/strong>\u81ea\u52a8\u8fde\u63a5\u7c7b UserService <\/strong>\u3002<\/span>
\n \u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a\u6620\u5c04\uff0c\u4e00\u4e2a\u6620\u5c04\u7528\u4e8e\u83b7\u53d6\u6240\u6709\u7528\u6237<\/strong>\uff0c\u53e6\u4e00\u4e2a\u6620\u5c04\u7528\u4e8e\u6dfb\u52a0\u7528\u6237\u3002<\/strong> <\/span> <\/p>\n

\n UserController.java <\/strong>\n <\/div>\n
\n
\r\npackage <\/span>com.lidihuo.controller;  \r\nimport org.springframework.beans.factory.annotation.Autowired;  \r\nimport org.springframework.web.bind.annotation.RequestBody;  \r\nimport org.springframework.web.bind.annotation.RequestMapping;  \r\nimport org.springframework.web.bind.annotation.RequestMethod;  \r\nimport org.springframework.web.bind.annotation.RestController;\r\nimport com.lidihuo.model.UserRecord;\r\nimport com.lidihuo.service.UserService;\r\nimport java.util.List;  \r\n@RestController  \r\npublic <\/span>class <\/span>UserController \r\n{  \r\n@Autowired  \r\nprivate <\/span>UserService userService;   \r\n@RequestMapping(\"\/\"<\/span>)  \r\npublic <\/span>List<UserRecord> getAllUser()\r\n{  \r\nreturn <\/span>userService.getAllUsers<\/span>();  \r\n}     \r\n@RequestMapping(value=\"\/add-user\"<\/span>, method=RequestMethod.POST)  \r\npublic void <\/span>addUser(@RequestBody UserRecord userRecord)\r\n{  \r\nuserService.addUser<\/span>(userRecord);  \r\n}     \r\n}  \r\n<\/pre>\n

<\/code>\n <\/div>\n

\n \u6b65\u9aa413: <\/strong>\u5728\u6587\u4ef6\u5939
\n src\/main\/java\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u79f0\u4e3a com.lidihuo.service <\/strong>\u7684\u5305\u3002<\/strong>\n <\/div>\n
\n \u6b65\u9aa414: <\/strong>\u5728\u5305
\n com.lidihuo.service <\/strong>\u4e2d\u521b\u5efa\u540d\u79f0\u4e3a
\n UserController <\/strong>\u7684Service\u7c7b\uff0c\u5e76\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c:\n <\/div>\n

\u901a\u8fc7\u4f7f\u7528\u6ce8\u89e3 @Service<\/strong>\u5c06\u8be5\u7c7b\u6807\u8bb0\u4e3a\u670d\u52a1\u3002 <\/span>
\n \u81ea\u52a8\u8fde\u63a5 UserRepository <\/strong> <\/span>
\n \u5b9a\u4e49\u65b9\u6cd5 getAllUsers()<\/strong>\uff0c\u8be5\u65b9\u6cd5\u8fd4\u56de\u4ee5\u4e0b\u5217\u8868: <\/span>
\n \u5b9a\u4e49\u53e6\u4e00\u4e2a\u65b9\u6cd5\u540d\u79f0 addUser()<\/strong>\uff0c\u4ee5\u4fdd\u5b58\u7528\u6237\u8bb0\u5f55\u3002<\/span> <\/p>\n

\n UserService.java <\/strong>\n <\/div>\n
\n
\r\npackage <\/span>com.lidihuo.service;  \r\nimport java.util.List;  \r\nimport java.util.ArrayList;  \r\nimport org.springframework.beans.factory.annotation.Autowired;  \r\nimport org.springframework.stereotype.Service;\r\nimport com.lidihuo.model.UserRecord;\r\nimport com.lidihuo.repository.UserRepository;  \r\n@Service  \r\npublic <\/span>class <\/span>UserService \r\n{  \r\n@Autowired  \r\nprivate <\/span>UserRepository userRepository;  \r\npublic <\/span>List<UserRecord> getAllUsers()\r\n{  \r\nList<UserRecord>userRecords = new <\/span>ArrayList<>();  \r\nuserRepository.findAll<\/span>().forEach<\/span>(userRecords::add);  \r\nreturn <\/span>userRecords;  \r\n}  \r\npublic void <\/span>addUser(UserRecord userRecord)\r\n{  \r\nuserRepository.save<\/span>(userRecord);  \r\n}  \r\n}  \r\n<\/pre>\n

<\/code>\n <\/div>\n

\n \u6b65\u9aa415: <\/strong>\u5728\u6587\u4ef6\u5939
\n src\/main\/java<\/strong>\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u79f0\u4e3a
\n com.lidihuo.repository <\/strong>\u7684\u5305\u3002\n <\/div>\n
\n \u6b65\u9aa416: <\/strong>\u5728\u5305
\n com.lidihuo.repository <\/strong>\u4e2d\u521b\u5efa\u540d\u79f0\u4e3a
\n UserRepository <\/strong>\u7684\u5b58\u50a8\u5e93\u63a5\u53e3\uff0c\u5e76\u6269\u5c55
\n CrudRepository <\/strong>\u3002\n <\/div>\n
\n UserRepository.java <\/strong>\n <\/div>\n
\n
\r\npackage <\/span>com.lidihuo.repository;  \r\nimport org.springframework.data.repository.CrudRepository;\r\nimport com.lidihuo.model.UserRecord;  \r\npublic <\/span>interface UserRepository extends <\/span>CrudRepository<UserRecord, String> \r\n{  \r\n}  \r\n<\/pre>\n

<\/code>\n <\/div>\n

\n \u6b65\u9aa417: <\/strong>\u73b0\u5728\uff0c\u6253\u5f00
\n ApacheDerbyExampleApplication.java <\/strong>\u6587\u4ef6\u3002\u5b83\u662f\u5728\u6211\u4eec\u8bbe\u7f6e\u5e94\u7528\u7a0b\u5e8f\u65f6\u9ed8\u8ba4\u521b\u5efa\u7684\u3002\n <\/div>\n
\n ApacheDerbyExampleApplication.java <\/strong>\n <\/div>\n
\n
\r\npackage <\/span>com.lidihuo;\r\nimport org.springframework.boot.SpringApplication;\r\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\r\n@SpringBootApplication\r\npublic <\/span>class <\/span>ApacheDerbyExampleApplication \r\n{\r\npublic <\/span>static void <\/span>main(String[] args) \r\n{\r\nSpringApplication.run<\/span>(ApacheDerbyExampleApplication.class, args);\r\n}\r\n}\r\n<\/pre>\n

<\/code>\n <\/div>\n

\n \u73b0\u5728\uff0c\u6211\u4eec\u5df2\u6839\u636e\u8981\u6c42\u8bbe\u7f6e\u4e86\u6240\u6709\u5fc5\u9700\u7684\u7c7b\u548c\u7a0b\u5e8f\u5305\u3002\u8bf7\u6ce8\u610f\uff0c\u6211\u4eec\u6ca1\u6709\u4e3a\u6570\u636e\u5e93\u63d0\u4f9b\u4efb\u4f55
\n \u8fde\u63a5URL <\/strong>\u3002\u5b8c\u6210\u4e0a\u8ff0\u6240\u6709\u6b65\u9aa4\u540e\uff0c\u9879\u76ee\u76ee\u5f55\u5982\u4e0b\u6240\u793a:\n <\/div>\n

Spring Boot JPA_https:\/\/bianchenghao6.com_\u3010SpringBoot \u6559\u7a0b\u3011_\u7b2c5\u5f20 <\/p>\n

\n \u8ba9\u6211\u4eec\u8fd0\u884c\u8be5\u5e94\u7528\u7a0b\u5e8f\u3002\n <\/div>\n
\n \u6b65\u9aa418: <\/strong>\u6253\u5f00
\n ApacheDerbyExampleApplication.java <\/strong>\u6587\u4ef6\u5e76\u5c06\u5176\u4f5c\u4e3aJava\u5e94\u7528\u7a0b\u5e8f\u8fd0\u884c\u3002\n <\/div>\n
\n \u7b2c19\u6b65: <\/strong>\u6253\u5f00\u6d4f\u89c8\u5668\u5e76\u8c03\u7528URL http:\/\/localhost:8080\/\u3002\u7531\u4e8e\u672a\u5728\u5217\u8868\u4e2d\u6dfb\u52a0\u4efb\u4f55\u7528\u6237\uff0c\u56e0\u6b64\u8fd4\u56de\u7a7a\u5217\u8868\u3002\n <\/div>\n
\n \u8981\u5c06\u7528\u6237\u6dfb\u52a0\u5230\u6570\u636e\u5e93\uff0c\u6211\u4eec\u5c06\u4f7f\u7528
\n \u53d1\u9001 POST <\/strong>\u8bf7\u6c42Postman<\/strong>\u3002\n <\/div>\n
\n \u6b65\u9aa420: <\/strong>\u6253\u5f00
\n Postman<\/strong>\uff0c\u7136\u540e\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c:\n <\/div>\n

\u9009\u62e9 POST <\/strong> <\/span>
\n \u8c03\u7528URL http:\/\/localhost:8080\/add-user\u3002<\/span>
\n \u70b9\u51fbBody<\/strong> <\/span>
\n \u9009\u62e9Content-Type\u4f5c\u4e3aJSON <\/strong>(application\/json)\u3002<\/span>
\n \u5c06\u8981\u63d2\u5165\u7684\u6570\u636e\u63d2\u5165\u6570\u636e\u5e93\u4e2d\u3002\u6211\u4eec\u63d2\u5165\u4e86\u4ee5\u4e0b\u6570\u636e: <\/span> <\/p>\n

\n
\r\n{\r\n\"id\"<\/span>: \"001\"<\/span>,\r\n\"name\"<\/span>: \"Tom\"<\/span>,\r\n\"email\"<\/span>: \"tom@gmail.com\"<\/span>\r\n}\r\n<\/pre>\n

<\/code>\n <\/div>\n

\u70b9\u51fb\u53d1\u9001<\/strong>\u6309\u94ae\u3002<\/span>
\n Spring Boot JPA_https:\/\/bianchenghao6.com_\u3010SpringBoot \u6559\u7a0b\u3011_\u7b2c6\u5f20 <\/p>\n

\n \u5f53\u6211\u4eec\u5355\u51fb\"\u53d1\u9001\"\u6309\u94ae\u65f6\uff0c\u5b83\u663e\u793a
\n \u72b6\u6001: 200 OK <\/strong>\u3002\u8fd9\u8868\u793a\u8bf7\u6c42\u5df2\u6210\u529f\u6267\u884c\u3002\n <\/div>\n
\n \u6b65\u9aa421: <\/strong>\u6253\u5f00\u6d4f\u89c8\u5668\u5e76\u8c03\u7528URL http: \/\/localhost: 8080\u3002\u5b83\u8fd4\u56de\u6211\u4eec\u5df2\u63d2\u5165\u6570\u636e\u5e93\u4e2d\u7684\u7528\u6237\u3002\n <\/div>\n

Spring Boot JPA_https:\/\/bianchenghao6.com_\u3010SpringBoot \u6559\u7a0b\u3011_\u7b2c7\u5f20
\n <\/p>\n


\n
\n SpringBoot JDBC
\n SpringBoot JDBC\u793a\u4f8b
\n SpringBoot \u589e\u5220\u6539\u67e5\u793a\u4f8b
\n SpringBoot H2\u6570\u636e\u5e93\u8bf4\u660e\u548c\u793a\u4f8b\n <\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Spring Boot JPAzh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[104],"tags":[],"class_list":["post-591","post","type-post","status-publish","format-standard","hentry","category-bc_springboot_jc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/591"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=591"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/591\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}