{"id":200385,"date":"2025-05-06T09:26:56","date_gmt":"2025-05-06T01:26:56","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200385\/"},"modified":"2025-05-06T09:26:56","modified_gmt":"2025-05-06T01:26:56","slug":"jdbctemplate%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e5%8d%a0%e4%bd%8d%e7%ac%a6%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200385\/","title":{"rendered":"JdbcTemplate\u4e2d\u5982\u4f55\u4f7f\u7528\u5360\u4f4d\u7b26\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>JdbcTemplate\u4e2d\u5982\u4f55\u4f7f\u7528\u5360\u4f4d\u7b26\uff1f<\/h1>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241103\/173063848967277299b3a56.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>jdbctemplate\u4e2d\u5360\u4f4d\u7b26\u7684\u4f7f\u7528<\/strong><\/p>\n<p>jdbctemplate\u662fspring jdbc\u6846\u67b6\u4e2d\u7684\u4e00\u4e2a\u7c7b\uff0c\u5b83\u63d0\u4f9b\u4e86\u5bf9jdbc api\u7684\u7b80\u5316\u8bbf\u95ee\u3002\u867d\u7136jdbctemplate\u901a\u5e38\u4f7f\u7528\u95ee\u53f7(?)\u4f5c\u4e3a\u5360\u4f4d\u7b26\uff0c\u4f46\u5b83\u5e76\u4e0d\u5f3a\u5236\u8981\u6c42\u3002<\/p>\n<p><strong>\u4f7f\u7528bean\u5c5e\u6027\u5360\u4f4d\u7b26<\/strong><\/p>\n<p>\u5982\u679c\u60a8\u5e0c\u671b\u4f7f\u7528bean\u5c5e\u6027\u4f5c\u4e3a\u5360\u4f4d\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528namedparameterjdbctemplate\u3002\u5176\u4f7f\u7528\u65b9\u6cd5\u4e0ejdbctemplate\u7c7b\u4f3c\uff0c\u4f46\u4f7f\u7528beanpropertysqlparametersource\u5bf9\u8c61\u6765\u6307\u5b9a\u53c2\u6570\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>employee employee = new employee();\nemployee.setid(1);\nnamedparameterjdbctemplate.queryforobject(\n  \"select * from test where id = :id\", new beanpropertysqlparametersource(employee), string.class);<\/pre>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c&#8221;:id&#8221;\u5c06\u88abbean\u5c5e\u6027&#8221;id&#8221;\u7684\u503c\u66ff\u6362\u3002<\/p>\n<p><strong>\u4f7f\u7528map\u5360\u4f4d\u7b26<\/strong><\/p>\n<p>\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528mapsqlparametersource\u5bf9\u8c61\u6307\u5b9a\u53c2\u6570\u503c\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>Map&lt;String, Object&gt; namedParameters = new HashMap&lt;&gt;();\nnamedParameters.put(\"id\", 1);\nnamedParameterJdbcTemplate.queryForObject(\n  \"SELECT * FROM test WHERE id = :id\", namedParameters, String.class);<\/pre>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>namedparameterjdbctemplate\u9700\u8981spring 4.0\u6216\u66f4\u9ad8\u7248\u672c\u3002<\/li>\n<li>\u5360\u4f4d\u7b26\u540d\u79f0\u5bf9\u4e8e\u4f7f\u7528mapsqlparametersource\u65f6\u5fc5\u987b\u76f8\u540c\u3002<\/li>\n<li>\u4f7f\u7528beanpropertysqlparametersource\u65f6\uff0c\u5360\u4f4d\u7b26\u540d\u79f0\u4e0d\u9700\u8981\u4e0ebean\u5c5e\u6027\u5b8c\u5168\u76f8\u540c\uff08\u5927\u5c0f\u5199\u654f\u611f\uff09\u3002<\/li>\n<\/ul>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aJdbcTemplate\u4e2d\u5982\u4f55\u4f7f\u7528\u5360\u4f4d\u7b26\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\u5427\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>JdbcTemplate\u4e2d\u5982\u4f55\u4f7f&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200385","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200385","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=200385"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200385\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}