﻿{"id":130,"date":"2016-03-16T23:49:14","date_gmt":"2016-03-16T15:49:14","guid":{"rendered":"http:\/\/www.chenweikang.top\/?p=130"},"modified":"2016-03-21T22:06:03","modified_gmt":"2016-03-21T14:06:03","slug":"mybatis%e5%ad%a6%e4%b9%a0%e4%b9%8b%e8%b7%af-%e5%8a%a8%e6%80%81sql%e6%9f%a5%e8%af%a2","status":"publish","type":"post","link":"https:\/\/www.chenweikang.top\/?p=130","title":{"rendered":"Mybatis\u5b66\u4e60\u4e4b\u8def\u2014\u52a8\u6001SQL\u67e5\u8be2"},"content":{"rendered":"<blockquote><p>mybatis\u4e2d \u4f7f\u7528 \u52a8\u6001SQL\uff1aif \/ where \/when\/otherwise\/trim \u7b49\u6761\u4ef6\u914d\u5408\u4f7f\u7528\uff0c\u5b9e\u73b0\u5f3a\u5927\u7684\u52a8\u6001SQL<\/p>\n<p>\u5b9a\u4e49\u4e00\u4e2a\u7ed3\u679c\u96c6<\/p>\n<p>&lt;resultMap type=\"Employee\" id=\"empRes\"&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;id property=\"id\" column=\"id\" \/&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;result property=\"username\" column=\"username\" \/&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;result property=\"salary\" column=\"salary\" \/&gt;<\/p>\n<p>&lt;\/resultMap&gt;<\/p>\n<p>parameterType=\"Map\" \u53c2\u6570\u7c7b\u578b\u4e3aMap<\/p>\n<p>resultMap=\"empRes\" \u4f7f\u7528\u5df2\u7ecf\u5b9a\u4e49\u7ed3\u679c\u96c6<\/p><\/blockquote>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-131\" src=\"http:\/\/www.chenweikang.top\/wp-content\/uploads\/2016\/03\/3-1.png\" alt=\"3\" width=\"650\" height=\"289\" \/><\/p>\n<h2>1. \u4f7f\u7528 if \u6761\u4ef6<\/h2>\n<blockquote><p>&lt;!-- \u52a8\u6001Sql if\u6761\u4ef6--&gt;<\/p>\n<p>&lt;select id=\"searchEmp\" parameterType=\"Map\" resultMap=\"empRes\"&gt;<\/p>\n<p style=\"padding-left: 30px;\">select * from \u00a0employee\u00a0where \u00a0deptID=#{deptID}<\/p>\n<p style=\"padding-left: 30px;\">&lt;!-- \u52a8\u6001\u67e5\u8be2 \u5224\u65ad\u6761\u4ef6\u62fc\u63a5 --&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;if test=\"username!=null\"&gt;<\/p>\n<p style=\"padding-left: 60px;\">and username like #{username}<\/p>\n<p style=\"padding-left: 30px;\">&lt;\/if&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;if test=\"salary!=null\"&gt;<\/p>\n<p style=\"padding-left: 60px;\">and salary=#{salary}<\/p>\n<p style=\"padding-left: 30px;\">&lt;\/if&gt;<br \/>\n&lt;\/select&gt;<\/p>\n<\/blockquote>\n<h2>2. \u4f7f\u7528 choose\/when\/otherwise<\/h2>\n<blockquote><p>&lt;!-- \u52a8\u6001SQL choose\/when --&gt;<br \/>\n&lt;select id=\"searchEmpByType\" parameterType=\"Map\" resultMap=\"empRes\" &gt;<\/p>\n<p>select * from employee<\/p>\n<p style=\"padding-left: 30px;\">&lt;choose&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;when test=\"searchBy=='deptID' \"&gt;<\/p>\n<p style=\"padding-left: 60px;\">where deptID=#{deptID}<\/p>\n<p style=\"padding-left: 30px;\">&lt;\/when&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;when test=\"searchBy=='username' \"&gt;<\/p>\n<p style=\"padding-left: 60px;\">where username like #{username}<\/p>\n<p style=\"padding-left: 30px;\">&lt;\/when&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;when test=\"searchBy=='salary' \"&gt;<\/p>\n<p style=\"padding-left: 60px;\">where salary=#{salary}<\/p>\n<p style=\"padding-left: 30px;\">&lt;\/when&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;\/choose&gt;<br \/>\n&lt;\/select&gt;<\/p>\n<\/blockquote>\n<h2>3. \u4f7f\u7528 where\/if \u5982\u679c\u53e5\u5b50\u4ee5and\u6216or\u5f00\u5934\u5c06\u81ea\u52a8\u5220\u9664 \u5bb9\u9519<\/h2>\n<blockquote><p>&lt;select id=\"searchEmp2\" parameterType=\"Map\" resultMap=\"empRes\" &gt;<\/p>\n<p>select * from employee<\/p>\n<p>&lt;where&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;!-- where\u5c06\u81ea\u52a8\u6dfb\u52a0\u6216\u5220\u9664and --&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;if test=\"deptID!=null\"&gt;<\/p>\n<p style=\"padding-left: 60px;\">deptID=#{deptID}<\/p>\n<p style=\"padding-left: 30px;\">&lt;\/if&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;if test=\"username!=null\"&gt;<\/p>\n<p style=\"padding-left: 60px;\">and username like #{username}<\/p>\n<p style=\"padding-left: 30px;\">&lt;\/if&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;if test=\"salary!=null\"&gt;<\/p>\n<p style=\"padding-left: 60px;\">and salary=#{salary}<\/p>\n<p style=\"padding-left: 30px;\">&lt;\/if&gt;<\/p>\n<p>&lt;\/where&gt;<br \/>\n&lt;\/select&gt;<\/p><\/blockquote>\n<h2>4. \u4f7f\u7528trim\u6761\u4ef6 \u63d0\u4f9b\u524d\u540e\u7f00\u529f\u80fd \u66f4\u52a0\u7075\u6d3b<\/h2>\n<blockquote><p>&lt;select id=\"searchEmp3\" parameterType=\"Map\" resultMap=\"empRes\" &gt;<\/p>\n<p>select * from employee<br \/>\n&lt;!--<\/p>\n<p>prefix=\"where\" \uff1a\u6dfb\u52a0\u524d\u7f00where<\/p>\n<p>prefixOverrides=\"and | or\" \u81ea\u52a8\u8986\u76d6and | or<\/p>\n<p>\u529f\u80fd\u548c\u4e0a\u8fb9where\u76f8\u540c<br \/>\n--&gt;<br \/>\n&lt;trim prefix=\"where\" prefixOverrides=\"and | or\"&gt;<\/p>\n<p>&lt;if test=\"deptID!=null\"&gt;<\/p>\n<p style=\"padding-left: 30px;\">deptID=#{deptID}<\/p>\n<p>&lt;\/if&gt;<\/p>\n<p>&lt;if test=\"username!=null\"&gt;<\/p>\n<p style=\"padding-left: 30px;\">and username like #{username}<\/p>\n<p>&lt;\/if&gt;<\/p>\n<p>&lt;if test=\"salary!=null\"&gt;<\/p>\n<p style=\"padding-left: 30px;\">and salary=#{salary}<\/p>\n<p>&lt;\/if&gt;<br \/>\n&lt;\/trim&gt;<br \/>\n&lt;\/select&gt;<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<h2>5. foreach\u5faa\u73af<\/h2>\n<blockquote><p>&lt;!-- \u6d4b\u8bd5foreach\u6761\u4ef6<br \/>\nitem=\"deptID\" \u904d\u5386\u7684\u53d6\u503c<br \/>\nopen=\"(\" \u5f00\u59cb\u52a0 (<br \/>\nseparator=\",\" \u9017\u53f7\u5206\u9694<br \/>\nclose=\")\" \u7ed3\u675f\u52a0 )<br \/>\n\u6700\u7ec8 \u5b9e\u73b0 (2,3,4,5,6 )<br \/>\n--&gt;<br \/>\n&lt;select id=\"searchEmp4\" parameterType=\"Map\" resultMap=\"empRes\" &gt;<\/p>\n<p style=\"padding-left: 30px;\">select * from employee<\/p>\n<p>&lt;where&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;if test=\"deptIds!=null\"&gt;<\/p>\n<p style=\"padding-left: 60px;\">deptID in<\/p>\n<p style=\"padding-left: 30px;\">&lt;foreach \u00a0collection=\"deptIds\" \u00a0item=\"deptID\" \u00a0open=\"(\" separator=\",\" close=\")\" \u00a0&gt;<\/p>\n<p style=\"padding-left: 60px;\">#{deptID}<\/p>\n<p style=\"padding-left: 30px;\">&lt;\/foreach&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;\/if&gt;<\/p>\n<p>&lt;\/where&gt;<br \/>\n&lt;\/select&gt;<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<h2>6. set \u6761\u4ef6(\u4f7f\u7528set\u4f7f\u66f4\u65b0\u65f6\u4e0d\u5fc5\u518d\u6b21\u66f4\u65b0\u5168\u90e8\u6570\u636e)<\/h2>\n<blockquote><p>&lt;!--<br \/>\nset \u6709\u5bb9\u9519\u529f\u80fd \u81ea\u52a8\u5254\u9664\u6700\u540e\u4e00\u4e2a\u9017\u53f7,<br \/>\n--&gt;<br \/>\n&lt;update id=\"updateEmp\" parameterType=\"Employee\"&gt;<\/p>\n<p style=\"padding-left: 30px;\">update employee<\/p>\n<p>&lt;set&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;if test=\"username!=null\"&gt;<\/p>\n<p style=\"padding-left: 60px;\">username=#{username},<\/p>\n<p style=\"padding-left: 30px;\">&lt;\/if&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;if test=\"salary&gt;0\"&gt;<\/p>\n<p style=\"padding-left: 60px;\">salary=#{salary}<\/p>\n<p style=\"padding-left: 30px;\">&lt;\/if&gt;<\/p>\n<p>&lt;\/set&gt;<\/p>\n<p>where id=#{id}<br \/>\n&lt;\/update&gt;<\/p><\/blockquote>\n<p>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1a<a href=\"https:\/\/www.chenweikang.top\">\u5de6\u624b\u4ee3\u7801\u53f3\u624b\u8bd7<\/a> &raquo; <a href=\"https:\/\/www.chenweikang.top\/?p=130\">Mybatis\u5b66\u4e60\u4e4b\u8def\u2014\u52a8\u6001SQL\u67e5\u8be2<\/a><\/p><div class=\"__youshang\">\r\n            <div id=\"__youshang_popup\" class=\"wechat popup\" style=\"display: none;\">\r\n                <div class=\"head\">~\u8c22\u8c22\u6253\u8d4f~<\/div>\r\n                <div class=\"qrcode\"><div class=\"qrcode-li wechat\" ><img src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2019\/07\/wexin.png\" \/><\/div><div class=\"qrcode-li alipay\" style=\"display:none;\"><img src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2019\/07\/ali-pay.png\" \/><\/div><div class=\"qrcode-li hongbao\" style=\"display:none;\"><img src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2019\/07\/ali-hongbao.png\" \/><\/div><\/div>\r\n                <ul class=\"platform\"><li class=\"icon-wechat active\" data-bg-color=\"#05af4e\" data-thanks=\"~\u8c22\u8c22\u6253\u8d4f~\"><\/li><li class=\"icon-alipay \" data-bg-color=\"#00a2ea\" data-thanks=\"~\u8c22\u8c22\u6253\u8d4f~\"><\/li><li class=\"icon-hongbao \" data-bg-color=\"#dd5746\" data-thanks=\"<p>\u626b\u7801\u9886\u7ea2\u5305<\/p><p style='margin-top: 24px;'>\uff08\u4f59\u989d\u5b9d\u652f\u4ed8\u65f6\u53ef\u62b5\u73b0\uff09<\/p>\"><\/li><\/ul>\r\n            <\/div>\r\n            <a href=\"javascript:void(0);\" id=\"__youshang_btn\">\u8d4f<\/a>\r\n        <\/div>","protected":false},"excerpt":{"rendered":"<p>mybatis\u4e2d \u4f7f\u7528 \u52a8\u6001SQL\uff1aif \/ where \/when\/otherwise\/trim \u7b49\u6761\u4ef6\u914d\u5408 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8,9,3],"tags":[32],"class_list":["post-130","post","type-post","status-publish","format-standard","hentry","category-java","category-houduan","category-note","tag-mybatis"],"_links":{"self":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/posts\/130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=130"}],"version-history":[{"count":0,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/posts\/130\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}