{"id":14169,"date":"2023-12-04T00:21:37","date_gmt":"2023-12-03T16:21:37","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=14169"},"modified":"2023-12-04T00:23:16","modified_gmt":"2023-12-03T16:23:16","slug":"mybatis-he-de-qu-bie-shi-shen-me","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/04\/mybatis-he-de-qu-bie-shi-shen-me\/","title":{"rendered":"MyBatis #{} \u548c ${} \u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"<p>\u5728 MyBatis \u4e2d\uff0c<code>#{}<\/code> \u548c <code>${}<\/code> \u662f\u7528\u4e8e\u5728 SQL \u8bed\u53e5\u4e2d\u4f20\u9012\u53c2\u6570\u7684\u4e24\u79cd\u4e0d\u540c\u7684\u5360\u4f4d\u7b26\u65b9\u5f0f\uff0c\u5b83\u4eec\u6709\u4ee5\u4e0b\u533a\u522b\uff1a<\/p>\n<h3><a id=\"\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a><code>#{}<\/code><\/h3>\n<ul>\n<li><strong>\u5b89\u5168\u6027\u9ad8\uff1a<\/strong> <code>#{}<\/code> \u4e2d\u7684\u503c\u4f1a\u88ab\u9884\u7f16\u8bd1\u6210\u5360\u4f4d\u7b26\uff0cMyBatis \u4f1a\u4f7f\u7528 PreparedStatement\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u53c2\u6570\u8bbe\u7f6e\u5360\u4f4d\u7b26\uff0c\u53ef\u4ee5\u9632\u6b62 SQL \u6ce8\u5165\u653b\u51fb\u3002<\/li>\n<li><strong>\u53c2\u6570\u66ff\u6362\uff1a<\/strong> <code>#{}<\/code> \u80fd\u591f\u5c06\u4f20\u5165\u7684\u53c2\u6570\u6309\u7167\u9884\u5b9a\u4e49\u7684 JDBC \u7c7b\u578b\u8fdb\u884c\u5904\u7406\u548c\u8f6c\u6362\uff0c\u901a\u5e38\u7528\u4e8e\u8bbe\u7f6e SQL \u7684\u53c2\u6570\uff0c\u4f8b\u5982 <code>WHERE column = #{value}<\/code>\u3002<\/li>\n<\/ul>\n<h3><a id=\"\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a><code>${}<\/code><\/h3>\n<ul>\n<li><strong>\u5b57\u7b26\u4e32\u66ff\u6362\uff1a<\/strong> <code>${}<\/code> \u4e2d\u7684\u503c\u4f1a\u76f4\u63a5\u66ff\u6362\u6210\u4f20\u5165\u7684\u503c\uff0c\u76f4\u63a5\u62fc\u63a5\u5230 SQL \u8bed\u53e5\u4e2d\uff0c\u4e0d\u8fdb\u884c\u9884\u7f16\u8bd1\uff0c\u53ef\u80fd\u5bfc\u81f4 SQL \u6ce8\u5165\u7684\u98ce\u9669\u3002<\/li>\n<li><strong>\u7528\u9014\uff1a<\/strong> <code>${}<\/code> \u901a\u5e38\u7528\u4e8e\u52a8\u6001\u4f20\u9012 SQL \u8bed\u53e5\u7684\u8868\u540d\u3001\u5b57\u6bb5\u540d\u7b49\u4e0d\u9700\u8981\u9884\u7f16\u8bd1\u7684\u90e8\u5206\uff0c\u5982 <code>SELECT * FROM ${tableName}<\/code>\u3002<\/li>\n<\/ul>\n<h3><a id=\"%E7%A4%BA%E4%BE%8B%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u793a\u4f8b\uff1a<\/h3>\n<p>\u5047\u8bbe\u6709\u4e00\u4e2a\u53c2\u6570 <code>name = &quot;John&quot;<\/code>\u3002<\/p>\n<ul>\n<li>\u4f7f\u7528 <code>#{}<\/code>\uff1a<code>WHERE name = #{name}<\/code> \u4f1a\u5c06 <code>name<\/code> \u4f5c\u4e3a\u9884\u7f16\u8bd1\u53c2\u6570\uff0c\u5b89\u5168\u6027\u9ad8\uff0c\u751f\u6210\u7684 SQL \u4e3a <code>WHERE name = ?<\/code>\u3002<\/li>\n<li>\u4f7f\u7528 <code>${}<\/code>\uff1a<code>WHERE name = '${name}'<\/code> \u4f1a\u76f4\u63a5\u66ff\u6362\u6210\u5b57\u7b26\u4e32\uff0c\u751f\u6210\u7684 SQL \u4e3a <code>WHERE name = 'John'<\/code>\uff0c\u5b58\u5728 SQL \u6ce8\u5165\u7684\u98ce\u9669\u3002<\/li>\n<\/ul>\n<p>\u5728\u5b9e\u9645\u4f7f\u7528\u65f6\uff0c\u63a8\u8350\u4f7f\u7528 <code>#{}<\/code>\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u5b89\u5168\u6027\u548c\u9884\u7f16\u8bd1\u529f\u80fd\uff0c\u6709\u52a9\u4e8e\u907f\u514d\u6f5c\u5728\u7684\u5b89\u5168\u98ce\u9669\u3002\u800c <code>${}<\/code> \u5219\u66f4\u9002\u5408\u4e8e\u90a3\u4e9b\u4e0d\u9700\u8981\u9884\u7f16\u8bd1\u7684\u573a\u666f\uff0c\u5982\u8868\u540d\u3001\u5217\u540d\u7b49\u52a8\u6001\u90e8\u5206\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 MyBatis \u4e2d\uff0c#{} \u548c ${} \u662f\u7528\u4e8e\u5728 SQL \u8bed\u53e5\u4e2d\u4f20\u9012\u53c2\u6570\u7684\u4e24\u79cd\u4e0d\u540c\u7684\u5360\u4f4d\u7b26\u65b9\u5f0f\uff0c\u5b83\u4eec\u6709\u4ee5\u4e0b [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[113],"tags":[],"class_list":["post-14169","post","type-post","status-publish","format-standard","hentry","category-mybatis"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/14169","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\/11"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=14169"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/14169\/revisions"}],"predecessor-version":[{"id":14170,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/14169\/revisions\/14170"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=14169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=14169"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=14169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}