{"id":12315,"date":"2023-11-28T22:50:01","date_gmt":"2023-11-28T14:50:01","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=12315"},"modified":"2023-12-11T16:18:32","modified_gmt":"2023-12-11T08:18:32","slug":"spring-zhong-jdbctemplate-le-jie-ma","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/spring-zhong-jdbctemplate-le-jie-ma\/","title":{"rendered":"Spring \u4e2d JdbcTemplate \u4e86\u89e3\u5417\uff1f"},"content":{"rendered":"<p>\u662f\u7684\uff0c\u6211\u4e86\u89e3<code>JdbcTemplate<\/code>\u3002<code>JdbcTemplate<\/code>\u662fSpring\u6846\u67b6\u4e2d\u7528\u4e8e\u7b80\u5316JDBC\u64cd\u4f5c\u7684\u6838\u5fc3\u7c7b\u4e4b\u4e00\u3002\u5b83\u5c01\u88c5\u4e86\u8bb8\u591a\u4f20\u7edf\u7684JDBC\u64cd\u4f5c\uff0c\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u65b9\u4fbf\u3001\u66f4\u9ad8\u7ea7\u7684\u65b9\u5f0f\u6765\u6267\u884cSQL\u67e5\u8be2\u3001\u66f4\u65b0\u7b49\u64cd\u4f5c\uff0c\u540c\u65f6\u5904\u7406\u4e86\u8d44\u6e90\u7684\u83b7\u53d6\u548c\u91ca\u653e\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f<code>JdbcTemplate<\/code>\u7684\u4e00\u4e9b\u5173\u952e\u7279\u70b9\u548c\u4f7f\u7528\u65b9\u5f0f\uff1a<\/p>\n<ol>\n<li>\n<strong>\u6570\u636e\u6e90\u914d\u7f6e\uff1a<\/strong><\/p>\n<ul>\n<li><code>JdbcTemplate<\/code>\u9700\u8981\u4e0e\u4e00\u4e2a\u6570\u636e\u6e90\uff08<code>DataSource<\/code>\uff09\u5173\u8054\uff0c\u8be5\u6570\u636e\u6e90\u8d1f\u8d23\u7ba1\u7406\u6570\u636e\u5e93\u8fde\u63a5\u3002\u53ef\u4ee5\u901a\u8fc7Spring\u7684\u914d\u7f6e\u6587\u4ef6\u6216\u6ce8\u89e3\u6765\u914d\u7f6e\u6570\u636e\u6e90\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<strong>SQL\u64cd\u4f5c\uff1a<\/strong><\/p>\n<ul>\n<li><code>JdbcTemplate<\/code>\u63d0\u4f9b\u4e86\u6267\u884c\u5404\u79cdSQL\u64cd\u4f5c\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u67e5\u8be2\u3001\u66f4\u65b0\u3001\u6279\u5904\u7406\u7b49\u3002\u5b83\u7b80\u5316\u4e86SQL\u8bed\u53e5\u7684\u6267\u884c\u8fc7\u7a0b\uff0c\u65e0\u9700\u624b\u52a8\u5904\u7406\u8fde\u63a5\u7684\u83b7\u53d6\u548c\u91ca\u653e\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">\/\/ \u793a\u4f8b\uff1a\u67e5\u8be2\u5355\u4e2a\u7ed3\u679c\nString sql = &quot;SELECT column FROM table WHERE condition&quot;;\nString result = jdbcTemplate.queryForObject(sql, String.class);\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u53c2\u6570\u5316\u67e5\u8be2\uff1a<\/strong><\/p>\n<ul>\n<li><code>JdbcTemplate<\/code>\u652f\u6301\u53c2\u6570\u5316\u67e5\u8be2\uff0c\u9632\u6b62SQL\u6ce8\u5165\u653b\u51fb\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>?<\/code>\u5360\u4f4d\u7b26\u6216\u547d\u540d\u53c2\u6570\u7684\u65b9\u5f0f\u8bbe\u7f6e\u67e5\u8be2\u53c2\u6570\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">\/\/ \u793a\u4f8b\uff1a\u4f7f\u7528\u547d\u540d\u53c2\u6570\u8fdb\u884c\u67e5\u8be2\nString sql = &quot;SELECT column FROM table WHERE id = :id&quot;;\nMap&lt;String, Object&gt; paramMap = new HashMap&lt;&gt;();\nparamMap.put(&quot;id&quot;, 123);\nString result = namedParameterJdbcTemplate.queryForObject(sql, paramMap, String.class);\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u5f02\u5e38\u5904\u7406\uff1a<\/strong><\/p>\n<ul>\n<li><code>JdbcTemplate<\/code>\u5bf9JDBC\u5f02\u5e38\u8fdb\u884c\u4e86\u5c01\u88c5\uff0c\u5c06\u5176\u8f6c\u5316\u4e3aSpring\u7684<code>DataAccessException<\/code>\uff0c\u4f7f\u5f02\u5e38\u5904\u7406\u66f4\u4e3a\u65b9\u4fbf\u3002\u53ef\u4ee5\u901a\u8fc7\u6355\u83b7<code>DataAccessException<\/code>\u6765\u5904\u7406\u6570\u636e\u5e93\u8bbf\u95ee\u7684\u5f02\u5e38\u60c5\u51b5\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<strong>\u56de\u8c03\u51fd\u6570\uff1a<\/strong><\/p>\n<ul>\n<li><code>JdbcTemplate<\/code>\u652f\u6301\u4f7f\u7528\u56de\u8c03\u51fd\u6570\uff08Callback\uff09\u6267\u884cJDBC\u64cd\u4f5c\u3002\u901a\u8fc7\u4f20\u9012\u56de\u8c03\u51fd\u6570\u7ed9<code>JdbcTemplate<\/code>\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u64cd\u4f5c\u7684\u4e0d\u540c\u9636\u6bb5\u6267\u884c\u81ea\u5b9a\u4e49\u7684\u903b\u8f91\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">\/\/ \u793a\u4f8b\uff1a\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u6267\u884c\u67e5\u8be2\njdbcTemplate.query(&quot;SELECT column FROM table&quot;, new RowCallbackHandler() {\n    @Override\n    public void processRow(ResultSet rs) throws SQLException {\n        \/\/ \u5904\u7406\u6bcf\u4e00\u884c\u7684\u7ed3\u679c\n    }\n});\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u6279\u5904\u7406\uff1a<\/strong><\/p>\n<ul>\n<li><code>JdbcTemplate<\/code>\u63d0\u4f9b\u4e86\u5bf9\u6279\u5904\u7406\u7684\u652f\u6301\uff0c\u53ef\u4ee5\u4e00\u6b21\u6027\u6267\u884c\u591a\u4e2aSQL\u8bed\u53e5\uff0c\u63d0\u9ad8\u6570\u636e\u64cd\u4f5c\u7684\u6548\u7387\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">\/\/ \u793a\u4f8b\uff1a\u6279\u5904\u7406\u66f4\u65b0\u64cd\u4f5c\njdbcTemplate.batchUpdate(&quot;UPDATE table SET column = ? WHERE id = ?&quot;, Arrays.asList(&quot;value1&quot;, 1), Arrays.asList(&quot;value2&quot;, 2));\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p><code>JdbcTemplate<\/code>\u662fSpring\u6846\u67b6\u4e2d\u5bf9JDBC\u64cd\u4f5c\u8fdb\u884c\u7b80\u5316\u548c\u62bd\u8c61\u7684\u91cd\u8981\u7ec4\u4ef6\u4e4b\u4e00\uff0c\u4f7f\u5f97\u5f00\u53d1\u8005\u80fd\u591f\u66f4\u5bb9\u6613\u5730\u8fdb\u884c\u6570\u636e\u5e93\u8bbf\u95ee\uff0c\u800c\u65e0\u9700\u5173\u6ce8\u5e95\u5c42\u7684JDBC\u7ec6\u8282\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u662f\u7684\uff0c\u6211\u4e86\u89e3JdbcTemplate\u3002JdbcTemplate\u662fSpring\u6846\u67b6\u4e2d\u7528\u4e8e\u7b80\u5316JDBC\u64cd\u4f5c\u7684\u6838\u5fc3\u7c7b [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[],"class_list":["post-12315","post","type-post","status-publish","format-standard","hentry","category-spring"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=12315"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12315\/revisions"}],"predecessor-version":[{"id":47884,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12315\/revisions\/47884"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=12315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=12315"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=12315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}