{"id":46188,"date":"2024-12-01T16:05:40","date_gmt":"2024-12-01T08:05:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46188\/"},"modified":"2024-12-01T16:05:40","modified_gmt":"2024-12-01T08:05:40","slug":"mybatis%e5%8a%a8%e6%80%81sql%e4%bc%98%e5%8c%96%ef%bc%9a%e5%a6%82%e4%bd%95%e6%ad%a3%e7%a1%ae%e4%bd%bf%e7%94%a8%ef%bc%9cif%ef%bc%9e%e5%92%8c%ef%bc%9cchoose%ef%bc%9e%e6%a0%87%e7%ad%be%ef%bc%9f-3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46188\/","title":{"rendered":"Mybatis\u52a8\u6001SQL\u4f18\u5316\uff1a\u5982\u4f55\u6b63\u786e\u4f7f\u7528\uff1cif\uff1e\u548c\uff1cchoose\uff1e\u6807\u7b7e\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Mybatis\u52a8\u6001SQL\u4f18\u5316\uff1a\u5982\u4f55\u6b63\u786e\u4f7f\u7528\uff1cif\uff1e\u548c\uff1cchoose\uff1e\u6807\u7b7e\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300aMybatis\u52a8\u6001SQL\u4f18\u5316\uff1a\u5982\u4f55\u6b63\u786e\u4f7f\u7528\uff1cif\uff1e\u548c\uff1cchoose\uff1e\u6807\u7b7e\uff1f\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241126\/17326213646745b434b5794.jpg\" class=\"aligncenter\" title=\"Mybatis\u52a8\u6001SQL\u4f18\u5316\uff1a\u5982\u4f55\u6b63\u786e\u4f7f\u7528\uff1cif\uff1e\u548c\uff1cchoose\uff1e\u6807\u7b7e\uff1f\u63d2\u56fe\" alt=\"Mybatis\u52a8\u6001SQL\u4f18\u5316\uff1a\u5982\u4f55\u6b63\u786e\u4f7f\u7528\uff1cif\uff1e\u548c\uff1cchoose\uff1e\u6807\u7b7e\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>mybatis\u52a8\u6001sql\u7f16\u5199\uff0c\u9047\u5230\u95ee\u9898\u6c42\u89e3<\/strong><\/p>\n<p>\u5728\u4f7f\u7528mybatis\u52a8\u6001sql\u65f6\uff0c\u9047\u5230\u5982\u4e0b\u95ee\u9898\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>\u60f3\u8981\u5c06\u5176\u4f18\u5316\u4e3a\uff1a<\/p>\n<pre>select * from table a where a.project_id=#{projectid} and a.id != #{id} and a.status=3 and\n&lt;if test=\"type == 'idcard'\"&gt;a.id_card = #{code}&lt;\/if&gt;\n&lt;if test=\"type == 'unitcode'\"&gt;a.unit_code = #{code}&lt;\/if&gt;<\/pre>\n<p>\u4f46\u4f18\u5316\u540e\u8fd0\u884c\u4f1a\u62a5\u9519\uff083\u79cd\u5199\u6cd5\u90fd\u4f1a\u62a5badsql\uff09\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong><\/p>\n<p>\u6b63\u786e\u7684\u5199\u6cd5\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>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aMybatis\u52a8\u6001SQL\u4f18\u5316\uff1a\u5982\u4f55\u6b63\u786e\u4f7f\u7528\uff1cif\uff1e\u548c\uff1cchoose\uff1e\u6807\u7b7e\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u5427\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Mybatis\u52a8\u6001SQL\u4f18\u5316\uff1a\u5982\u4f55\u6b63\u786e\u4f7f\u7528\uff1cif\uff1e\u548c\uff1cchoose\uff1e\u6807\u7b7e\uff1f \u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300aMybatis\u52a8\u6001SQL\u4f18\u5316\uff1a\u5982\u4f55\u6b63\u786e\u4f7f\u7528\uff1cif\uff1e\u548c\uff1cchoose\uff1e\u6807\u7b7e\uff1f\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~ mybatis\u52a8\u6001sql\u7f16\u5199\uff0c\u9047\u5230\u95ee\u9898\u6c42\u89e3 \u5728\u4f7f\u7528mybatis\u52a8\u6001sql\u65f6\uff0c\u9047\u5230\u5982\u4e0b\u95ee\u9898\uff1a 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} \u60f3\u8981\u5c06\u5176\u4f18\u5316\u4e3a\uff1a select * from table a where a.project_id=#{projectid} and a.id != #{id} and a.status=3 and &lt;if test=&#8221;type == &#8216;idcard'&#8221;&gt;a.id_card = #{code}&lt;\/if&gt; &lt;if test=&#8221;type == &#8216;unitcode'&#8221;&gt;a.unit_code = #{code}&lt;\/if&gt; \u4f46\u4f18\u5316\u540e\u8fd0\u884c\u4f1a\u62a5\u9519\uff083\u79cd\u5199\u6cd5\u90fd\u4f1a\u62a5badsql\uff09\u3002 \u89e3\u51b3\u65b9\u6cd5 \u6b63\u786e\u7684\u5199\u6cd5\u5982\u4e0b\uff1a [&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-46188","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46188","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=46188"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46188\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}