{"id":240,"date":"2021-09-27T23:35:32","date_gmt":"2021-09-27T15:35:32","guid":{"rendered":"http:\/\/qqiuklele.cn\/?p=240"},"modified":"2025-03-26T15:19:44","modified_gmt":"2025-03-26T07:19:44","slug":"mybatis%e5%a4%9a%e8%a1%a8%e8%81%94%e5%90%88%e6%9f%a5%e8%af%a2","status":"publish","type":"post","link":"https:\/\/qqiukulele.cn\/?p=240","title":{"rendered":"Mybatis\u591a\u8868\u8054\u5408\u67e5\u8be2"},"content":{"rendered":"\n<p>\u8868\u4e0e\u8868\u4e4b\u95f4\u7684\u5173\u7cfb\u53ef\u4ee5\u5206\u4e3a\u4e09\u79cd\uff1a1-1\u30011-N\u3001N-N\u3002\u90a3\u4e48\u6211\u5c06\u7528\u5f3a\u5927\u7684mybatis\u6f14\u793a\u548c\u603b\u7ed3\u4e00\u4e0b\u8fd9<code>\u4e00\u5bf9\u591a<\/code>\u548c<code>\u591a\u5bf9\u591a<\/code>\u7684\u5173\u7cfb\u7684\u67e5\u8be2\uff0c\u4e14\u5206\u522b\u7528\u4e24\u6b65\u67e5\u8be2\u65b9\u6cd5\u548c\u8fde\u63a5\u67e5\u8be2\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u51c6\u5907\u5de5\u4f5c<\/h2>\n\n\n\n<p>\u5efa\u7acb4\u5f20\u8868\uff0c\u73ed\u4e3b\u4efb\u8868\u3001\u5b66\u751f\u8868\u3001\u8bfe\u7a0b\u8868\u3001\u9009\u8bfe\u60c5\u51b5\u8868\u3002\u73ed\u4e3b\u4efb\u548c\u5b66\u751f\u5bf9\u5e94\u7684\u662f1-N\u3001\u5b66\u751f\u548c\u8bfe\u7a0b\u5bf9\u5e94\u7684\u662fN-N\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/qqiukulele.cn\/wp-content\/uploads\/2021\/09\/zk1.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"http:\/\/qqiukulele.cn\/wp-content\/uploads\/2021\/09\/zk1.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\"\/><\/div><\/figure>\n\n\n\n<p>\u5176\u6b21\uff0c\u521b\u5efa\u5bf9\u5e94\u76843\u4e2a\u5b9e\u4f53\u7c7b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public class Course {<br> &nbsp; &nbsp;private Integer id;<br> &nbsp; &nbsp;private String name; <br> &nbsp; &nbsp;private List&lt;Student&gt; students;\/\/\u591a\u8868\u67e5\u8be2 \u591a\u5bf9\u591a<br> &nbsp; &nbsp;\/\/get...set...<br>}<br>\u200b<br>public class Student {<br> &nbsp; &nbsp;private Integer id;<br> &nbsp; &nbsp;private Integer tid;<br> &nbsp; &nbsp;private String name;<br> &nbsp; &nbsp;private Teacher teacher; \/\/\u591a\u8868\u67e5\u8be2 \u591a\u5bf9\u4e00<br>}<br>\u200b<br>public class Teacher {<br> &nbsp; &nbsp;private Integer id;<br> &nbsp; &nbsp;private String name;<br> &nbsp; &nbsp;private List&lt;Student&gt; students; \/\/\u591a\u8868\u67e5\u8be2 \u4e00\u5bf9\u591a<br>}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">1-N<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e24\u6b65\u67e5\u8be2<\/h3>\n\n\n\n<p>\u4e24\u6b65\u67e5\u8be2\u6bd4\u8f83\u597d\u7406\u89e3\uff0c\u5c31\u662f\u5bf9\u6301\u6709\u5f15\u7528\u5bf9\u8c61\u7684\u5bf9\u8c61\u6267\u884c\u4e24\u6b21select\u8bed\u53e5\uff0c\u628a\u5c5e\u6027\u7684\u5bf9\u8c61\u4e5f\u67e5\u51fa\u6765\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u67e5\u8be2\u201c1-N\u201d\u7684\u4e00\u65b9\uff0c\u4e5f\u5c31\u662f\u67e5\u8be2\u8001\u5e08\u65b9\uff0c\u628a\u5e26\u7684\u5b69\u5b50\u90fd\u67e5\u8be2\u51fa\u6765\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public List&lt;Teacher&gt; selectAllFromTeacher();<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;resultMap id=\"myMap\" type=\"com.vo.Teacher\"&gt;<br>&lt;!-- &nbsp;  \u8fdb\u884c\u6570\u636e\u5e93\u5b57\u6bb5\u4e0eJavaBean\u5b57\u6bb5\u7684\u6620\u5c04--&gt;<br> &nbsp;  &lt;id column=\"id\" property=\"id\"&gt;&lt;\/id&gt;<br> &nbsp;  &lt;result column=\"name\" property=\"name\"&gt;&lt;\/result&gt;<br> &nbsp;  &lt;collection property=\"students\" select=\"com.mapper.IStudent.selectAllByTid\" column=\"id\"&gt;&lt;\/collection&gt;<br>&lt;\/resultMap&gt;<br>\u200b<br>&lt;select id=\"selectAllFromTeacher\" resultMap=\"myMap\"&gt;<br> &nbsp; &nbsp;select * from teacher<br>&lt;\/select&gt;<br> &nbsp; &nbsp;<br>&lt;select id=\"selectAllByTid\" parameterType=\"int\" resultType=\"com.vo.Student\"&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;select * from student where tid = #{id}<br>&lt;\/select&gt;<br>\u200b<\/pre>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230select\u8bed\u53e5\u91cc\u6ca1\u6709\u4f7f\u7528resultType\uff0c\u800c\u662f\u7528\u4e86resultMap\u3002\u56e0\u4e3aTeacher\u91cc\u6709\u4e00\u4e2a\u5c5e\u6027\u662fstudent\u96c6\u5408\uff0c\u6240\u4ee5\u53ef\u4ee5\u4f7f\u7528resultMap\uff08\u7ed3\u679c\u6620\u5c04\uff09\u53bb\u628a\u5173\u8054\u7684student\u90fd\u6620\u5c04\u51fa\u6765\u3002<\/p>\n\n\n\n<p>resultType\u7528\u5230\u7684\u5b50\u5143\u7d20\u6709\uff1aid\uff08\u6807\u8bb0\u51fa\u4f5c\u4e3a ID \u7684\u7ed3\u679c\u53ef\u4ee5\u5e2e\u52a9\u63d0\u9ad8\u6574\u4f53\u6027\u80fd\uff09\u3001result\uff08\u6ce8\u5165\u5230\u5b57\u6bb5\u6216 JavaBean \u5c5e\u6027\u7684\u666e\u901a\u7ed3\u679c\uff09\u3001collection\uff08\u5d4c\u5957\u7ed3\u679c\u6620\u5c04\uff09\uff0c\u4ee5\u4e0b\u662f\u6211\u5b66\u4e60\u8fc7\u7a0b\u601d\u8003\u4e86\u6bd4\u8f83\u4e45\u7684\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em>id<\/em> \u548c <em>result<\/em> \u5143\u7d20\u90fd\u5c06\u4e00\u4e2a\u5217\u7684\u503c\u6620\u5c04\u5230\u4e00\u4e2a\u7b80\u5355\u6570\u636e\u7c7b\u578b\uff08String, int, double, Date \u7b49\uff09\u7684\u5c5e\u6027\u6216\u5b57\u6bb5\u3002<\/li>\n\n\n\n<li>id\u548cresult\u8fd9\u4e24\u4e2a\u5b50\u96c6\u7684column\u91cc\u7684\u503c\u7684\u5c31\u662f\u6570\u636e\u5e93\u91cc\u7684\u5217\u540d\u5b57\uff08\u5982\u679csql\u8bed\u53e5\u91cc\u4e3a\u5217\u53d6\u4e86\u522b\u540d\uff0c\u53ef\u4ee5\u66ff\u6362\u6210\u522b\u540d\uff09\uff0cproperty\u5c31\u662f\u4e0a\u9762type\u91cc\u5b9a\u4e49\u7684Teacher\u5bf9\u8c61\u91cc\u7684\u5c5e\u6027\u540d\u3002<\/li>\n\n\n\n<li>collection\u5b50\u96c6\u5c31\u7279\u522b\u6709\u610f\u601d\u4e86\uff0c\u5b83\u4e5f\u662f\u4e00\u4e2a\u96c6\u5408\uff0c\u800c\u4e14\u5b83\u662f\u53e6\u4e00\u4e2a\u67e5\u8be2\u65b9\u6cd5selectAllByTid\u67e5\u51fa\u6765\u7684\u7ed3\u679c\u96c6\u5408\u3002\u5b83\u7684property\u5c31\u662fTeacher\u5bf9\u8c61\u91cc\u7684\u9700\u8981\u591a\u8868\u67e5\u8be2\u7684Students\u5c5e\u6027\u5566\uff1bcolumn\u91cc\u7684id\u5bf9\u5e94\u7684\u4e5f\u662f\u6570\u636e\u5e93\u5217\u540d\uff0c\u4f46\u662f\u5b83\u4e5f\u5145\u5f53\u7740\u4f20\u9012\u7ed9\u8fd9\u4e2a\u5d4c\u5957\u67e5\u8be2\u7684sql\u8bed\u53e5\u6240\u9700\u8981\u7684\u53c2\u6570\u3002\u6240\u4ee5\u53ef\u4ee5\u770b\u5230selectAllByTid\u65b9\u6cd5\u91cc\u6709#{id}\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u67e5\u8be2\u201c1-N\u201d\u7684\u591a\u65b9\uff0c\u4e5f\u5c31\u662f\u67e5\u5b66\u751f\uff0c\u60f3\u628a\u5bf9\u5e94\u7684\u73ed\u4e3b\u4efb\u7684\u4fe1\u606f\u4e5f\u67e5\u51fa\u6765\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public List&lt;Student&gt; selectAllStu();<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;resultMap id=\"studentMap\" type=\"com.vo.Student\"&gt;<br> &nbsp;  &lt;id column=\"id\" property=\"id\"&gt;&lt;\/id&gt;<br> &nbsp;  &lt;result column=\"tid\" property=\"tid\"&gt;&lt;\/result&gt;<br> &nbsp;  &lt;result column=\"name\" property=\"name\"&gt;&lt;\/result&gt;<br> &nbsp;  &lt;association property=\"teacher\" column=\"tid\" select=\"com.mapper.ITeacher.selectByTid\"&gt;<br> &nbsp;  &lt;\/association&gt;<br>&lt;\/resultMap&gt;<br>\u200b<br>\u200b<br>&lt;select id=\"selectAllStu\" resultMap=\"studentMap\"&gt;<br> &nbsp; &nbsp;select * from student<br>&lt;\/select&gt;<br>\u200b<br>\u200b<br>&lt;select id=\"selectByTid\" parameterType=\"int\" resultMap=\"myMap\"&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;select * &nbsp;from teacher where id = #{tid}<br>&lt;\/select&gt;<\/pre>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u6574\u4e2a\u4ee3\u7801\u5176\u5b9e\u548c\u67e5\u8be2\u8001\u5e08\u7684\u611f\u89c9\u662f\u5dee\u4e0d\u591a\u7684\u3002\u4e5f\u662f\u5728\u67e5\u8be2\u5b66\u751f\u65f6\uff0c\u7528\u4e86\u5c5e\u6027resultMap\u6765\u628a\u67e5\u8be2\u5230\u7684\u6240\u6709\u5b66\u751f\u7684\u7ed3\u679c\u6620\u5c04\u4e0b\u6765\u3002\u552f\u4e00\u7684\u533a\u522b\u5c31\u662f\u6ca1\u6709\u518d\u4f7f\u7528collection\uff0c\u800c\u662f\u4f7f\u7528\u4e86association\u3002<\/p>\n\n\n\n<p>association\u7684\u4f5c\u7528\u662f\u5c06\u6570\u636e\u5e93\u4e2d\u7684\u73ed\u4e3b\u4efb\u7684\u4fe1\u606f\u6620\u5c04\u5230Student\u7c7b\u91cc\u7684Teacher\u7c7b\u5c5e\u6027\u4e2d\u3002association\u91cc\u7684property\u4f9d\u7136\u5bf9\u5e94\u7684\u662fStudent\u5bf9\u8c61\u7684\u5c5e\u6027\u540d\u5b57teacher\uff1bcolumn\u4e5f\u5c31\u662f\u628a\u6570\u636e\u5e93\u5217\u540dtid\u7684\u503c\uff0c\u4e5f\u662f\u8981\u4f20\u9012\u7ed9\u5d4c\u5957select\u65b9\u6cd5\u7684\u53c2\u6570\u3002\u6240\u4ee5\u4f60\u4e5f\u53ef\u4ee5\u5728selectByTid\u65b9\u6cd5\u91cc\u770b\u89c1id = #{tid}\u3002\u5c31\u662f\u8fd9\u4e48\u7b80\u5355\uff01<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">association\u548ccollection<\/h4>\n\n\n\n<p>\u90a3\u4e48\u4e3a\u4ec0\u4e48\u4e00\u4f1a\u513f\u7528association\uff0c\u4e00\u4f1a\u513f\u7528collection\u5462\uff1f<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>association\u7528\u5728\u201chas one\u201d\u7684\u60c5\u51b5\uff0c\u6bd4\u5982\u4e0a\u9762\u6bcf\u4e2a\u5b66\u751f\u90fd\u53ea\u6709\u4e00\u4f4d\u73ed\u4e3b\u4efb\uff0c\u6240\u4ee5\u67e5\u8be2\u5b66\u751f\u8868\u7684\u73ed\u4e3b\u4efb\u4fe1\u606f\u65f6\uff0c\u7528\u4e86association\u53bb\u5d4c\u5957\u67e5\u8be2<\/li>\n\n\n\n<li>collection\u7528\u5728\u201chas many\u201d\u7684\u60c5\u51b5\uff0c\u6bd4\u5982\u4e0a\u9762\u4e00\u4f4d\u73ed\u4e3b\u4efb\u5e26\u4e00\u4e2a\u73ed\u7ea7\u7684\u5f88\u591a\u5b66\u751f\uff0c\u90a3\u4e48\u67e5\u8be2\u8001\u5e08\u4fe1\u606f\u65f6\uff0c\u5979\u5e26\u7684\u5b69\u5b50\u5f97\u7528collection\u53bb\u5d4c\u5957\u67e5\u8be2<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u5ef6\u8fdf\u52a0\u8f7d<\/h4>\n\n\n\n<p>\u8bf4\u5230\u5206\u5e03\u67e5\u8be2\uff0c\u4e0d\u5f97\u4e0d\u63d0\u7684\u662f\u5ef6\u8fdf\u52a0\u8f7d\u3002\u5206\u6b65\u67e5\u8be2\u662f\u5411\u6570\u636e\u5e93\u53d1\u9001\u4e86\u4e24\u6761sql\u8bed\u53e5\uff0c\u4f46\u662f\u6211\u4eec\u6709\u7684\u65f6\u5019\u4e0d\u9700\u8981\u67e5\u8be2\u53e6\u4e00\u4e2a\u8868\u7684\u6d88\u606f\uff0c\u800c\u662f\u5c31\u67e5\u81ea\u5df1\u8868\u7684\u4fe1\u606f\u5c31\u53ef\u4ee5\u4e86\uff0c\u6240\u4ee5\u5b83\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5230\u5ef6\u8fdf\u61d2\u52a0\u8f7d\u7684\u529f\u80fd\uff0c\u4e5f\u5c31\u662f\u4e0d\u53bb\u6267\u884c\u53e6\u4e00\u6761\u5d4c\u5957\u6267\u884c\u8bed\u53e5\u3002<\/p>\n\n\n\n<p>\u5f00\u542f\u5ef6\u8fdf\u52a0\u8f7d\u7684\u65b9\u6cd5\u5341\u5206\u7684\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5728mybatis\u7a0d\u5fae\u52a0\u4e00\u4e0b\u914d\u7f6e\u4e00\u4e0b\uff0c\u5e76\u4e14\u5728resultMap\u7684\u6807\u7b7e\u52a0\u4e0a<code>fetchType=\"lazy\"<\/code>\uff0c\u8fd9\u6837\u8bbe\u7f6e\u540e\uff0c\u53ea\u6709\u5728\u6d4b\u8bd5\u6216\u8005\u9700\u8981\u8c03\u7528\u6570\u636e\u5e93\u65b9\u6cd5\u7684\u65f6\u5019\uff0c\u989d\u5916\u8c03\u7528getTeacher()\u65b9\u6cd5\u83b7\u53d6Teacher\u65f6\uff0c\u624d\u4f1a\u53bb\u6267\u884c\u4f8b\u5982selectByTid\u8fd9\u6837\u7684\u5d4c\u5957\u67e5\u8be2\u64cd\u4f5c\uff01<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;settings&gt;<br>&lt;!-- \u5ef6\u8fdf\u52a0\u8f7d\u914d\u7f6e,3.4.5\u7248\u672c\u9ed8\u8ba4\u4e3afalse\uff0c\u5f53\u4e3atrue\u65f6&lt;association&gt;\u6807\u7b7e\u7684fetchType=\"lazy\"\u65e0\u6548\uff0c\u8981\u751f\u6548\u5fc5\u987b\u4e3afalse--&gt;<br>    &lt;setting name=\"aggressiveLazyLoading\" value=\"false\" \/&gt;<br>&lt;\/settings&gt;<br>\u200b<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u8fde\u63a5\u67e5\u8be2<\/h3>\n\n\n\n<p>\u5728\u8bf4\u8054\u5408\u67e5\u8be2\u4e4b\u524d\uff0c\u53ef\u4ee5\u5148\u590d\u4e60\u4e00\u4e0b4\u79cd<a href=\"https:\/\/cloud.tencent.com\/developer\/article\/1639849\">\u8fde\u63a5\u67e5\u8be2<\/a>\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>left join \uff08\u5de6\u8fde\u63a5\uff09\uff1a\u8fd4\u56de\u5305\u62ec\u5de6\u8868\u4e2d\u7684\u6240\u6709\u8bb0\u5f55\u548c\u53f3\u8868\u4e2d\u8fde\u63a5\u5b57\u6bb5\u76f8\u7b49\u7684\u8bb0\u5f55\u3002<\/li>\n\n\n\n<li>right join \uff08\u53f3\u8fde\u63a5\uff09\uff1a\u8fd4\u56de\u5305\u62ec\u53f3\u8868\u4e2d\u7684\u6240\u6709\u8bb0\u5f55\u548c\u5de6\u8868\u4e2d\u8fde\u63a5\u5b57\u6bb5\u76f8\u7b49\u7684\u8bb0\u5f55\u3002<\/li>\n\n\n\n<li>inner join \uff08\u7b49\u503c\u8fde\u63a5\u6216\u8005\u53eb\u5185\u8fde\u63a5\uff09\uff1a\u53ea\u8fd4\u56de\u4e24\u4e2a\u8868\u4e2d\u8fde\u63a5\u5b57\u6bb5\u76f8\u7b49\u7684\u884c\u3002<\/li>\n\n\n\n<li>full join \uff08\u5168\u5916\u8fde\u63a5\uff09\uff1a\u8fd4\u56de\u5de6\u53f3\u8868\u4e2d\u6240\u6709\u7684\u8bb0\u5f55\u548c\u5de6\u53f3\u8868\u4e2d\u8fde\u63a5\u5b57\u6bb5\u76f8\u7b49\u7684\u8bb0\u5f55\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f8b\u5b503<\/h3>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u67e5\u8be2\u4e00\u65b9\uff0c\u4e5f\u5c31\u662f\u67e5\u8be2\u8001\u5e08\u65b9\uff0c\u628a\u6559\u7684\u5b66\u751f\u4e5f\u90fd\u67e5\u8be2\u51fa\u6765\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public List&lt;Teacher&gt; selectAll2();<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!-- &nbsp;  \u8054\u5408\u67e5\u8be2--&gt;<br>&lt;resultMap id=\"teacher2\" type=\"com.vo.Teacher\"&gt;<br> &nbsp; &nbsp; &nbsp;  &lt;id column=\"tid\" property=\"id\"&gt;&lt;\/id&gt;<br> &nbsp; &nbsp; &nbsp;  &lt;result column=\"tname\" property=\"name\"&gt;&lt;\/result&gt;<br>&lt;!-- \u4e00\u5bf9\u591a\uff0c\u67e5\u8be2\u591a\u65b9,\u4e3b\u8868\u6709\u96c6\u5408\u7684\uff0c\u4e00\u5b9a\u4f1a\u6709resultMap\uff0c\u4e14\u6309ofType\u53bb\u5c01\u88c5--&gt;<br> &nbsp; &nbsp; &nbsp;  &lt;collection property=\"students\" ofType=\"Student\"&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &lt;id column=\"sid\" property=\"id\"&gt;&lt;\/id&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &lt;result column=\"sname\" property=\"name\"&gt;&lt;\/result&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &lt;result column=\"tid\" property=\"tid\"&gt;&lt;\/result&gt;<br> &nbsp; &nbsp; &nbsp;  &lt;\/collection&gt;<br>&lt;\/resultMap&gt;<br>\u200b<br>&lt;!-- &nbsp;  \u5de6\u8fde\u63a5--&gt;<br> &nbsp;  &lt;select id=\"selectAll2\" resultMap=\"teacher2\"&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;select t.id tid, t.name tname, s.id sid, s.name sname, tid<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FROM teacher t LEFT JOIN student s ON s.tid = t.id<br> &nbsp;  &lt;\/select&gt;<\/pre>\n\n\n\n<p>\u8fd9\u6837\u7684\u8fde\u63a5\u67e5\u8be2\u7684\u7528\u6cd5\u3001\u5199\u6cd5\u548c\u5206\u6b65\u67e5\u8be2\u6709\u4ec0\u4e48\u533a\u522b\u5462\uff1f<\/p>\n\n\n\n<p>\u7b2c\u4e00\u80af\u5b9a\u662f\u67e5\u8be2\u7684sql\u8bed\u53e5\u5199\u6210\u4e24\u5f20\u8868\u7684\u8054\u5408\u67e5\u8be2\u5f62\u5f0f\uff08\u6211\u5199\u7684\u4f8b\u5b50\u662f\u5de6\u8fde\u63a5\uff09\u5566\uff01\u90a3\u4e48\u7b2c\u4e8c\u70b9\uff0c\u91cd\u70b9\u770b\u770bresultMap\u91cc\u7684collection\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u53ef\u4ee5\u770b\u5230column\u91cc\u586b\u5199\u7684\u4e0d\u518d\u662f\u6570\u636e\u5e93\u7684\u5217\u540d\u4e86\uff0c\u800c\u662fsql\u8bed\u53e5\u91cc\u7ed9\u5217\u540d\u5b9a\u4e49\u7684\u522b\u540d\u4e86\uff08\u53ef\u4ee5\u81ea\u5df1\u4fee\u6539\u522b\u540d\u9a8c\u8bc1\u4e00\u4e0b\uff09\u3002\u4f46\u662fproperty\u4f9d\u7136\u5bf9\u5e94\u7684Teacher\u7c7b\u7684\u5c5e\u6027\u540d\u5b57\u3002<\/li>\n\n\n\n<li>collection\u91cc\u4e0d\u518d\u8fdb\u884c\u7b2c\u4e8c\u6b21\u7684select\u67e5\u8be2\u4e86\uff0c\u800c\u662f\u76f4\u63a5\u5217\u540d\u4e0e\u5c5e\u6027\u540d\u5bf9\u5e94\u8d77\u6765\u3002mybatis\u4f1a\u5e2e\u5fd9\u6620\u5c04\u7ed3\u679c\u3002<\/li>\n\n\n\n<li>\u6709\u4e00\u4e2a\u65b0\u7684 \u201cofType\u201d \u5c5e\u6027\u3002\u8fd9\u4e2a\u5c5e\u6027\u975e\u5e38\u91cd\u8981\uff0c\u5b83\u7528\u6765\u5c06 JavaBean\uff08\u6216\u5b57\u6bb5\uff09\u5c5e\u6027\u7684\u7c7b\u578b\u548c\u96c6\u5408\u5b58\u50a8\u7684\u7c7b\u578b\u533a\u5206\u5f00\u6765\u3002\u6216\u8005\u5728\u6211\u7406\u89e3\u770b\uff0c\u7528ofType\u7684\u539f\u56e0\u7684\u89e3\u91ca\u5c31\u662fTeacher\u7c7b\u91cc\u6709\u4e00\u4e2a\u5c5e\u6027\u662fList&lt;Student&gt;\uff0c\u5b83\u4f1a\u628a\u67e5\u8be2\u5230\u7684sid\u3001sname\u3001tid\u7ed3\u679c\u5c01\u88c5\u6210Student\u5bf9\u8c61\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f8b\u5b504<\/h3>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u67e5\u8be2\u4e00\u5bf9\u591a\u7684\u591a\u65b9\uff0c\u4e5f\u5c31\u662f\u67e5\u5b66\u751f\uff0c\u60f3\u628a\u8001\u5e08\u7684\u4fe1\u606f\u4e5f\u67e5\u51fa\u6765\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public List&lt;Student&gt; selectAll();<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!-- &nbsp;  \u5de6\u8fde\u63a5\u67e5\u8be2\u4e00\u5bf9\u591a\u7684\u591a\u65b9--&gt;<br>&lt;select id=\"selectAll\" resultType=\"com.vo.Student\"&gt;<br> &nbsp; SELECT s.id id , s.name name, tid, t.id `teacher.id` , t.name `teacher.name` FROM student s LEFT JOIN teacher t ON s.tid = t.id<br>&lt;\/select&gt;<\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u67e5\u8be2\u4e2d\u7684<code>teacher.id<\/code>\u91cc\u7684teacher\u554a\uff0c\u5207\u8bb0\u662fStudent\u7c7b\u91cc\u7684\u5c5e\u6027teacher\uff0c\u5c5e\u6027\u540d\u662f\u5565\u5c31\u5199\u5565\u4e0d\u80fd\u4e71\u5199\u3002\u7531\u4e8e\u4e00\u4e2a\u5b66\u751f\u76f8\u5bf9\u4e8e\u4e00\u4e2a\u8001\u5e08\u5c31\u662f\u4e00\u5bf9\u4e00\u7684\u5173\u7cfb\uff0c\u4f7f\u7528\u8fd9\u79cd\u8fde\u63a5\u67e5\u8be2\u7684\u65b9\u5f0f\uff0c\u5c31\u4e0d\u9700\u8981\u4f7f\u7528resultMap\u548cresultMap\u7684association\u5566~~~~\u4e00\u4e2asql\u8bed\u53e5\u5c31\u641e\u5f97\u4e86\u5462\uff01<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">N-N<\/h2>\n\n\n\n<p>\u591a\u5bf9\u591a\u5462\uff0c\u6211\u626f\u4e86\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5c31\u662f\u591a\u6dfb\u52a0\u4e86\u4e00\u4e2a\u8bfe\u7a0b\u8868\uff0c\u56e0\u4e3a\u4e00\u4e2a\u5b66\u751f\u53ef\u4ee5\u9009\u5f88\u591a\u8bfe\uff0c\u4e00\u95e8\u8bfe\u53c8\u53ef\u4ee5\u88ab\u5f88\u591a\u5b66\u751f\u9009\u3002\u540c\u6837\u7684\uff0c\u6211\u4f9d\u7136\u7528\u4e0a\u9762\u4e24\u79cd\u65b9\u5f0f\u6f14\u793a\uff0c\u800c\u4e14\u591a\u5bf9\u591a\u554a\u5176\u5b9e\u548c\u4e00\u5bf9\u591a\u6ca1\u591a\u533a\u522b\uff0c\u800c\u4e14\u6211\u5c31\u6f14\u793a\u4e00\u79cd\u60c5\u51b5\u5c31\u53ef\u4ee5\u4e86\uff01\u67e5\u8be2\u9009\u8bfe\u60c5\u51b5\u2014\u2014\u6bcf\u95e8\u8bfe\u7684\u9009\u8bfe\u5b66\u751f\u4fe1\u606f\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5206\u6b65\u67e5\u8be2<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">\/**<br> * \u5206\u6b65\u67e5\u8be2\u6bcf\u95e8\u8bfe\u53ca\u9009\u8bfe\u5b66\u751f\u60c5\u51b5<br> *\/<br>public List&lt;Course&gt; queryCourseStu();<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!-- resultMap:\u6620\u5c04\u5b9e\u4f53\u7c7b\u548c\u5b57\u6bb5\u4e4b\u95f4\u7684\u4e00\u4e00\u5bf9\u5e94\u7684\u5173\u7cfb --&gt;<br>&lt;resultMap id=\"studentCourseMap\" type=\"com.vo.Course\"&gt;<br> &nbsp;  &lt;id property=\"id\" column=\"cid\" \/&gt;<br> &nbsp;  &lt;result property=\"name\" column=\"cname\" \/&gt;<br> &nbsp;  &lt;!-- \u591a\u5bf9\u591a\u5173\u8054\u6620\u5c04\uff1acollection --&gt;<br> &nbsp;  &lt;collection property=\"students\"<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  ofType=\"com.vo.Student\"<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  column=\"sid\"<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;select=\"com.mapper.IStudent.helpCourse\"<br> &nbsp;  &gt;<br> &nbsp;  &lt;\/collection&gt;<br>&lt;\/resultMap&gt;<br>\u200b<br>&lt;select id=\"queryCourseStu\" resultMap=\"studentCourseMap\"&gt;<br> &nbsp; &nbsp;SELECT<br> &nbsp;  student.id as sid, student.name as sname, course.id as cid, course.name as cname<br> &nbsp; &nbsp;FROM student_course sc, student, course<br> &nbsp; &nbsp;WHERE sc.sid = student.id AND sc.cid = course.id<br>&lt;\/select&gt;<\/pre>\n\n\n\n<p>\u7531\u4e8e\u662f\u5206\u6b65\u67e5\u8be2\uff0c\u6240\u4ee5\u8fd8\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u628a\u67e5\u5230\u7684sid\u53bb\u5b66\u751f\u8868\u67e5\u8be2\u5b66\u751f\u4fe1\u606f\u7684\u67e5\u8be2\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/**<br> * \u7528\u4ee5\u9009\u8bfe\u60c5\u51b5\u7684\u5206\u6b65\u67e5\u8be2<br> * @return<br> *\/<br>public List&lt;Student&gt; helpCourse();<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;select id=\"helpCourse\" parameterType=\"int\" resultType=\"Student\"&gt;<br> &nbsp; &nbsp;select id, name from student where id = #{sid}<br>&lt;\/select&gt;<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u8fde\u63a5\u67e5\u8be2<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">public interface ICourse {<br> &nbsp; &nbsp;\/**<br> &nbsp; &nbsp; * \u67e5\u8be2\u6bcf\u95e8\u8bfe\u5373\u9009\u8bfe\u60c5\u51b5<br> &nbsp; &nbsp; * @return<br> &nbsp; &nbsp; *\/<br> &nbsp; &nbsp;public List&lt;Course&gt; queryCourseStu();<br>}<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!-- resultMap:\u6620\u5c04\u5b9e\u4f53\u7c7b\u548c\u5b57\u6bb5\u4e4b\u95f4\u7684\u4e00\u4e00\u5bf9\u5e94\u7684\u5173\u7cfb --&gt;<br>&lt;resultMap id=\"studentCourseMap\" type=\"com.vo.Course\"&gt;<br> &nbsp;  &lt;id property=\"id\" column=\"cid\" \/&gt;<br> &nbsp;  &lt;result property=\"name\" column=\"cname\" \/&gt;<br> &nbsp;  &lt;!-- \u591a\u5bf9\u591a\u5173\u8054\u6620\u5c04\uff1acollection --&gt;<br> &nbsp;  &lt;collection property=\"students\" ofType=\"com.vo.Student\"&gt;<br> &nbsp; &nbsp; &nbsp;  &lt;id property=\"id\" column=\"sid\" \/&gt;<br> &nbsp; &nbsp; &nbsp;  &lt;result property=\"name\" column=\"sname\" \/&gt;<br> &nbsp;  &lt;\/collection&gt;<br>&lt;\/resultMap&gt;<br>\u200b<br>&lt;!-- sql\u8bed\u53e5\u5199\u6cd51 --&gt;<br>&lt;select id=\"queryCourseStu\" resultMap=\"studentCourseMap\"&gt;<br> &nbsp; &nbsp;select<br> &nbsp;  s.id as sid, s.name as sname, c.id as cid, c.name as cname<br> &nbsp; &nbsp;from<br> &nbsp;  student_course sc<br> &nbsp;  LEFT OUTER JOIN course c ON c.id = sc.cid<br> &nbsp;  LEFT OUTER JOIN student s ON s.id = sc.sid<br>&lt;\/select&gt;<br>&lt;!-- sql\u8bed\u53e5\u5199\u6cd52 --&gt;<br>&lt;select id=\"queryCourseStu\" resultMap=\"studentCourseMap\"&gt;<br> &nbsp; &nbsp;select<br> &nbsp;  s.id as sid, s.name as sname, c.id as cid, c.name as cname<br> &nbsp; &nbsp;from<br> &nbsp;  student s,course c,student_course sc<br> &nbsp; &nbsp;where s.id=sc.sid<br> &nbsp; &nbsp;and c.id=sc.cid<br>&lt;\/select&gt;<\/pre>\n\n\n\n<p>\u8fd9\u91cc\u5b66\u751f\u548c\u8bfe\u7a0b\u5c31\u662f\u591a\u5bf9\u591a\u7684\u5173\u7cfb\uff0c\u6240\u4ee5resultMap\u91cc\u7528\u7684\u5c31\u662fcollection\u5566~\u5176\u4ed6\u7684\u5c31\u6ca1\u4ec0\u4e48\u4e86~~~\u554a\u554a\u554a\u554a\u597d\u5f00\u5fc3\u554a\uff01<\/p>\n\n\n\n<p>\u53c2\u8003\uff1a <\/p>\n\n\n\n<p>\u2460<a href=\"https:\/\/mybatis.org\/mybatis-3\/zh\/sqlmap-xml.html#Result_Maps\"> mybatis\u5b98\u65b9\u6587\u6863<\/a><\/p>\n\n\n\n<p>\u2461<a href=\"https:\/\/blog.csdn.net\/codejas\/article\/details\/79527393\"> MyBatis\u4f7f\u7528resultMap\u81ea\u5b9a\u4e49\u6620\u5c04\u89c4\u5219\u4e0e\u5173\u8054\u6620\u5c04<\/a><\/p>\n\n\n\n<p>\u2462<a href=\"https:\/\/stackoverflow.com\/questions\/12425384\/difference-between-collection-and-association-mapping-in-mybatis-3\"> difference-between-collection-and-association<\/a><\/p>\n\n\n\n<p>\u2463<a href=\"https:\/\/dev-tang.com\/post\/2020\/04\/mybatis-one-to-many.html\"> \u591a\u5bf9\u591a\u67e5\u8be2<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53c8\u5728\u5468\u4e00\u7684\u8bfe\u4e0a\u5199\u4e86\u6ce8\u89e3\u7684\u591a\u8868\u67e5\u8be2&#8230;&#8230;\u6280\u80fd+1<\/p>\n","protected":false},"author":1,"featured_media":244,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[25],"class_list":["post-240","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-22","tag-mybatis"],"_links":{"self":[{"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=\/wp\/v2\/posts\/240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=240"}],"version-history":[{"count":4,"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=\/wp\/v2\/posts\/240\/revisions"}],"predecessor-version":[{"id":466,"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=\/wp\/v2\/posts\/240\/revisions\/466"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=\/wp\/v2\/media\/244"}],"wp:attachment":[{"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}