{"id":1960,"date":"2023-04-01T10:25:03","date_gmt":"2023-04-01T02:25:03","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1960"},"modified":"2023-04-22T08:37:49","modified_gmt":"2023-04-22T00:37:49","slug":"mybatis-dynamically-passes-table-name-and-field-name-parameters-solution-used-for-table-partitioning-table-names-are-dynamic","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/mybatis-dynamically-passes-table-name-and-field-name-parameters-solution-used-for-table-partitioning-table-names-are-dynamic\/","title":{"rendered":"MyBatis\u52a8\u6001\u4f20\u5165\u8868\u540d\uff0c\u5b57\u6bb5\u540d\u53c2\u6570\u7684\u89e3\u51b3\u529e\u6cd5&#8211;\u7528\u4e8e\u5206\u8868&#8211;\u8868\u540d\u662f\u52a8\u6001\u7684"},"content":{"rendered":"<h2>\u80cc\u666f\u63cf\u8ff0<\/h2>\n<p>\u4e00\u76f4\u5728\u4f7f\u7528MyBatis\u8fd9\u4e2aORM\u6846\u67b6\uff0c\u90fd\u662f\u4f7f\u7528MyBatis\u91cc\u7684\u4e00\u4e9b\u5e38\u7528\u529f\u80fd\u3002\u4eca\u5929\u5728\u9879\u76ee\u5f00\u53d1\u4e2d\u6709\u4e2a\u4e1a\u52a1\u662f\u9700\u8981\u9650\u5236\u5404\u4e2a\u7528\u6237\u5bf9\u67d0\u4e9b\u8868\u91cc\u7684\u5b57\u6bb5\u67e5\u8be2\u4ee5\u53ca\u67d0\u4e9b\u5b57\u6bb5\u662f\u5426\u663e\u793a\uff0c\u5982\u67d0\u5f20\u8868\u7684\u67d0\u4e9b\u5b57\u6bb5\u4e0d\u8ba9\u7528\u6237\u67e5\u8be2\u5230\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5c31\u9700\u8981\u6784\u5efasql\u6765\u52a8\u6001\u4f20\u5165\u8868\u540d\u3001\u5b57\u6bb5\u540d\u4e86\u3002\u73b0\u5728\u5bf9\u89e3\u51b3\u65b9\u6cd5\u8fdb\u884c\u4e0b\u603b\u7ed3\uff0c\u5e0c\u671b\u5bf9\u9047\u5230\u540c\u6837\u95ee\u9898\u7684\u4f19\u4f34\u6709\u4e9b\u5e2e\u52a9\u3002<\/p>\n<p><!-- more --><\/p>\n<p>\u52a8\u6001SQL\u662fMyBatis\u7684\u5f3a\u5927\u7279\u6027\u4e4b\u4e00\uff0cMyBatis\u5728\u5bf9sql\u8bed\u53e5\u8fdb\u884c\u9884\u7f16\u8bd1\u4e4b\u524d\uff0c\u4f1a\u5bf9sql\u8fdb\u884c\u52a8\u6001\u89e3\u6790\uff0c\u89e3\u6790\u4e3a\u4e00\u4e2aBoundSql\u5bf9\u8c61\uff0c\u4e5f\u662f\u5728\u6b64\u5904\u5bf9\u52a8\u6001sql\u8fdb\u884c\u5904\u7406\u3002\u4e0b\u9762\u8ba9\u6211\u4eec\u5148\u6765\u719f\u6089\u4e0bMyBatis\u91cc#{}\u4e0e${}\u7684\u7528\u6cd5\uff1a<\/p>\n<p>\u5728\u52a8\u6001sql\u89e3\u6790\u8fc7\u7a0b\uff0c<code>#{}<\/code>\u4e0e<code>${}<\/code>\u7684\u6548\u679c\u662f\u4e0d\u4e00\u6837\u7684\uff1a<\/p>\n<p>\uff081\uff09<code>#{}<\/code>\u89e3\u6790\u4e3a\u4e00\u4e2a JDBC \u9884\u7f16\u8bd1\u8bed\u53e5\uff08prepared statement\uff09\u7684\u53c2\u6570\u6807\u8bb0\u7b26<\/p>\n<p>\u5982\u4ee5\u4e0bsql\u8bed\u53e5<\/p>\n<pre><code class=\"language-sql\">select * from user where name = #{name};<\/code><\/pre>\n<p>\u4f1a\u88ab\u89e3\u6790\u4e3a\uff1a<\/p>\n<pre><code class=\"language-sql\">select * from user where name = ?;<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230<code>#{}<\/code>\u88ab\u89e3\u6790\u4e3a\u4e00\u4e2a\u53c2\u6570\u5360\u4f4d\u7b26<code>?<\/code><\/p>\n<p>\uff082\uff09<code>${}<\/code>\u4ec5\u4ec5\u4e3a\u4e00\u4e2a\u7eaf\u788e\u7684 string \u66ff\u6362\uff0c\u5728\u52a8\u6001 SQL \u89e3\u6790\u9636\u6bb5\u5c06\u4f1a\u8fdb\u884c\u53d8\u91cf\u66ff\u6362<\/p>\n<p>\u5982\u4ee5\u4e0bsql\u8bed\u53e5\uff1a<\/p>\n<pre><code class=\"language-sql\">select * from user where name = ${name};<\/code><\/pre>\n<p>\u5f53\u6211\u4eec\u4f20\u9012\u53c2\u6570&quot;sprite&quot;\u65f6\uff0csql\u4f1a\u89e3\u6790\u4e3a\uff1a<\/p>\n<pre><code class=\"language-sql\">select * from user where name = &quot;sprite&quot;;<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\u9884\u7f16\u8bd1\u4e4b\u524d\u7684sql\u8bed\u53e5\u5df2\u7ecf\u4e0d\u5305\u542b\u53d8\u91cfname\u4e86<\/p>\n<p>\u7efc\u4e0a\u6240\u5f97\uff0c<code>${}<\/code>\u7684\u53d8\u91cf\u66ff\u6362\u9636\u6bb5\u662f\u5728\u52a8\u6001 SQL \u89e3\u6790\u9636\u6bb5\uff0c\u800c<code>#{}<\/code>\u7684\u53d8\u91cf\u66ff\u6362\u662f\u5728 DBMS \u4e2d<\/p>\n<p><strong><code>#{}<\/code>\u4e0e<code>${}<\/code>\u7684\u533a\u522b\u53ef\u4ee5\u7b80\u5355\u603b\u7ed3\u5982\u4e0b<\/strong>\uff1a<\/p>\n<ul>\n<li><code>#{}<\/code>\u5c06\u4f20\u5165\u7684\u53c2\u6570\u5f53\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4f1a\u7ed9\u4f20\u5165\u7684\u53c2\u6570\u52a0\u4e00\u4e2a\u53cc\u5f15\u53f7<\/li>\n<li><code>${}<\/code>\u5c06\u4f20\u5165\u7684\u53c2\u6570\u76f4\u63a5\u663e\u793a\u751f\u6210\u5728sql\u4e2d\uff0c\u4e0d\u4f1a\u6dfb\u52a0\u5f15\u53f7<\/li>\n<li><code>#{}<\/code>\u80fd\u591f\u5f88\u5927\u7a0b\u5ea6\u4e0a\u9632\u6b62sql\u6ce8\u5165\uff0c<code>${}<\/code>\u65e0\u6cd5\u9632\u6b62sql\u6ce8\u5165<\/li>\n<\/ul>\n<p><code>${}<\/code>\u5728\u9884\u7f16\u8bd1\u4e4b\u524d\u5df2\u7ecf\u88ab\u53d8\u91cf\u66ff\u6362\u4e86\uff0c\u8fd9\u4f1a\u5b58\u5728sql\u6ce8\u5165\u7684\u98ce\u9669\u3002\u5982\u4e0bsql<\/p>\n<pre><code class=\"language-sql\">select * from ${tableName} where name = ${name}<\/code><\/pre>\n<p>\u5982\u679c\u4f20\u5165\u7684\u53c2\u6570tableName\u4e3a<code>user; delete user; --<\/code>\uff0c\u90a3\u4e48sql\u52a8\u6001\u89e3\u6790\u4e4b\u540e\uff0c\u9884\u7f16\u8bd1\u4e4b\u524d\u7684sql\u5c06\u53d8\u4e3a\uff1a<\/p>\n<pre><code class=\"language-sql\">select * from user; delete user; -- where name = ?;<\/code><\/pre>\n<p><code>--<\/code>\u4e4b\u540e\u7684\u8bed\u53e5\u5c06\u4f5c\u4e3a\u6ce8\u91ca\u4e0d\u8d77\u4f5c\u7528\uff0c\u987f\u65f6\u6211\u548c\u6211\u7684\u5c0f\u4f19\u4f34\u60ca\u5446\u4e86\uff01\uff01\uff01\u770b\u5230\u6ca1\uff0c\u672c\u6765\u7684\u67e5\u8be2\u8bed\u53e5\uff0c\u7adf\u7136\u5077\u5077\u7684\u5305\u542b\u4e86\u4e00\u4e2a\u5220\u9664\u8868\u6570\u636e\u7684sql\uff0c\u662f\u5220\u9664\uff0c\u5220\u9664\uff0c\u5220\u9664\uff01\uff01\uff01\u91cd\u8981\u7684\u4e8b\u60c5\u8bf4\u4e09\u904d\uff0c\u53ef\u60f3\u800c\u77e5\uff0c\u8fd9\u4e2a\u98ce\u9669\u662f\u6709\u591a\u5927\u3002<\/p>\n<ul>\n<li><code>${}<\/code>\u4e00\u822c\u7528\u4e8e\u4f20\u8f93\u6570\u636e\u5e93\u7684\u8868\u540d\u3001\u5b57\u6bb5\u540d\u7b49<\/li>\n<li>\u80fd\u7528<code>#{}<\/code>\u7684\u5730\u65b9\u5c3d\u91cf\u522b\u7528<code>${}<\/code><\/li>\n<\/ul>\n<h2>MyBatis\u52a8\u6001\u4f20\u5165\u5b57\u6bb5\u540d\u5b9e\u73b0<\/h2>\n<p>\u8fdb\u5165\u6b63\u9898\uff0c\u901a\u8fc7\u4e0a\u9762\u7684\u5206\u6790\uff0c\u76f8\u4fe1\u5927\u5bb6\u53ef\u80fd\u5df2\u7ecf\u5bf9\u5982\u4f55\u52a8\u6001\u8c03\u7528\u8868\u540d\u548c\u5b57\u6bb5\u540d\u6709\u4e9b\u601d\u8def\u4e86\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-sql\">&lt;select id=&quot;getUser&quot; resultType=&quot;java.util.Map&quot; parameterType=&quot;java.lang.String&quot; statementType=&quot;STATEMENT&quot;&gt;\n  select \n    ${columns}\n  from ${tableName}\n  where COMPANY_REMARK = ${company}\n&lt;\/select&gt;<\/code><\/pre>\n<p>\u8981\u5b9e\u73b0\u52a8\u6001\u8c03\u7528\u8868\u540d\u548c\u5b57\u6bb5\u540d\uff0c\u5c31\u4e0d\u80fd\u4f7f\u7528\u9884\u7f16\u8bd1\u4e86\uff0c\u9700\u6dfb\u52a0<code>statementType=&quot;STATEMENT&quot;<\/code><\/p>\n<ul>\n<li><code>statementType<\/code>\uff1a<code>STATEMENT<\/code>\uff08\u975e\u9884\u7f16\u8bd1\uff09\uff0c<code>PREPARED<\/code>\uff08\u9884\u7f16\u8bd1\uff09\u6216<code>CALLABLE<\/code>\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\uff0c\u8fd9\u5c31\u544a\u8bc9 MyBatis \u5206\u522b\u4f7f\u7528<code>Statement<\/code>\uff0c<code>PreparedStatement<\/code>\u6216\u8005<code>CallableStatement<\/code>\u3002\u9ed8\u8ba4\uff1a<code>PREPARED<\/code>\u3002\u8fd9\u91cc\u663e\u7136\u4e0d\u80fd\u4f7f\u7528\u9884\u7f16\u8bd1\uff0c\u8981\u6539\u6210\u975e\u9884\u7f16\u8bd1<\/li>\n<li>\u5176\u6b21\uff0csql\u91cc\u7684\u53d8\u91cf\u53d6\u503c\u662f<code>${xxx}<\/code>\uff0c\u4e0d\u662f<code>#{xxx}<\/code><\/li>\n<li>\u56e0\u4e3a<code>${}<\/code>\u662f\u5c06\u4f20\u5165\u7684\u53c2\u6570\u76f4\u63a5\u663e\u793a\u751f\u6210sql\uff0c\u5982<code>${xxx}<\/code>\u4f20\u5165\u7684\u53c2\u6570\u4e3a\u5b57\u7b26\u4e32\u6570\u636e\uff0c\u9700\u5728\u53c2\u6570\u4f20\u5165\u524d\u52a0\u4e0a\u5f15\u53f7\uff0c\u5982\uff1a<\/li>\n<\/ul>\n<pre><code class=\"language-java\">String name = &quot;sprite&quot;;\nname = &quot;&#039;&quot; + name + &quot;&#039;&quot;;<\/code><\/pre>\n<p>MyBatis\u52a8\u6001\u8c03\u7528\u8868\u540d\u548c\u5b57\u6bb5\u540d\uff0c\u8fd8\u53ef\u4ee5\u5e94\u7528\u4e8e\u65e5\u5fd7\u7684\u6536\u96c6\u4e0a\uff0c\u5982\u6570\u636e\u5e93\u7684\u65e5\u5fd7\u8868\uff0c\u6bcf\u9694\u4e00\u4e2a\u6708\u52a8\u6001\u5efa\u4e00\u4e2a\u65e5\u5fd7\u8868\uff0c\u8868\u540d\u524d\u7f00\u76f8\u540c\uff08\u5982log_201610,log_201611\u7b49\uff09\uff0c\u8fd9\u6837\u5b9e\u73b0\u65e5\u5fd7\u7684\u5206\u6708\u5206\u8868\u5b58\u50a8\uff0c\u65b9\u4fbf\u65e5\u5fd7\u7684\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f\u63cf\u8ff0 \u4e00\u76f4\u5728\u4f7f\u7528MyBatis\u8fd9\u4e2aORM\u6846\u67b6\uff0c\u90fd\u662f\u4f7f\u7528MyBatis\u91cc\u7684\u4e00\u4e9b\u5e38\u7528\u529f\u80fd\u3002\u4eca\u5929\u5728\u9879\u76ee\u5f00\u53d1\u4e2d\u6709\u4e2a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-1960","post","type-post","status-publish","format-standard","hentry","category-mybatis"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1960","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1960"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1960\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}