{"id":4140,"date":"2019-04-23T15:37:05","date_gmt":"2019-04-23T07:37:05","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=4140"},"modified":"2019-04-23T15:37:05","modified_gmt":"2019-04-23T07:37:05","slug":"%e9%9d%a2%e8%af%95%e5%ae%98%ef%bc%9a%e4%bd%bf%e7%94%a8jdbctemplate-in-%e8%af%ad%e5%8f%a5%e4%bd%a0%e9%81%87%e5%88%b0%e8%bf%87%e5%93%aa%e4%ba%9b%e5%9d%91%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=4140","title":{"rendered":"\u9762\u8bd5\u5b98\uff1a\u4f7f\u7528JdbcTemplate in \u8bed\u53e5\u4f60\u9047\u5230\u8fc7\u54ea\u4e9b\u5751\uff1f"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u6700\u8fd1\uff0c\u540c\u4e8b\u5728\u7a7a\u95f2\u65f6\u95f4\u5199\u4e86\u4e00\u4e2a Spring \u7684 demo\uff0c\u9047\u5230\u4e86\u4e00\u4e2a\u5173\u4e8e JdbcTemplate \u7684\u5c0f\u5751\u3002\u4eca\u5929\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u8fd9\u4e2a\u5751\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u822c\u540c\u4e8b\u4e0d\u558a\u6211\uff0c\u4e00\u558a\u6211\u51c6\u6ca1\u597d\u4e8b\u3002\u8fd9\u6b21\u4e5f\u4e0d\u4f8b\u5916\uff0c\u4ed6\u558a\u6211\u5e76\u5411\u6211\u629b\u4e86\u4e00\u4e2a\u5f02\u5e38\uff1ajava.sql.SQLException: No value specified for parameter 3\u3002\u5e76\u8ba9\u6211\u5e2e\u4ed6\u68c0\u67e5\u4ee3\u7801\uff0c\u6211\u57fa\u672c\u4e0a\u90fd\u662f\u975e\u5e38\u4e50\u610f\u7684\u3002\u56e0\u4e3a\u5e2e\u52a9\u522b\u4eba\u89e3\u51b3\u95ee\u9898\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4e5f\u80fd\u6536\u83b7\u66f4\u591a\u3002\u4e0d\u5149\u662f\u53cb\u8c0a\uff0c\u5feb\u4e50\u548c\u77e5\u8bc6\u89c1\u8bc6\u589e\u957f\u4e5f\u662f\u5176\u4e2d\u7684\u4e00\u90e8\u5206\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u770b\u4e86\u4ed6\u7684\u4ee3\u7801\uff0c\u5185\u5bb9\u7b80\u5316\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Component(\"xttblogDao\")\npublic class XttblogDaoImpl implements XttblogDao {\n    @Autowired\n    private JdbcTemplate jdbcTemplate;\n    @Override\n    public List&lt;Xttblog&gt; selectByDynamicParams(List&lt;Integer&gt; idList, String name) {\n        StringBuffer sql = new StringBuffer();\n        sql.append(\"select * from xttblog where id in ( \");\n        \/\/ \u62fc\u88c5SQL\n        List&lt;String&gt; stringList = new ArrayList&lt;&gt;();\n        for (int i = 0;i &lt; idList.size();i++){\n            stringList.add(\"?\");\n        }\n        sql.append(String.join(\",\",stringList));\n        sql.append(\" ) and name = ?\");\n        System.err.println(sql.toString());\n        return jdbcTemplate.query(sql.toString(),new XttblogMapper(),idList,name);\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7136\u540e\u6d4b\u8bd5\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u53d1\u73b0\u6700\u7ec8\u62a5\u9519\uff1ajava.sql.SQLException: No value specified for parameter 3\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1g2cltolwstj20gn01e3yp.jpg\" alt=\"\" \/><figcaption>java.sql.SQLException: No value specified for parameter 3<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u65f6\u95f4\uff0c\u6211\u4ed4\u7ec6\u770b\u4e86\u770b SQL\uff0c\u6ca1\u95ee\u9898\u3002\u7136\u540e\u628a\u201c ) and name = ?\u201d\u8fd9\u90e8\u5206\u53bb\u6389\uff0c\u4e5f\u5c31\u662f\u53bb\u6389\u6700\u540e\u4e00\u4e2a\u67e5\u8be2\u53c2\u6570\u3002\u53d1\u73b0 OK \u4e86\uff0c\u4e0d\u62a5\u9519\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u662f\u6211\u53c8\u60f3\u5230\u4e86 Spring \u63d0\u4f9b\u7684\u53e6\u5916\u4e00\u4e2a\u7c7b\uff1aNamedParameterJdbcTemplate\u3002\u7136\u540e\u6539\u9020\u4ee3\u7801\uff0c\u4f7f\u7528 NamedParameterJdbcTemplate \u67e5\u8be2\u4e00\u4e0b\u8bd5\u4e00\u8bd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u4ece\u5bb9\u5668\u4e2d\u5c06dataSource\u8fd9\u4e2a\u5bf9\u8c61\u6ce8\u5165\u8fdb\u6765\n@Autowired\nprivate DataSource dataSource;\n@Bean\npublic NamedParameterJdbcTemplate namedParameterJdbcTemplate(){\n    \/\/ \u8981\u6784\u9020NamedParameterJdbcTemplate\u5bf9\u8c61\u9700\u8981\u4f9d\u8d56dataSource\n    return new NamedParameterJdbcTemplate(dataSource);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u540c\u6837\u7684\u4ee3\u7801\uff0c\u6362\u4e00\u4e2a\u7c7b\u5c31\u5b8c\u5168\u652f\u6301\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9664\u6b64\u4e4b\u5916\uff0cJdbcTemplate \u8fd8\u6709\u53e6\u5916\u4e00\u4e2a\u95ee\u9898\u3002\u6bd4\u5982\uff0c\u6211\u4eec\u4f7f\u7528 SQL Server \u7684\u5206\u9875 top \u65f6\uff0c\u540c\u6837\u65e0\u6cd5\u4f7f\u7528 JdbcTemplate\u3002\u56e0\u4e3a JdbcTemplate \u53ea\u80fd\u5c06\u95ee\u53f7\u7528\u4e8ewhere\u8bed\u53e5\u4e2d\u7684\u53c2\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5173\u4e8e JDBC \u7684\u64cd\u4f5c\uff0cSpring \u63d0\u4f9b\u4e86 3 \u4e2a\u6a21\u677f\u7c7b\u4f9b\u6211\u4eec\u9009\u62e9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>JdbcTemplate\uff1a\u63d0\u4f9b\u6309\u7167\u7d22\u5f15\u53c2\u6570\u4f20\u9012\uff0c\u7528 in \u7684\u65f6\u5019\u8981\u7279\u522b\u6ce8\u610f\uff0c\u63a8\u8350\u4f7f\u7528 NamedParameterJdbcTemplate\u3002 <\/li><li>NamedParameterJdbcTemplate\uff1a\u6309\u7167\u540d\u5b57\u53c2\u6570\u4f20\u9012 <\/li><li>SimpleJdbcTemplate\uff1a\u652f\u6301Java5\u7279\u6027\uff0c\u5982\u81ea\u52a8\u88c5\u7bb1\u3001\u6cdb\u578b\u548c\u53ef\u53d8\u53c2\u6570\u5217\u8868\u7b49 <\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">JdbcTemplate \u7684\u5904\u7406\u903b\u8f91\u662f\u4e00\u4e2a \uff1f\u4e00\u4e2a\u53c2\u8d5b\u3002\u800c in \u5f80\u5f80\u6211\u4eec\u53ef\u80fd\u53ea\u7559\u4e86\u4e00\u4e2a \uff1f\u3002NamedParameterJdbcTemplate \u5728\u5904\u7406 sql \u4e2d in \u8303\u56f4\u67e5\u8be2\uff0c\u4f1a\u6839\u636e\u5b9e\u9645\u4f20\u5165\u53c2\u6570\u7684 List \u4e2a\u6570\uff0c\u505a\u4e86\u4e00\u4e9b\u4fee\u6539\uff0c\u800c\u539f\u751f\u7684 JdbcTemplate \u5219\u4e0d\u4f1a\u8fd9\u4e48\u505a\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0a\u8fd9\u4e9b\u5185\u5bb9\uff0c\u5e0c\u671b\u80fd\u591f\u5e2e\u52a9\u5927\u5bb6\u6392\u9664\u4e00\u4e9b\u5751\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=4140\">\u9762\u8bd5\u5b98\uff1a\u4f7f\u7528JdbcTemplate in \u8bed\u53e5\u4f60\u9047\u5230\u8fc7\u54ea\u4e9b\u5751\uff1f<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\uff0c\u540c\u4e8b\u5728\u7a7a\u95f2\u65f6\u95f4\u5199\u4e86\u4e00\u4e2a Spring \u7684 demo\uff0c\u9047\u5230\u4e86\u4e00\u4e2a\u5173\u4e8e JdbcTemplate \u7684\u5c0f\u5751\u3002\u4eca [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1962,551,587,588,1350],"class_list":["post-4140","post","type-post","status-publish","format-standard","hentry","category-java","tag-jdbctemplate","tag-spring","tag-spring-boot","tag-spring-cloud","tag-spring-mvc"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4140"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4140\/revisions"}],"predecessor-version":[{"id":4141,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4140\/revisions\/4141"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}