{"id":25401,"date":"2024-11-24T08:25:34","date_gmt":"2024-11-24T00:25:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/25401\/"},"modified":"2024-11-24T08:25:34","modified_gmt":"2024-11-24T00:25:34","slug":"mybatis%e5%8a%a8%e6%80%81sql%e4%b8%ad%ef%bc%8c%e5%a6%82%e4%bd%95%e9%81%bf%e5%85%8d%e5%92%8c%e5%b9%b6%e5%88%97%e4%bd%bf%e7%94%a8%e5%af%bc%e8%87%b4%e7%9a%84badsql%e9%97%ae%e9%a2%98%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/25401\/","title":{"rendered":"MyBatis\u52a8\u6001SQL\u4e2d\uff0c\u5982\u4f55\u907f\u514d&#8220;\u548c&#8220;\u5e76\u5217\u4f7f\u7528\u5bfc\u81f4\u7684BadSql\u95ee\u9898\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173060731995803.jpg\" class=\"aligncenter\" title=\"MyBatis\u52a8\u6001SQL\u4e2d\uff0c\u5982\u4f55\u907f\u514d&#8220;\u548c&#8220;\u5e76\u5217\u4f7f\u7528\u5bfc\u81f4\u7684BadSql\u95ee\u9898\uff1f\u63d2\u56fe\" alt=\"MyBatis\u52a8\u6001SQL\u4e2d\uff0c\u5982\u4f55\u907f\u514d&#8220;\u548c&#8220;\u5e76\u5217\u4f7f\u7528\u5bfc\u81f4\u7684BadSql\u95ee\u9898\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>mybatis\u52a8\u6001sql\u5bfc\u81f4badsql\u95ee\u9898<\/strong><\/p>\n<p>\u4f7f\u7528mybatis\u7684\u52a8\u6001sql\u6761\u4ef6\u5224\u65ad\u65f6\uff0c\u9700\u8981\u4f7f\u7528    \u6807\u7b7e\u6765\u5305\u88f9\u4e0d\u540c\u7684\u6761\u4ef6\u5224\u65ad\u3002   <\/p>\n<p><strong>\u9519\u8bef\u793a\u4f8b\uff1a<\/strong><\/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>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u539f\u56e0\uff1a<\/strong><br \/>\u5728  \u6807\u7b7e\u5185\uff0c  \u548c  \u6761\u4ef6\u5e76\u5217\u4f7f\u7528\u65f6\uff0c\u4f1a\u4ea7\u751f\u8bed\u4e49\u9519\u8bef\u3002   <\/p>\n<p><strong>\u4f18\u5316\u540e\u7684\u4ee3\u7801\uff1a<\/strong><\/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><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<p>\u4f18\u5316\u540e\u7684\u4ee3\u7801\u4f7f\u7528    \u6807\u7b7e\u5bf9\u4e0d\u540c\u7684\u6761\u4ef6\u8fdb\u884c\u5224\u65ad\uff0c\u786e\u4fdd\u53ea\u6709\u6ee1\u8db3\u6761\u4ef6\u7684\u5206\u652f\u624d\u4f1a\u88ab\u6267\u884c\uff0c\u4ece\u800c\u89e3\u51b3\u4e86badsql\u95ee\u9898\u3002   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fMyBatis\u52a8\u6001SQL\u4e2d\uff0c\u5982\u4f55\u907f\u514d&#8220;\u548c&#8220;\u5e76\u5217\u4f7f\u7528\u5bfc\u81f4\u7684BadSql\u95ee\u9898\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>mybatis\u52a8\u6001sql\u5bfc\u81f4badsql\u95ee\u9898 \u4f7f\u7528mybatis\u7684\u52a8\u6001sql\u6761\u4ef6\u5224\u65ad\u65f6\uff0c\u9700\u8981\u4f7f\u7528 \u6807\u7b7e\u6765\u5305\u88f9\u4e0d\u540c\u7684\u6761\u4ef6\u5224\u65ad\u3002 \u9519\u8bef\u793a\u4f8b\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} \u767b\u5f55\u540e\u590d\u5236 \u539f\u56e0\uff1a\u5728 \u6807\u7b7e\u5185\uff0c \u548c \u6761\u4ef6\u5e76\u5217\u4f7f\u7528\u65f6\uff0c\u4f1a\u4ea7\u751f\u8bed\u4e49\u9519\u8bef\u3002 \u4f18\u5316\u540e\u7684\u4ee3\u7801\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 [&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-25401","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25401","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=25401"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25401\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=25401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=25401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=25401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}