{"id":6831,"date":"2024-11-14T09:28:11","date_gmt":"2024-11-14T01:28:11","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6831\/"},"modified":"2024-11-14T09:28:11","modified_gmt":"2024-11-14T01:28:11","slug":"%e4%bd%bf%e7%94%a8-jdbc-%e6%a8%a1%e6%9d%bf%e6%97%b6%ef%bc%8c%e5%a6%82%e4%bd%95%e8%87%aa%e5%ae%9a%e4%b9%89%e5%8d%a0%e4%bd%8d%e7%ac%a6%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6831\/","title":{"rendered":"\u4f7f\u7528 JDBC \u6a21\u677f\u65f6\uff0c\u5982\u4f55\u81ea\u5b9a\u4e49\u5360\u4f4d\u7b26\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/172995480568846.jpg\" class=\"aligncenter\" title=\"\u4f7f\u7528 JDBC \u6a21\u677f\u65f6\uff0c\u5982\u4f55\u81ea\u5b9a\u4e49\u5360\u4f4d\u7b26\uff1f\u63d2\u56fe\" alt=\"\u4f7f\u7528 JDBC \u6a21\u677f\u65f6\uff0c\u5982\u4f55\u81ea\u5b9a\u4e49\u5360\u4f4d\u7b26\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4f7f\u7528 jdbc \u6a21\u677f\u65f6\uff0c\u662f\u5426\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5360\u4f4d\u7b26\uff1f<\/strong><\/p>\n<p>jdbctemplate \u7684\u5360\u4f4d\u7b26\u4ec5\u9650\u4e8e\u95ee\u53f7 (?)\u3002\u5982\u679c\u60a8\u5e0c\u671b\u4f7f\u7528\u81ea\u5b9a\u4e49\u5360\u4f4d\u7b26\uff08\u4f8b\u5982 :id\uff09\uff0c\u5219\u9700\u8981\u4f7f\u7528 namedparameterjdbctemplate\uff0c\u8fd9\u662f\u4e00\u79cd\u6269\u5c55\u7684 jdbctemplate\u3002<\/p>\n<p>namedparameterjdbctemplate \u5141\u8bb8\u60a8\u4f7f\u7528\u5177\u6709\u540d\u79f0\u7684\u5360\u4f4d\u7b26\uff0c\u4f8b\u5982 :id\u3002\u4e0b\u9762\u662f\u5982\u4f55\u4f7f\u7528\u5b83\uff1a<\/p>\n<ol>\n<li>\u521b\u5efa sqlparametersource \u5bf9\u8c61\u3002\u8fd9\u53ef\u4ee5\u662f mapsqlparametersource\uff08\u4f7f\u7528\u6307\u5b9a\u53c2\u6570\uff09\u6216 beanpropertysqlparametersource\uff08\u4f7f\u7528 javabean \u5c5e\u6027\uff09\u3002<\/li>\n<li>\u8bbe\u7f6e\u547d\u540d\u53c2\u6570\uff1a<\/li>\n<\/ol>\n<pre>sqlparametersource namedparameters = new mapsqlparametersource().addvalue(\"id\", 1);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u5728 sql \u67e5\u8be2\u4e2d\u4f7f\u7528\u547d\u540d\u7684\u5360\u4f4d\u7b26\uff1a<\/li>\n<\/ol>\n<pre>select first_name from employee where id = :id<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u4f7f\u7528 namedparameterjdbctemplate.queryforobject() \u65b9\u6cd5\u6267\u884c\u67e5\u8be2\uff0c\u5c06 sqlparametersource \u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\uff1a<\/li>\n<\/ol>\n<pre>string firstname = namedparameterjdbctemplate.queryforobject(\n    \"select first_name from employee where id = :id\", namedparameters, string.class);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528 namedparameterjdbctemplate \u6765\u8bbe\u7f6e\u590d\u6742\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u5e26\u6709 firstname \u5c5e\u6027\u7684 employee \u7c7b\uff1a<\/p>\n<pre>employee employee = new employee();\nemployee.setfirstname(\"james\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>sql \u67e5\u8be2\u53ef\u4ee5\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>select count(*) from employee where first_name = :firstname<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>SqlParameterSource namedParameters = new BeanPropertySqlParameterSource(employee);\nInteger count = namedParameterJdbcTemplate.queryForObject(\n    SELECT_BY_ID, namedParameters, Integer.class);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528 JDBC \u6a21\u677f\u65f6\uff0c\u5982\u4f55\u81ea\u5b9a\u4e49\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>\u4f7f\u7528 jdbc \u6a21\u677f\u65f6\uff0c\u662f\u5426\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5360\u4f4d\u7b26\uff1f jdbctemplate \u7684\u5360\u4f4d\u7b26\u4ec5\u9650\u4e8e\u95ee\u53f7 (?)\u3002\u5982\u679c\u60a8\u5e0c\u671b\u4f7f\u7528\u81ea\u5b9a\u4e49\u5360\u4f4d\u7b26\uff08\u4f8b\u5982 :id\uff09\uff0c\u5219\u9700\u8981\u4f7f\u7528 namedparameterjdbctemplate\uff0c\u8fd9\u662f\u4e00\u79cd\u6269\u5c55\u7684 jdbctemplate\u3002 namedparameterjdbctemplate \u5141\u8bb8\u60a8\u4f7f\u7528\u5177\u6709\u540d\u79f0\u7684\u5360\u4f4d\u7b26\uff0c\u4f8b\u5982 :id\u3002\u4e0b\u9762\u662f\u5982\u4f55\u4f7f\u7528\u5b83\uff1a \u521b\u5efa sqlparametersource \u5bf9\u8c61\u3002\u8fd9\u53ef\u4ee5\u662f mapsqlparametersource\uff08\u4f7f\u7528\u6307\u5b9a\u53c2\u6570\uff09\u6216 beanpropertysqlparametersource\uff08\u4f7f\u7528 javabean \u5c5e\u6027\uff09\u3002 \u8bbe\u7f6e\u547d\u540d\u53c2\u6570\uff1a sqlparametersource namedparameters = new mapsqlparametersource().addvalue(&#8220;id&#8221;, 1); \u767b\u5f55\u540e\u590d\u5236 \u5728 sql \u67e5\u8be2\u4e2d\u4f7f\u7528\u547d\u540d\u7684\u5360\u4f4d\u7b26\uff1a select first_name from employee where id = :id \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 namedparameterjdbctemplate.queryforobject() \u65b9\u6cd5\u6267\u884c\u67e5\u8be2\uff0c\u5c06 sqlparametersource \u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\uff1a string firstname = namedparameterjdbctemplate.queryforobject( &#8220;select first_name from employee where id = [&hellip;]<\/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-6831","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6831","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=6831"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6831\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}