{"id":200435,"date":"2025-05-06T08:11:19","date_gmt":"2025-05-06T00:11:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200435\/"},"modified":"2025-05-06T08:11:19","modified_gmt":"2025-05-06T00:11:19","slug":"mybatis%e5%8a%a8%e6%80%81sql%e4%bc%98%e5%8c%96%ef%bc%9a%e5%a6%82%e4%bd%95%e9%81%bf%e5%85%8d%e6%8b%bc%e6%8e%a5%e9%94%99%e8%af%af%e5%af%bc%e8%87%b4%e6%9f%a5%e8%af%a2%e6%8a%a5%e9%94%99%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200435\/","title":{"rendered":"Mybatis\u52a8\u6001SQL\u4f18\u5316\uff1a\u5982\u4f55\u907f\u514d\u62fc\u63a5\u9519\u8bef\u5bfc\u81f4\u67e5\u8be2\u62a5\u9519\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Mybatis\u52a8\u6001SQL\u4f18\u5316\uff1a\u5982\u4f55\u907f\u514d\u62fc\u63a5\u9519\u8bef\u5bfc\u81f4\u67e5\u8be2\u62a5\u9519\uff1f<\/h1>\n<p>\u76ee\u524d\u4e3b\u673a\u5b9d\u8d1d\u4e0a\u5df2\u7ecf\u6709\u5f88\u591a\u5173\u4e8e\u7684\u6587\u7ae0\u4e86\uff0c\u81ea\u5df1\u5728\u521d\u6b21\u9605\u8bfb\u8fd9\u4e9b\u6587\u7ae0\u4e2d\uff0c\u4e5f\u89c1\u8bc6\u5230\u4e86\u5f88\u591a\u5b66\u4e60\u601d\u8def\uff1b\u90a3\u4e48\u672c\u6587\uff0c\u4e5f\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff0c\u5982\u679c\u9605\u8bfb\u5b8c\u540e\u771f\u7684\u5bf9\u4f60\u5b66\u4e60\u6709\u5e2e\u52a9\uff0c\u6b22\u8fce\u52a8\u52a8\u624b\u6307\uff0c\u8bc4\u8bba\u7559\u8a00\u5e76\u5206\u4eab~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241105\/17307707946729776a64c5d.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>mybatis\u52a8\u6001sql\u4f18\u5316\u62a5\u9519<\/strong><\/p>\n<p>\u5728mybatis\u4e2d\u8fdb\u884c\u52a8\u6001sql\u67e5\u8be2\u65f6\uff0c\u7ecf\u5e38\u4f1a\u9047\u5230sql\u62fc\u63a5\u4e0d\u5f53\uff0c\u5bfc\u81f4\u67e5\u8be2\u62a5\u9519\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5178\u578b\u7684\u62a5\u9519\uff1a<\/p>\n<pre>select * from table a where a.project_id=#{projectid} and a.id != #{id} and a.status=3 and a.id_card = #{code} or a.unit_code = #{code}<\/pre>\n<p>\u9488\u5bf9\u8be5\u95ee\u9898\uff0c\u6709\u51e0\u79cd\u5e38\u89c1\u7684\u4f18\u5316\u65b9\u6cd5\uff1a<\/p>\n<p>\u65b9\u6cd51\uff1a\u4f7f\u7528&lt;if&gt;\u6807\u7b7e<\/p>\n<pre>&lt;pre&gt;&lt;code&gt;&amp;lt;if test=&amp;quot;type == 'idcard'&amp;quot;&amp;gt;a.id_card = #{code}&amp;lt;\/if&amp;gt;\n&amp;lt;if test=&amp;quot;type == 'unitcode'&amp;quot;&amp;gt;a.unit_code = #{code}&amp;lt;\/if&amp;gt;&lt;\/code&gt;&lt;\/pre&gt;<\/pre>\n<p>\u65b9\u6cd52\uff1a\u4f7f\u7528&lt;choose&gt;\u6807\u7b7e<\/p>\n<pre>&lt;pre&gt;&lt;code&gt;&amp;lt;choose&amp;gt;\n    &amp;lt;when test=&amp;quot;type == idcard&amp;quot;&amp;gt; \n        and a.id_card = #{code}\n    &amp;lt;\/when&amp;gt;\n&amp;lt;when test=&amp;quot;type == unitcode&amp;quot;&amp;gt;and a.unit_code = #{code}&amp;lt;\/when&amp;gt;\n    &amp;lt;otherwise&amp;gt;  \n    &amp;lt;\/otherwise&amp;gt;  \n&amp;lt;\/choose&amp;gt;&lt;\/code&gt;&lt;\/pre&gt;<\/pre>\n<p>\u4f18\u5316\u540e\u7684sql\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;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>\u91c7\u7528\u4e0a\u8ff0\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u89e3\u51b3mybatis\u52a8\u6001sql\u67e5\u8be2\u4e2d\u51fa\u73b0\u7684sql\u62fc\u63a5\u9519\u8bef\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aMybatis\u52a8\u6001SQL\u4f18\u5316\uff1a\u5982\u4f55\u907f\u514d\u62fc\u63a5\u9519\u8bef\u5bfc\u81f4\u67e5\u8be2\u62a5\u9519\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Mybatis\u52a8\u6001SQL\u4f18\u5316\uff1a\u5982&#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-200435","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200435","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=200435"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200435\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}