{"id":7175,"date":"2024-11-14T14:29:03","date_gmt":"2024-11-14T06:29:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7175\/"},"modified":"2024-11-14T14:29:03","modified_gmt":"2024-11-14T06:29:03","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:\/\/fwq.ai\/blog\/7175\/","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><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173059201816953.jpg\" class=\"aligncenter\" title=\"MyBatis\u52a8\u6001SQL\u62a5\u9519\u201cbadSql\u201d\uff0c\u5982\u4f55\u4fee\u6539SQL\u8bed\u53e5\u4f7f\u5176\u6b63\u786e\u6267\u884c\uff1f\u63d2\u56fe\" alt=\"MyBatis\u52a8\u6001SQL\u62a5\u9519\u201cbadSql\u201d\uff0c\u5982\u4f55\u4fee\u6539SQL\u8bed\u53e5\u4f7f\u5176\u6b63\u786e\u6267\u884c\uff1f\u63d2\u56fe\" \/><\/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&#8221;badsql&#8221;\uff0c\u53ef\u80fd\u7684\u539f\u56e0\u662f\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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 \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>\u4ee5\u4e0a\u5c31\u662fMyBatis\u52a8\u6001SQL\u62a5\u9519\u201cbadSql\u201d\uff0c\u5982\u4f55\u4fee\u6539SQL\u8bed\u53e5\u4f7f\u5176\u6b63\u786e\u6267\u884c\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>mybatis\u52a8\u6001sql\u62a5\u9519\u5f81\u89e3 \u5728\u4f7f\u7528mybatis\u8fdb\u884c\u52a8\u6001sql\u64cd\u4f5c\u65f6\uff0c\u9047\u5230&#8221;badsql&#8221;\uff0c\u53ef\u80fd\u7684\u539f\u56e0\u662f\u5b58\u5728\u8bed\u6cd5\u9519\u8bef\u3002 \u9488\u5bf9\u63d0\u4f9b\u7684sql\u8bed\u53e5\uff1a select * from table a &lt;where&gt; a.project_id=#{projectid} and a.id != #{id} and a.status=3 &lt;choose&gt; &lt;when test=&#8221;type == idcard&#8221;&gt; and a.id_card = #{code} &lt;\/when&gt; &lt;when test=&#8221;type == unitcode&#8221;&gt;and a.unit_code = #{code}&lt;\/when&gt; &lt;otherwise&gt; &lt;\/otherwise&gt; &lt;\/choose&gt; &lt;\/where&gt; \u767b\u5f55\u540e\u590d\u5236 \u4fee\u6539\u540e\uff0c\u6b63\u786e\u7684sql\u8bed\u53e5\u5e94\u8be5\u5982\u4e0b\uff1a select * from table a &lt;where&gt; a.project_id=#{projectId} and a.id != #{id} and a.status=3 &lt;choose&gt; &lt;when test=&#8221;type [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-7175","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=7175"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7175\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}