{"id":14248,"date":"2023-12-04T01:02:53","date_gmt":"2023-12-03T17:02:53","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=14248"},"modified":"2023-12-04T01:03:47","modified_gmt":"2023-12-03T17:03:47","slug":"mybatis-dong-tai-sql-you-shen-me-yong-zhi-xing-yua","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/04\/mybatis-dong-tai-sql-you-shen-me-yong-zhi-xing-yua\/","title":{"rendered":"MyBatis \u52a8\u6001 sql \u6709\u4ec0\u4e48\u7528\uff1f\u6267\u884c\u539f\u7406\uff1f\u6709\u54ea\u4e9b\u52a8\u6001 sql\uff1f"},"content":{"rendered":"<p>MyBatis \u52a8\u6001 SQL \u63d0\u4f9b\u4e86\u5728 XML \u6620\u5c04\u6587\u4ef6\u4e2d\u7f16\u5199\u52a8\u6001 SQL \u8bed\u53e5\u7684\u80fd\u529b\uff0c\u5141\u8bb8\u6839\u636e\u6761\u4ef6\u52a8\u6001\u751f\u6210 SQL \u8bed\u53e5\uff0c\u4e3b\u8981\u7528\u4e8e\u5728\u67e5\u8be2\u3001\u66f4\u65b0\u3001\u5220\u9664\u7b49\u64cd\u4f5c\u4e2d\u6839\u636e\u4e0d\u540c\u6761\u4ef6\u52a8\u6001\u62fc\u63a5 SQL \u8bed\u53e5\u3002<\/p>\n<h3>\u7528\u9014\uff1a<\/h3>\n<ol>\n<li><strong>\u6761\u4ef6\u5224\u65ad\uff1a<\/strong> \u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u52a8\u6001\u6dfb\u52a0 WHERE \u5b50\u53e5\u6216\u5176\u4ed6\u6761\u4ef6\u3002<\/li>\n<li><strong>\u52a8\u6001\u6392\u5e8f\uff1a<\/strong> \u6839\u636e\u6761\u4ef6\u52a8\u6001\u6dfb\u52a0 ORDER BY \u5b50\u53e5\u3002<\/li>\n<li><strong>\u52a8\u6001\u7ec4\u88c5\uff1a<\/strong> \u6839\u636e\u6761\u4ef6\u52a8\u6001\u7ec4\u88c5 SQL \u8bed\u53e5\uff0c\u5982\u52a8\u6001\u62fc\u63a5\u5217\u540d\u3001\u8868\u540d\u7b49\u3002<\/li>\n<\/ol>\n<h3>\u6267\u884c\u539f\u7406\uff1a<\/h3>\n<p>MyBatis \u7684\u52a8\u6001 SQL \u662f\u901a\u8fc7 XML \u6620\u5c04\u6587\u4ef6\u4e2d\u7684\u7279\u5b9a\u6807\u7b7e\u5b9e\u73b0\u7684\uff0c\u4e3b\u8981\u4f7f\u7528\u7684\u6807\u7b7e\u5305\u62ec <code>&lt;if&gt;<\/code>, <code>&lt;choose&gt;<\/code>, <code>&lt;when&gt;<\/code>, <code>&lt;otherwise&gt;<\/code>, <code>&lt;foreach&gt;<\/code> \u7b49\u3002<\/p>\n<ul>\n<li><code>&lt;if&gt;<\/code> \u6807\u7b7e\uff1a\u7528\u4e8e\u6761\u4ef6\u5224\u65ad\uff0c\u6839\u636e\u6761\u4ef6\u52a8\u6001\u751f\u6210 SQL \u7247\u6bb5\u3002<\/li>\n<li><code>&lt;choose&gt;<\/code>\u3001<code>&lt;when&gt;<\/code>\u3001<code>&lt;otherwise&gt;<\/code> \u6807\u7b7e\uff1a\u7528\u4e8e\u5b9e\u73b0\u7c7b\u4f3c switch-case \u7684\u6761\u4ef6\u5224\u65ad\uff0c\u9009\u62e9\u5176\u4e2d\u4e00\u4e2a\u5206\u652f\u6267\u884c\u3002<\/li>\n<li><code>&lt;foreach&gt;<\/code> \u6807\u7b7e\uff1a\u7528\u4e8e\u5faa\u73af\u904d\u5386\u96c6\u5408\u6216\u6570\u7ec4\uff0c\u52a8\u6001\u751f\u6210\u591a\u4e2a SQL \u7247\u6bb5\u3002<\/li>\n<\/ul>\n<p>MyBatis \u5728\u89e3\u6790 XML \u6620\u5c04\u6587\u4ef6\u65f6\uff0c\u4f1a\u6839\u636e\u6761\u4ef6\u5224\u65ad\uff0c\u52a8\u6001\u62fc\u63a5 SQL \u8bed\u53e5\uff0c\u5e76\u6839\u636e\u914d\u7f6e\u7684\u6761\u4ef6\u7ec4\u5408\u6700\u7ec8\u751f\u6210\u5b8c\u6574\u7684 SQL \u8bed\u53e5\u3002<\/p>\n<h3>\u52a8\u6001 SQL \u7684\u7c7b\u578b\uff1a<\/h3>\n<ol>\n<li><strong><code>&lt;if&gt;<\/code> \u6807\u7b7e\uff1a<\/strong> \u6839\u636e\u6761\u4ef6\u5224\u65ad\u662f\u5426\u6dfb\u52a0 SQL \u7247\u6bb5\u3002\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><code>&lt;choose&gt;<\/code>\u3001<code>&lt;when&gt;<\/code>\u3001<code>&lt;otherwise&gt;<\/code> \u6807\u7b7e\uff1a<\/strong> \u5b9e\u73b0\u7c7b\u4f3c switch-case \u7684\u6761\u4ef6\u5224\u65ad\uff0c\u9009\u62e9\u5176\u4e2d\u4e00\u4e2a\u5206\u652f\u6267\u884c\u3002\n<pre data-language=XML><code class=\"language-markup  line-numbers\">&lt;select id=\"getUserByType\" parameterType=\"map\" resultType=\"User\"&gt;\n   SELECT * FROM users\n   &lt;where&gt;\n       &lt;choose&gt;\n           &lt;when test=\"type == 'admin'\"&gt;\n               AND role = 'admin'\n           &lt;\/when&gt;\n           &lt;when test=\"type == 'user'\"&gt;\n               AND role = 'user'\n           &lt;\/when&gt;\n           &lt;otherwise&gt;\n               AND role = 'guest'\n           &lt;\/otherwise&gt;\n       &lt;\/choose&gt;\n   &lt;\/where&gt;\n&lt;\/select&gt;\n<\/code><\/pre>\n<\/li>\n<li><strong><code>&lt;foreach&gt;<\/code> \u6807\u7b7e\uff1a<\/strong> \u5faa\u73af\u904d\u5386\u96c6\u5408\u6216\u6570\u7ec4\uff0c\u52a8\u6001\u751f\u6210\u591a\u4e2a SQL \u7247\u6bb5\u3002\n<pre data-language=XML><code class=\"language-markup  line-numbers\">&lt;select id=\"getUsersByIdList\" parameterType=\"list\" resultType=\"User\"&gt;\n   SELECT * FROM users\n   WHERE id IN\n   &lt;foreach item=\"id\" collection=\"idList\" open=\"(\" separator=\",\" close=\")\"&gt;\n       #{id}\n   &lt;\/foreach&gt;\n&lt;\/select&gt;\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u8fd9\u4e9b\u52a8\u6001 SQL \u7684\u6807\u7b7e\u4f7f\u5f97 SQL \u8bed\u53e5\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u7075\u6d3b\u751f\u6210\uff0c\u63d0\u9ad8\u4e86 SQL \u67e5\u8be2\u548c\u64cd\u4f5c\u7684\u7075\u6d3b\u6027\u548c\u53ef\u5b9a\u5236\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MyBatis \u52a8\u6001 SQL \u63d0\u4f9b\u4e86\u5728 XML \u6620\u5c04\u6587\u4ef6\u4e2d\u7f16\u5199\u52a8\u6001 SQL \u8bed\u53e5\u7684\u80fd\u529b\uff0c\u5141\u8bb8\u6839\u636e\u6761\u4ef6\u52a8\u6001\u751f\u6210  [&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-14248","post","type-post","status-publish","format-standard","hentry","category-mybatis"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/14248","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=14248"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/14248\/revisions"}],"predecessor-version":[{"id":14249,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/14248\/revisions\/14249"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=14248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=14248"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=14248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}