{"id":14199,"date":"2023-12-04T00:45:51","date_gmt":"2023-12-03T16:45:51","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=14199"},"modified":"2023-12-04T00:46:55","modified_gmt":"2023-12-03T16:46:55","slug":"mybatis-dong-tai-sql-shi-zuo-shen-me-de-dou-you-na","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/04\/mybatis-dong-tai-sql-shi-zuo-shen-me-de-dou-you-na\/","title":{"rendered":"MyBatis \u52a8\u6001 sql \u662f\u505a\u4ec0\u4e48\u7684\uff1f\u90fd\u6709\u54ea\u4e9b\u52a8\u6001 sql\uff1f\u80fd\u7b80\u8ff0\u4e00\u4e0b\u52a8\u6001 sql \u7684\u6267\u884c\u539f\u7406\u4e0d\uff1f"},"content":{"rendered":"<p>MyBatis \u7684\u52a8\u6001 SQL \u5141\u8bb8\u5728 SQL \u6620\u5c04\u6587\u4ef6\u4e2d\u7f16\u5199\u52a8\u6001\u6761\u4ef6\uff0c\u6839\u636e\u4e0d\u540c\u7684\u6761\u4ef6\u751f\u6210\u4e0d\u540c\u7684 SQL \u8bed\u53e5\uff0c\u4ee5\u9002\u5e94\u4e0d\u540c\u7684\u67e5\u8be2\u9700\u6c42\u3002\u52a8\u6001 SQL \u63d0\u4f9b\u4e86\u5728\u4e0d\u540c\u6761\u4ef6\u4e0b\u7075\u6d3b\u6784\u5efa SQL \u67e5\u8be2\u8bed\u53e5\u7684\u80fd\u529b\uff0c\u907f\u514d\u4e86\u786c\u7f16\u7801\u591a\u4e2a\u56fa\u5b9a SQL \u67e5\u8be2\u3002<\/p>\n<h3>\u52a8\u6001 SQL \u7684\u4e3b\u8981\u5b9e\u73b0\uff1a<\/h3>\n<ol>\n<li><strong>if \u5143\u7d20\uff1a<\/strong> \u6761\u4ef6\u5224\u65ad\uff0c\u6839\u636e\u6761\u4ef6\u751f\u6210\u4e0d\u540c\u7684 SQL \u8bed\u53e5\u7247\u6bb5\u3002\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre data-language=XML><code class=\"language-markup  line-numbers\">&lt;select id=\"getUserByCondition\" parameterType=\"map\" resultType=\"User\"&gt;\n   SELECT * FROM users\n   &lt;where&gt;\n       &lt;if test=\"name != null\"&gt;\n           AND name = #{name}\n       &lt;\/if&gt;\n       &lt;if test=\"age != null\"&gt;\n           AND age = #{age}\n       &lt;\/if&gt;\n   &lt;\/where&gt;\n&lt;\/select&gt;\n<\/code><\/pre>\n<\/li>\n<li><strong>choose \u5143\u7d20\uff1a<\/strong> \u7c7b\u4f3c\u4e8e switch-case \u7684\u9009\u62e9\u7ed3\u6784\uff0c\u6839\u636e\u6761\u4ef6\u9009\u62e9\u4e0d\u540c\u7684 SQL \u8bed\u53e5\u5757\u3002\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre data-language=XML><code class=\"language-markup  line-numbers\">&lt;select id=\"getUserByCondition\" parameterType=\"map\" resultType=\"User\"&gt;\n   SELECT * FROM users\n   &lt;where&gt;\n       &lt;choose&gt;\n           &lt;when test=\"name != null\"&gt;\n               AND name = #{name}\n           &lt;\/when&gt;\n           &lt;when test=\"age != null\"&gt;\n               AND age = #{age}\n           &lt;\/when&gt;\n           &lt;otherwise&gt;\n               AND status = 'active'\n           &lt;\/otherwise&gt;\n       &lt;\/choose&gt;\n   &lt;\/where&gt;\n&lt;\/select&gt;\n<\/code><\/pre>\n<\/li>\n<li><strong>trim\u3001where\u3001set\u3001foreach \u5143\u7d20\u7b49\uff1a<\/strong> \u7528\u4e8e\u5728\u751f\u6210 SQL \u8bed\u53e5\u65f6\u53bb\u9664\u6216\u6dfb\u52a0\u7279\u5b9a\u7684 SQL \u7247\u6bb5\uff0c\u6839\u636e\u6761\u4ef6\u52a8\u6001\u6784\u5efa SQL\u3002<\/p>\n<\/li>\n<\/ol>\n<h3>\u52a8\u6001 SQL \u7684\u6267\u884c\u539f\u7406\uff1a<\/h3>\n<ul>\n<li>\n<p><strong>\u89e3\u6790\u4e0e\u5408\u5e76\uff1a<\/strong> MyBatis \u89e3\u6790 SQL \u6620\u5c04\u6587\u4ef6\u65f6\uff0c\u4f1a\u6839\u636e\u6761\u4ef6\u5224\u65ad\u3001\u9009\u62e9\u7b49\u6807\u7b7e\u8fdb\u884c\u89e3\u6790\u3002\u6839\u636e\u6761\u4ef6\u5224\u65ad\u7684\u7ed3\u679c\uff0c\u9009\u62e9\u76f8\u5e94\u7684 SQL \u8bed\u53e5\u7247\u6bb5\uff0c\u7136\u540e\u5c06\u8fd9\u4e9b SQL \u7247\u6bb5\u5408\u5e76\u4e3a\u6700\u7ec8\u7684 SQL \u8bed\u53e5\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u52a8\u6001 SQL \u751f\u6210\uff1a<\/strong> \u6839\u636e\u89e3\u6790\u540e\u7684\u7ed3\u679c\uff0cMyBatis \u5c06\u52a8\u6001 SQL \u7684\u5404\u4e2a\u90e8\u5206\u6309\u7167\u4e00\u5b9a\u7684\u89c4\u5219\u7ec4\u88c5\u6210\u5b8c\u6574\u7684 SQL \u8bed\u53e5\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u53c2\u6570\u7ed1\u5b9a\u4e0e\u6267\u884c\uff1a<\/strong> \u6700\u7ec8\u751f\u6210\u7684 SQL \u8bed\u53e5\u4e0e\u53c2\u6570\u7ed1\u5b9a\u540e\u4ea4\u7531\u6570\u636e\u5e93\u6267\u884c\uff0c\u5f97\u5230\u7ed3\u679c\u8fd4\u56de\u7ed9\u8c03\u7528\u65b9\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u52a8\u6001 SQL \u7684\u6267\u884c\u539f\u7406\u662f\u6839\u636e\u6761\u4ef6\u52a8\u6001\u751f\u6210 SQL \u8bed\u53e5\uff0c\u5e76\u4e14\u5728\u8fd0\u884c\u65f6\u6839\u636e\u6761\u4ef6\u7684\u4e0d\u540c\u7ec4\u88c5\u51fa\u4e0d\u540c\u7684 SQL \u7247\u6bb5\uff0c\u8fd9\u79cd\u7075\u6d3b\u6027\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u573a\u666f\u751f\u6210\u4e0d\u540c\u7684 SQL \u67e5\u8be2\u8bed\u53e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MyBatis \u7684\u52a8\u6001 SQL \u5141\u8bb8\u5728 SQL \u6620\u5c04\u6587\u4ef6\u4e2d\u7f16\u5199\u52a8\u6001\u6761\u4ef6\uff0c\u6839\u636e\u4e0d\u540c\u7684\u6761\u4ef6\u751f\u6210\u4e0d\u540c\u7684 SQL \u8bed\u53e5 [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[113],"tags":[],"class_list":["post-14199","post","type-post","status-publish","format-standard","hentry","category-mybatis"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/14199","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=14199"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/14199\/revisions"}],"predecessor-version":[{"id":14200,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/14199\/revisions\/14200"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=14199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=14199"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=14199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}