{"id":200825,"date":"2025-05-06T12:33:19","date_gmt":"2025-05-06T04:33:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200825\/"},"modified":"2025-05-06T12:33:19","modified_gmt":"2025-05-06T04:33:19","slug":"mybatis%e5%8a%a8%e6%80%81sql%e6%8a%a5%e9%94%99badsql%ef%bc%8c%e5%a6%82%e4%bd%95%e4%bf%ae%e6%94%b9sql%e8%af%ad%e5%8f%a5%e4%bd%bf%e5%85%b6%e6%ad%a3%e7%a1%ae%e6%89%a7%e8%a1%8c%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200825\/","title":{"rendered":"MyBatis\u52a8\u6001SQL\u62a5\u9519\u201cbadSql\u201d\uff0c\u5982\u4f55\u4fee\u6539SQL\u8bed\u53e5\u4f7f\u5176\u6b63\u786e\u6267\u884c\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>MyBatis\u52a8\u6001SQL\u62a5\u9519\u201cbadSql\u201d\uff0c\u5982\u4f55\u4fee\u6539SQL\u8bed\u53e5\u4f7f\u5176\u6b63\u786e\u6267\u884c\uff1f<\/h1>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241116\/17317163276737e4e77e6e2.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>mybatis\u52a8\u6001sql\u62a5\u9519\u5f81\u89e3<\/strong><\/p>\n<p>\u5728\u4f7f\u7528mybatis\u8fdb\u884c\u52a8\u6001sql\u64cd\u4f5c\u65f6\uff0c\u9047\u5230\u62a5\u9519\u63d0\u793a&#8221;badsql&#8221;\uff0c\u53ef\u80fd\u7684\u539f\u56e0\u662fsql\u8bed\u53e5\u5b58\u5728\u8bed\u6cd5\u9519\u8bef\u3002<\/p>\n<p>\u9488\u5bf9\u63d0\u4f9b\u7684sql\u8bed\u53e5\uff1a<\/p>\n<pre>select * from table a \n&lt;where&gt;\n a.project_id=#{projectid}\n and a.id != #{id}\n and a.status=3 \n&lt;choose&gt;\n    &lt;when test=\"type == idcard\"&gt; \n        and a.id_card = #{code}\n    &lt;\/when&gt;\n&lt;when test=\"type == unitcode\"&gt;and a.unit_code = #{code}&lt;\/when&gt;\n    &lt;otherwise&gt;  \n    &lt;\/otherwise&gt;  \n&lt;\/choose&gt;\n&lt;\/where&gt;<\/pre>\n<p>\u4fee\u6539\u540e\uff0c\u6b63\u786e\u7684sql\u8bed\u53e5\u5e94\u8be5\u5982\u4e0b\uff1a<\/p>\n<pre>select * from table a\n&lt;where&gt;\n a.project_id=#{projectId}\n and a.id != #{id}\n and a.status=3 \n&lt;choose&gt;\n    &lt;when test=\"type == 'idCard'\"&gt; \n        and a.id_card = #{code}\n    &lt;\/when&gt;\n    &lt;when test=\"type == 'unitCode'\"&gt; \n        and a.unit_code = #{code}\n    &lt;\/when&gt;\n    &lt;otherwise&gt;  \n    &lt;\/otherwise&gt;  \n&lt;\/choose&gt;\n&lt;\/where&gt;<\/pre>\n<p>\u4fee\u6539\u540e\u7684\u90e8\u5206\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u5c06test=&#8217;type == idcard&#8217;\u4fee\u6539\u4e3atest=&#8221;type == &#8216;idcard'&#8221;\uff0c\u6dfb\u52a0\u4e86\u5f15\u53f7\u3002<\/li>\n<li>\u5c06test=&#8217;type == unitcode&#8217;\u4fee\u6539\u4e3atest=&#8221;type == &#8216;unitcode'&#8221;\uff0c\u6dfb\u52a0\u4e86\u5f15\u53f7\u3002<\/li>\n<li>\u6dfb\u52a0\u4e86&lt;otherwise&gt;&lt;\/otherwise&gt;\u8bed\u53e5\u5757\u3002<\/li>\n<\/ul>\n<p>\u4fee\u6539\u540e\uff0csql\u8bed\u53e5\u7684\u8bed\u6cd5\u6b63\u786e\uff0c\u53ef\u4ee5\u6b63\u5e38\u6267\u884c\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aMyBatis\u52a8\u6001SQL\u62a5\u9519\u201cbadSql\u201d\uff0c\u5982\u4f55\u4fee\u6539SQL\u8bed\u53e5\u4f7f\u5176\u6b63\u786e\u6267\u884c\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>MyBatis\u52a8\u6001SQL\u62a5\u9519\u201cb&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200825","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200825","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=200825"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200825\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}