{"id":1992,"date":"2017-06-15T21:09:59","date_gmt":"2017-06-15T13:09:59","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=1992"},"modified":"2026-08-15T16:59:24","modified_gmt":"2026-08-15T08:59:24","slug":"mybatis%e5%a4%9a%e8%a1%a8%e5%85%b3%e8%81%94%e6%9f%a5%e8%af%a2%e4%b9%8b%e5%a4%9a%e5%af%b9%e5%a4%9a","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=1992","title":{"rendered":"Mybatis\u591a\u8868\u5173\u8054\u67e5\u8be2\u4e4b\u591a\u5bf9\u591a"},"content":{"rendered":"<p>Mybatis\u591a\u8868\u5173\u8054\u53ef\u5206\u6210\u56db\u79cd\u60c5\u51b5:\u4e00\u5bf9\u4e00, \u4e00\u5bf9\u591a, \u591a\u5bf9\u4e00, \u591a\u5bf9\u591a.\u5b9e\u73b0\u6620\u5c04\u7684\u5173\u952e\u662fresultMap\u7f51\u4e0a\u6709\u5f88\u591a\u4ecb\u7ecd\u53ef\u81ea\u884c\u641c\u7d22\u5b66\u4e60\uff0c\u5982:<a class=\"quote_title\" href=\"http:\/\/limingnihao.iteye.com\/blog\/781878\" target=\"_blank\" rel=\"noopener\">MyBatis\u5b66\u4e60 \u4e4b \u4e8c\u3001SQL\u8bed\u53e5\u6620\u5c04\u6587\u4ef6(1)resultMap<\/a><\/p>\n<h3>Mybatis\u591a\u8868\u5173\u8054\u67e5\u8be2\u4e4b\u591a\u5bf9\u591a\u5b9e\u73b0<br \/>\n\u8868\u7ed3\u6784\uff1a<\/h3>\n<p><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2017\/06\/many_many_table.jpg\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-3550\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2017\/06\/many_many_table.jpg\" alt=\"\" width=\"1352\" height=\"224\" srcset=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2017\/06\/many_many_table.jpg 1352w, https:\/\/ai.zhousir.top\/wp-content\/uploads\/2017\/06\/many_many_table-768x127.jpg 768w\" sizes=\"(max-width: 1352px) 100vw, 1352px\" \/><\/a><\/p>\n<h3>\u7c7b\u7ed3\u6784<\/h3>\n<p><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2017\/06\/many_many.jpg\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-3551\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2017\/06\/many_many.jpg\" alt=\"\" width=\"945\" height=\"219\" srcset=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2017\/06\/many_many.jpg 945w, https:\/\/ai.zhousir.top\/wp-content\/uploads\/2017\/06\/many_many-768x178.jpg 768w\" sizes=\"(max-width: 945px) 100vw, 945px\" \/><\/a><\/p>\n<h3>\u6620\u5c04\u6587\u4ef6:<\/h3>\n<h4>CoursesMapper.xml<\/h4>\n<pre>[java]&amp;lt;mapper namespace=&quot;com.ittx.mybatis.demo1.dao.CoursesDao&quot;&amp;gt;\n\t&amp;lt;resultMap type=&quot;com.ittx.mybatis.demo1.model.Courses&quot;\n\t\tid=&quot;coursesMap&quot;&amp;gt;\n\t\t&amp;lt;!-- \u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cmybatis\u4f1a\u81ea\u52a8\u5728TypeAliasRegistry\u521d\u59cb\u5316\u7684\u65f6\u5019\u6302\u5728\u5f88\u591ajdk\u5e38\u7528\u7c7b\uff0c \u6240\u4ee5javaType=&quot;java.lang.Integer&quot;\u53ef\u4ee5\u5199\u6210javaType=&quot;Integer&quot; --&amp;gt;\n\t\t&amp;lt;id property=&quot;id&quot; column=&quot;cid&quot; javaType=&quot;java.lang.Integer&quot; \/&amp;gt;\n\t\t&amp;lt;result property=&quot;name&quot; column=&quot;courses_name&quot; javaType=&quot;java.lang.String&quot; \/&amp;gt;\n\t&amp;lt;\/resultMap&amp;gt;\n\n\t&amp;lt;resultMap type=&quot;com.ittx.mybatis.demo1.model.Courses&quot;\n\t\tid=&quot;couAndStu&quot;&amp;gt;\n\t\t&amp;lt;id property=&quot;id&quot; column=&quot;cid&quot; javaType=&quot;java.lang.Integer&quot; \/&amp;gt;\n\t\t&amp;lt;result property=&quot;name&quot; column=&quot;courses_name&quot; javaType=&quot;java.lang.String&quot; \/&amp;gt;\n\n\t\t&amp;lt;!-- \u5bf9\u4e8e\u4e00\u4e2a\u5c5e\u6027\u7684\u7c7b\u578b\u662f\u4e00\u4e2a\u96c6\u5408\uff0c\u5c31\u4f7f\u7528collection \u5bf9\u4e8e\u4e00\u4e2a\u5c5e\u6027\u7684\u7c7b\u578b\u662f\u4e00\u4e2a\u7c7b\uff0c\u5c31\u4f7f\u7528association --&amp;gt;\n\t\t&amp;lt;collection property=&quot;student&quot; column=&quot;cid&quot;\n\t\t\tselect=&quot;findStudentByCourses&quot;&amp;gt;&amp;lt;\/collection&amp;gt;\n\t&amp;lt;\/resultMap&amp;gt;\n\n\t&amp;lt;!-- \u6709\u5b66\u751f\u8868\uff0c\u8bfe\u7a0b\u8868\u8fd9\u4e24\u5f20\u8868\u90fd\u6ca1\u6709\u5916\u952e\uff0c\u6211\u4eec\u5c31\u8981\u4f7f\u7528\u7b2c\u4e09\u5f20\u5173\u8054\u8868\u3002\u6211\u4eec\u5c31\u8981\u6839\u636e\u8bfe\u7a0b\u8868\u7684fk_cou_id\uff0c\u628a\u5b66\u751f\u7684id\u503c\u5f97\u5230\u3002 \u5bf9\u4e8e\u591a\u5bf9\u591a\u90a3\u4e48\u8fd9\u4e2a\u5b66\u751f\u7684id\u503c\u5c31\u4e0d\u53ef\u80fd\u662f\u4e00\u4e2a\u503c\n\t\t\u3002\u5728\u6570\u636e\u5e93\u91cc\u5c31\u8981\u4f7f\u7528in --&amp;gt;\n\t&amp;lt;select id=&quot;findStudentByCourses&quot; resultMap=&quot;com.ittx.mybatis.demo1.dao.StudentDao.studentMap&quot;&amp;gt;\n\t\tselect * from\n\t\tt_student where sid in (select fk_stu_id from t_stu_cou where\n\t\tfk_cou_id=#{id})\n\t&amp;lt;\/select&amp;gt;\n\t&amp;lt;!--resultMap=&quot;coursesMap&quot; \u53ea\u6839\u636e\u8bfe\u7a0bid\u627e\u8bfe\u7a0b\u4e0d\u5173\u8054\u9009\u4fee\u8bfe\u7a0b\u7684\u5b66\u751f  --&amp;gt;\n\t&amp;lt;select id=&quot;findCouById&quot; resultMap=&quot;coursesMap&quot;&amp;gt;\n\t\tselect * from t_courses where\n\t\tcid=#{id}\n\t&amp;lt;\/select&amp;gt;\n\t&amp;lt;!--resultMap=&quot;couAndStu&quot; \u53ea\u6839\u636e\u8bfe\u7a0bid\u627e\u8bfe\u7a0b\u540c\u65f6\u5173\u8054\u67e5\u8c12\u9009\u4fee\u8bfe\u7a0b\u7684\u5b66\u751f  --&amp;gt;\n\t&amp;lt;select id=&quot;findCouAndStu&quot; resultMap=&quot;couAndStu&quot;&amp;gt;\n\t\tselect * from t_courses\n\t\twhere cid=#{id}\n\t&amp;lt;\/select&amp;gt;\n&amp;lt;\/mapper&amp;gt;[\/java]<\/pre>\n<h4>StudentMapper.xml<\/h4>\n<pre>[java]&amp;lt;mapper namespace=&quot;com.ittx.mybatis.demo1.dao.StudentDao&quot;&amp;gt;\n\n\t&amp;lt;resultMap type=&quot;com.ittx.mybatis.demo1.model.Student&quot;\n\t\tid=&quot;studentMap&quot;&amp;gt;\n\t\t&amp;lt;id property=&quot;id&quot; column=&quot;sid&quot; javaType=&quot;java.lang.Integer&quot; \/&amp;gt;\n\t\t&amp;lt;result property=&quot;name&quot; column=&quot;student_name&quot; javaType=&quot;java.lang.String&quot; \/&amp;gt;\n\t&amp;lt;\/resultMap&amp;gt;\n\n\t&amp;lt;resultMap type=&quot;com.ittx.mybatis.demo1.model.Student&quot;\n\t\tid=&quot;studentAndCourses&quot;&amp;gt;\n\t\t&amp;lt;id property=&quot;id&quot; column=&quot;sid&quot; javaType=&quot;java.lang.Integer&quot; \/&amp;gt;\n\t\t&amp;lt;result property=&quot;name&quot; column=&quot;student_name&quot; javaType=&quot;java.lang.String&quot; \/&amp;gt;\n\t\t&amp;lt;collection property=&quot;courses&quot; column=&quot;sid&quot;\n\t\t\tselect=&quot;findCoursesByStudent&quot;&amp;gt;&amp;lt;\/collection&amp;gt;\n\t&amp;lt;\/resultMap&amp;gt;\n\n\t&amp;lt;select id=&quot;findCoursesByStudent&quot; resultMap=&quot;com.ittx.mybatis.demo1.dao.CoursesDao.coursesMap&quot;&amp;gt;\n\t\tselect * from t_courses where cid in (select fk_cou_id from t_stu_cou where\n\t\tfk_stu_id = #{id})\n\t&amp;lt;\/select&amp;gt;\n\t&amp;lt;!--resultMap=&quot;studentMap&quot; \u53ea\u6839\u636e\u5b66\u751fid\u627e\u5b66\u751f\u4e0d\u5173\u8054\u5b66\u751f\u9009\u4fee\u7684\u8bfe\u7a0b  --&amp;gt;\n\t&amp;lt;select id=&quot;findStuById&quot; resultMap=&quot;studentMap&quot;&amp;gt;\n\t\tselect * from t_student where sid = #{id}\n\t&amp;lt;\/select&amp;gt;\n\t&amp;lt;!--resultMap=&quot;studentAndCourses&quot; \u53ea\u6839\u636e\u5b66\u751fid\u627e\u5b66\u751f\u540c\u65f6\u5173\u8054\u67e5\u8be2\u5b66\u751f\u9009\u4fee\u7684\u8bfe\u7a0b  --&amp;gt;\n\t&amp;lt;select id=&quot;findStuAndCou&quot; resultMap=&quot;studentAndCourses&quot;&amp;gt;\n\t\tselect * from t_student where sid = #{id}\n\t&amp;lt;\/select&amp;gt;\n&amp;lt;\/mapper&amp;gt;[\/java]<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Mybatis\u591a\u8868\u5173\u8054\u53ef\u5206\u6210\u56db\u79cd\u60c5\u51b5:\u4e00\u5bf9\u4e00, \u4e00\u5bf9\u591a, \u591a\u5bf9\u4e00, \u591a\u5bf9\u591a.\u5b9e\u73b0\u6620\u5c04\u7684\u5173\u952e\u662fresultMap [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[92],"tags":[],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1992"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1992"}],"version-history":[{"count":6,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1992\/revisions"}],"predecessor-version":[{"id":3552,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1992\/revisions\/3552"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}