{"id":6836,"date":"2024-11-14T11:24:07","date_gmt":"2024-11-14T03:24:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6836\/"},"modified":"2024-11-14T11:24:07","modified_gmt":"2024-11-14T03:24:07","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:\/\/fwq.ai\/blog\/6836\/","title":{"rendered":"JdbcTemplate\u4e2d\u5982\u4f55\u4f7f\u7528\u5360\u4f4d\u7b26\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/172998865276152.jpg\" class=\"aligncenter\" title=\"JdbcTemplate\u4e2d\u5982\u4f55\u4f7f\u7528\u5360\u4f4d\u7b26\uff1f\u63d2\u56fe\" alt=\"JdbcTemplate\u4e2d\u5982\u4f55\u4f7f\u7528\u5360\u4f4d\u7b26\uff1f\u63d2\u56fe\" \/><\/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>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\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>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\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>\u4ee5\u4e0a\u5c31\u662fJdbcTemplate\u4e2d\u5982\u4f55\u4f7f\u7528\u5360\u4f4d\u7b26\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>jdbctemplate\u4e2d\u5360\u4f4d\u7b26\u7684\u4f7f\u7528 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 \u4f7f\u7528bean\u5c5e\u6027\u5360\u4f4d\u7b26 \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 employee employee = new employee(); employee.setid(1); namedparameterjdbctemplate.queryforobject( &#8220;select * from test where id = :id&#8221;, new beanpropertysqlparametersource(employee), string.class); \u767b\u5f55\u540e\u590d\u5236 \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c&#8221;:id&#8221;\u5c06\u88abbean\u5c5e\u6027&#8221;id&#8221;\u7684\u503c\u66ff\u6362\u3002 \u4f7f\u7528map\u5360\u4f4d\u7b26 \u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528mapsqlparametersource\u5bf9\u8c61\u6307\u5b9a\u53c2\u6570\u503c\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a Map&lt;String, Object&gt; namedParameters = new HashMap&lt;&gt;(); namedParameters.put(&#8220;id&#8221;, 1); namedParameterJdbcTemplate.queryForObject( &#8220;SELECT * FROM test WHERE id = :id&#8221;, namedParameters, String.class); \u767b\u5f55\u540e\u590d\u5236 \u6ce8\u610f\uff1a namedparameterjdbctemplate\u9700\u8981spring 4.0\u6216\u66f4\u9ad8\u7248\u672c\u3002 \u5360\u4f4d\u7b26\u540d\u79f0\u5bf9\u4e8e\u4f7f\u7528mapsqlparametersource\u65f6\u5fc5\u987b\u76f8\u540c\u3002 \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 \u4ee5\u4e0a\u5c31\u662fJdbcTemplate\u4e2d\u5982\u4f55\u4f7f\u7528\u5360\u4f4d\u7b26\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-6836","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6836","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=6836"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6836\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}