{"id":8555,"date":"2022-11-16T08:07:33","date_gmt":"2022-11-16T00:07:33","guid":{"rendered":"http:\/\/123.57.164.21\/?p=8555"},"modified":"2022-11-16T08:11:55","modified_gmt":"2022-11-16T00:11:55","slug":"mybatis-association-collection","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=8555","title":{"rendered":"mybatis association collection"},"content":{"rendered":"\n<p><strong>\u4e00\u3001resultMap\u7684\u4f7f\u7528<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>resultMap \u4e5f\u662f\u5b9a\u4e49\u8fd4\u56de\u503c\u7c7b\u578b\uff0c\u8fd4\u56de\u503c\u4e3a\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u7c7b\u578b\uff0c\u53ef\u7528\u4e8e\u89e3\u51b3JavaBean\u4e2d\u7684\u5c5e\u6027\u540d\u548c\u6570\u636e\u5e93\u4e2d\u7684\u5217\u540d\u4e0d\u4e00\u81f4\u7684\u60c5\u51b5<\/p>\n\n\n\n<p>\u4e4b\u524d\u5bf9\u4e8eJavaBean\u4e2d\u5c5e\u6027\u540d\u548c\u6570\u636e\u5e93\u4e2d\u7684\u5217\u540d\u4e0d\u4e00\u81f4\u7684\u60c5\u51b5\uff0c\u901a\u8fc7\u6709\u4e24\u79cd\u529e\u6cd5\uff0c1\u3001\u901a\u8fc7\u5728sql\u4e2d\u4f7f\u7528\u522b\u540d 2\u3001\u5982\u679c\u6b63\u597d\u7b26\u5408\u9a7c\u5cf0\u547d\u540d\uff0c\u9700\u8981\u5728settings\u4e2d\u914d\u7f6e\uff0c\u73b0\u5728\u53ef\u4ee5\u901a\u8fc7resultMap\u6765\u89e3\u51b3<\/p>\n\n\n\n<p>hotelMapper.xml<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n&lt;!DOCTYPE mapper PUBLIC \"-\/\/mybatis.org\/\/DTD Mapper 3.0\/\/EN\" \"http:\/\/mybatis.org\/dtd\/mybatis-3-mapper.dtd\" >\n&lt;mapper namespace=\"com.pjf.mybatis.dao.HotelMapper\">\n&lt;!-- resultMap\u5b9a\u4e49 \ntype:javaBean\u7684\u5168\u7c7b\u540d\uff0c\nid\u4e3a\u8be5resultMap\u7684\u552f\u4e00\u6807\u8bc6 -->\n&lt;resultMap type=\"com.pjf.mybatis.po.Hotel\" id=\"myHotel\">\n&lt;!--id \u6307\u5b9a\u4e3b\u952e\u7684\u5c01\u88c5\u89c4\u5219 \ncolumn\uff1a\u6570\u636e\u5e93\u4e2d\u5217\u540d \nproperty\uff1ajavaBean\u7684\u5c5e\u6027\u540d -->\n&lt;id column=\"id\" property=\"id\" jdbcType=\"INTEGER\" \/>\n&lt;!--result \u6307\u5b9a\u975e\u4e3b\u952e\u7684\u5c01\u88c5\u89c4\u5219\ncolumn\uff1a\u6570\u636e\u5e93\u4e2d\u5217\u540d\nproperty\uff1ajavaBean\u7684\u5c5e\u6027\u540d -->\n&lt;result column=\"hotel_name\" property=\"hotelName\" jdbcType=\"VARCHAR\" \/>\n&lt;result column=\"hotel_address\" property=\"hotelAddress\"\njdbcType=\"VARCHAR\" \/>\n&lt;result column=\"price\" property=\"price\" jdbcType=\"INTEGER\" \/>\n&lt;\/resultMap>\n\n&lt;!-- resultMap\u4f7f\u7528 -->\n&lt;select id=\"getHotel\" resultMap=\"myHotel\">\nselect * from hotel\nwhere\nid=#{id}\n&lt;\/select>\n&lt;\/mapper><\/pre>\n\n\n\n<p><strong>\u4e8c\u3001association\u7684\u4f7f\u7528<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>association\u548ccollection\u90fd\u662f\u7528\u6765\u5173\u8054\u53e6\u4e00\u4e2a\u8868\u7684\u6570\u636e\uff0c\u533a\u522b\u5c31\u662f\u7528\u6765\u5173\u8054\u5bf9\u8c61\u7684\u5c01\u88c5\u7684\uff0c\u800ccollection\u662f\u7528\u6765\u5173\u8054\u96c6\u5408\u5c01\u88c5\u7684\uff0c<\/p>\n\n\n\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u6bd4\u5982\u901a\u8fc7\u67e5\u8be2\u9152\u5e97\uff0c\u67e5\u51fa\u8be5\u9152\u5e97\u7684\u57ce\u5e02\uff0c\u662f\u4e00\u4e2a\u57ce\u5e02\u5bf9\u5e94\u4e00\u4e2a\u9152\u5e97\uff0c\u7528association<\/p>\n\n\n\n<p>\u800c\u67e5\u8be2\u4e00\u4e2a\u57ce\u5e02\u7684\u9152\u5e97\uff0c\u662f\u4e00\u5bf9\u591a\u7684\uff0c\u7528collection\uff0c\u4e0b\u9762\u6765\u5177\u4f53\u5b9e\u73b0\u4e0b\u8fd9\u4e2a\u4f8b\u5b50\u3002<\/p>\n\n\n\n<p><strong>1\u3001\u73af\u5883\u51c6\u5907<\/strong><\/p>\n\n\n\n<p>\u4fee\u6539hotel.java\u4ee3\u7801\uff0c\u589e\u52a0\u4e00\u79cd\u7c7b\u6210\u5458\u53d8\u91cfCity\uff0c\u901a\u8fc7\u67e5\u8be2\u9152\u5e97\uff0c\u76f4\u63a5\u67e5\u51fa\u4ed6\u6240\u5728\u7684\u57ce\u5e02<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package com.pjf.mybatis.po;\n\npublic class Hotel {\nprivate int id;\nprivate String hotelName;\nprivate String hotelAddress;\nprivate int price;\nprivate City city;\n\npublic int getId() {\nreturn id;\n}\n\npublic void setId(int id) {\nthis.id = id;\n}\n\npublic String getHotelName() {\nreturn hotelName;\n}\n\npublic void setHotelName(String hotelName) {\nthis.hotelName = hotelName;\n}\n\npublic String getHotelAddress() {\nreturn hotelAddress;\n}\n\npublic void setHotelAddress(String hotelAddress) {\nthis.hotelAddress = hotelAddress;\n}\n\npublic int getPrice() {\nreturn price;\n}\n\npublic void setPrice(int price) {\nthis.price = price;\n}\n\npublic City getCity() {\nreturn city;\n}\n\npublic void setCity(City city) {\nthis.city = city;\n}\n\n@Override\npublic String toString() {\nreturn \"Hotel [id=\" + id + \", hotelName=\" + hotelName + \", hotelAddress=\" + hotelAddress + \", price=\" + price\n+ \"]\";\n}\n}<\/pre>\n\n\n\n<p>\u589e\u52a0\u57ce\u5e02\u7c7b City.java<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package com.pjf.mybatis.po;\n\npublic class City {\nprivate int cityCode;\nprivate String cityName;\n\npublic int getCityCode() {\nreturn cityCode;\n}\n\npublic void setCityCode(int cityCode) {\nthis.cityCode = cityCode;\n}\n\npublic String getCityName() {\nreturn cityName;\n}\n\npublic void setCityName(String cityName) {\nthis.cityName = cityName;\n}\n\n@Override\npublic String toString() {\nreturn \"City [cityCode=\" + cityCode + \", cityName=\" + cityName + \"]\";\n}\n}<\/pre>\n\n\n\n<p>\u8fd8\u6709\u6570\u636e\u5e93\u7684\u4fee\u6539\uff0chotel\u8868\u4e2d\u589e\u52a0\u4e00\u5217city_code\uff0c\u65b0\u589e\u4e00\u4e2acity\u8868\uff0c<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-221-1024x189.png\" alt=\"\" class=\"wp-image-8556\" width=\"498\" height=\"92\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-221-1024x189.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-221-300x55.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-221-768x142.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-221-830x153.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-221-230x43.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-221-350x65.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-221-480x89.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-221.png 1504w\" sizes=\"(max-width: 498px) 100vw, 498px\" \/><figcaption>hotel\u8868<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-222.png\" alt=\"\" class=\"wp-image-8557\" width=\"229\" height=\"109\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-222.png 434w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-222-300x142.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-222-230x109.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-222-350x166.png 350w\" sizes=\"(max-width: 229px) 100vw, 229px\" \/><figcaption>city\u8868<\/figcaption><\/figure><\/div>\n\n\n\n<p>hotelMapper\u63a5\u53e3\u4e0d\u53d8<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package com.pjf.mybatis.dao;\n\nimport com.pjf.mybatis.po.Hotel;\n\npublic interface HotelMapper {\n\n    public Hotel getHotel(Integer i);\n}<\/pre>\n\n\n\n<p><strong>\u00a02\u3001association\u7684\u4f7f\u7528<\/strong><\/p>\n\n\n\n<p>\u901a\u8fc7association\u6765\u5173\u8054city\u8868\uff0c\u4f7f\u7528\u89c4\u5219\u5982\u4e0b<\/p>\n\n\n\n<p>hotelMapper.xml<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n&lt;!DOCTYPE mapper PUBLIC \"-\/\/mybatis.org\/\/DTD Mapper 3.0\/\/EN\" \"http:\/\/mybatis.org\/dtd\/mybatis-3-mapper.dtd\" >\n&lt;mapper namespace=\"com.pjf.mybatis.dao.HotelMapper\">\n    &lt;!-- resultMap\u5b9a\u4e49 type:javaBean\u7684\u5168\u7c7b\u540d\uff0c id\u4e3a\u8be5resultMap\u7684\u552f\u4e00\u6807\u8bc6 -->\n    &lt;resultMap type=\"com.pjf.mybatis.po.Hotel\" id=\"myHotel\">\n        &lt;!--id \u6307\u5b9a\u4e3b\u952e\u7684\u5c01\u88c5\u89c4\u5219 column\uff1a\u6570\u636e\u5e93\u4e2d\u5217\u540d property\uff1ajavaBean\u7684\u5c5e\u6027\u540d -->\n        &lt;id column=\"id\" property=\"id\" jdbcType=\"INTEGER\" \/>\n        &lt;!--result \u6307\u5b9a\u975e\u4e3b\u952e\u7684\u5c01\u88c5\u89c4\u5219 column\uff1a\u6570\u636e\u5e93\u4e2d\u5217\u540d property\uff1ajavaBean\u7684\u5c5e\u6027\u540d -->\n        &lt;result column=\"hotel_name\" property=\"hotelName\" jdbcType=\"VARCHAR\" \/>\n        &lt;result column=\"hotel_address\" property=\"hotelAddress\"\n            jdbcType=\"VARCHAR\" \/>\n        &lt;result column=\"price\" property=\"price\" jdbcType=\"INTEGER\" \/>\n        \n        &lt;!--association \u5173\u8054\u7684\u8868\n               property \u6307\u88ab\u5173\u8054\u7684\u7c7b\u6210\u5458\u53d8\u91cf  \n               javaType \u6307\u88ab\u5173\u8054\u7684\u7c7b\u6210\u5458\u53d8\u91cf\u7684\u5168\u7c7b\u540d   -->\n        &lt;association property=\"city\" javaType=\"com.pjf.mybatis.po.City\">\n            &lt;id column=\"city_code\" property=\"cityCode\" jdbcType=\"INTEGER\"\/>\n            &lt;result column=\"city_name\" property=\"cityName\" jdbcType=\"VARCHAR\"\/>\n        &lt;\/association>\n    &lt;\/resultMap>\n\n    &lt;!-- resultMap\u4f7f\u7528 -->\n    &lt;select id=\"getHotel\" resultMap=\"myHotel\">\n        select\n        h.id,h.hotel_name,h.hotel_address,h.price,c.city_code,c.city_name from\n        hotel h ,city c\n        where\n        h.city_code=c.city_code and h.id=#{id}\n    &lt;\/select>\n&lt;\/mapper><\/pre>\n\n\n\n<p>\u6d4b\u8bd5\u7c7b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package com.pjf.mybatis;\n\nimport java.io.IOException;\nimport java.io.InputStream;\nimport java.util.Map;\n\nimport org.apache.ibatis.io.Resources;\nimport org.apache.ibatis.session.SqlSession;\nimport org.apache.ibatis.session.SqlSessionFactory;\nimport org.apache.ibatis.session.SqlSessionFactoryBuilder;\nimport org.junit.Test;\n\nimport com.pjf.mybatis.dao.HotelMapper;\nimport com.pjf.mybatis.po.Hotel;\n\npublic class TestHotel {\n\n    public SqlSessionFactory sqlSessionFactory() throws IOException {\n        \/\/ mybatis\u7684\u914d\u7f6e\u6587\u4ef6\n        String resource = \"mybatis_config.xml\";\n        \/\/ \u4f7f\u7528\u7c7b\u52a0\u8f7d\u5668\u52a0\u8f7dmybatis\u7684\u914d\u7f6e\u6587\u4ef6\uff08\u5b83\u4e5f\u52a0\u8f7d\u5173\u8054\u7684\u6620\u5c04\u6587\u4ef6\uff09TestHotel.class.getClassLoader()\n        InputStream is = Resources.getResourceAsStream(resource);\n        \/\/ \u6784\u5efasqlSession\u7684\u5de5\u5382\n        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);\n        return sessionFactory;\n    }\n\n    \/\/ \u67e5\n    @Test\n    public void getHotel() throws IOException {\n\n        SqlSessionFactory sessionFactory = sqlSessionFactory();\n        SqlSession session = sessionFactory.openSession();\n        HotelMapper hotelMapper = session.getMapper(HotelMapper.class);\n        System.out.println(hotelMapper.getClass());\n        Hotel hotel = hotelMapper.getHotel(1004);\n        \/\/ \u6253\u5370\u9152\u5e97\n        System.out.println(hotel);\n        \/\/ \u6253\u5370\u57ce\u5e02\n        System.out.println(hotel.getCity());\n        session.close();\n    }\n}<\/pre>\n\n\n\n<p>\u8fd9\u65f6\u5019\u5c31\u53ef\u4ee5\u770b\u5230\u7ed3\u679c\u4e86<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-223-1024x145.png\" alt=\"\" class=\"wp-image-8558\" width=\"524\" height=\"73\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-223-1024x145.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-223-300x42.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-223-768x109.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-223-1536x217.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-223-2048x289.png 2048w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-223-830x117.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-223-230x33.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-223-350x49.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-223-480x68.png 480w\" sizes=\"(max-width: 524px) 100vw, 524px\" \/><\/figure><\/div>\n\n\n\n<p><strong>\u4e09\u3001collection\u7684\u4f7f\u7528<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5b9e\u4f8b\uff1a\u67e5\u8be2\u67d0\u4e2a\u57ce\u5e02\u7684\u5168\u90e8\u9152\u5e97<\/p>\n\n\n\n<p>\u4fee\u6539city\u7c7b<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package com.pjf.mybatis.po;\n\nimport java.util.List;\n\npublic class City {\n    private int cityCode;\n    private String cityName;\n    private List&lt;Hotel> hotel;\n\n    public List&lt;Hotel> getHotel() {\n        return hotel;\n    }\n\n    public void setHotel(List&lt;Hotel> hotel) {\n        this.hotel = hotel;\n    }\n\n    public int getCityCode() {\n        return cityCode;\n    }\n\n    public void setCityCode(int cityCode) {\n        this.cityCode = cityCode;\n    }\n\n    public String getCityName() {\n        return cityName;\n    }\n\n    public void setCityName(String cityName) {\n        this.cityName = cityName;\n    }\n\n    @Override\n    public String toString() {\n        return \"City [cityCode=\" + cityCode + \", cityName=\" + cityName + \"]\";\n    }\n\n}<\/pre>\n\n\n\n<p>\u4fee\u6539hotelMapper\u63a5\u53e3\uff08\u53ef\u4ee5\u91cd\u65b0\u5b9a\u4e49\u4e00\u4e2a\u63a5\u53e3\u548cmapper.xml\u6587\u4ef6\uff09<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package com.pjf.mybatis.dao;\n\nimport com.pjf.mybatis.po.City;\n\npublic interface HotelMapper {\n\n    public City  getCityHotel(Integer i);\n}<\/pre>\n\n\n\n<p>hotelMapper.xml\u6587\u4ef6<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n&lt;!DOCTYPE mapper PUBLIC \"-\/\/mybatis.org\/\/DTD Mapper 3.0\/\/EN\" \"http:\/\/mybatis.org\/dtd\/mybatis-3-mapper.dtd\" >\n&lt;mapper namespace=\"com.pjf.mybatis.dao.HotelMapper\">\n    &lt;resultMap type=\"com.pjf.mybatis.po.City\" id=\"cityHotel\">\n     &lt;id column=\"city_code\" property=\"cityCode\"\/>\n     &lt;result column=\"city_name\" property=\"cityName\"\/>\n     &lt;!--collection\u88ab\u5173\u8054\u7684\u96c6\u5408 \n           ofType\u88ab\u5173\u8054\u96c6\u5408\u5143\u7d20\u7684\u5168\u7c7b\u540d\n     -->\n     &lt;collection property=\"hotel\" ofType=\"com.pjf.mybatis.po.Hotel\">\n        &lt;id column=\"id\" property=\"id\"\/>\n        &lt;result column=\"hotel_name\" property=\"hotelName\"\/>\n        &lt;result column=\"hotel_address\" property=\"hotelAddress\"\/>\n        &lt;result column=\"price\" property=\"price\"\/>\n     &lt;\/collection>\n    &lt;\/resultMap>\n    \n    &lt;select id=\"getCityHotel\" resultMap=\"cityHotel\">\n       SELECT c.city_code,c.city_name ,h.id,h.hotel_name,h.hotel_address,h.price \n       FROM city c LEFT JOIN hotel h ON c.city_code=h.city_code \n       WHERE c.city_code=#{cityCode}\n    &lt;\/select>\n&lt;\/mapper><\/pre>\n\n\n\n<p>\u6d4b\u8bd5\u7c7b<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package com.pjf.mybatis;\n\nimport java.io.IOException;\nimport java.io.InputStream;\nimport java.util.Map;\n\nimport org.apache.ibatis.io.Resources;\nimport org.apache.ibatis.session.SqlSession;\nimport org.apache.ibatis.session.SqlSessionFactory;\nimport org.apache.ibatis.session.SqlSessionFactoryBuilder;\nimport org.junit.Test;\n\nimport com.pjf.mybatis.dao.HotelMapper;\nimport com.pjf.mybatis.po.City;\nimport com.pjf.mybatis.po.Hotel;\n\npublic class TestHotel {\n\n    public SqlSessionFactory sqlSessionFactory() throws IOException {\n        \/\/ mybatis\u7684\u914d\u7f6e\u6587\u4ef6\n        String resource = \"mybatis_config.xml\";\n        \/\/ \u4f7f\u7528\u7c7b\u52a0\u8f7d\u5668\u52a0\u8f7dmybatis\u7684\u914d\u7f6e\u6587\u4ef6\uff08\u5b83\u4e5f\u52a0\u8f7d\u5173\u8054\u7684\u6620\u5c04\u6587\u4ef6\uff09TestHotel.class.getClassLoader()\n        InputStream is = Resources.getResourceAsStream(resource);\n        \/\/ \u6784\u5efasqlSession\u7684\u5de5\u5382\n        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);\n        return sessionFactory;\n    }\n\n    \/\/ \u67e5\n    @Test\n    public void getHotel() throws IOException {\n\n        SqlSessionFactory sessionFactory = sqlSessionFactory();\n        SqlSession session = sessionFactory.openSession();\n        HotelMapper hotelMapper = session.getMapper(HotelMapper.class);\n        System.out.println(hotelMapper.getClass());\n        City city = hotelMapper.getCityHotel(1);\n        \/\/ \u6253\u5370\u57ce\u5e02\n        System.out.println(city);\n        \/\/ \u6253\u5370\u9152\u5e97\n        for (Hotel  hotel : city.getHotel()) {\n            System.out.println(hotel);\n        }    \n        session.close();\n    }\n}<\/pre>\n\n\n\n<p>\u67e5\u770b\u7ed3\u679c<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-224-1024x148.png\" alt=\"\" class=\"wp-image-8560\" width=\"580\" height=\"84\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-224-1024x148.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-224-300x43.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-224-768x111.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-224-1536x223.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-224-2048x297.png 2048w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-224-830x120.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-224-230x33.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-224-350x51.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-224-480x70.png 480w\" sizes=\"(max-width: 580px) 100vw, 580px\" \/><\/figure><\/div>\n\n\n\n<p><strong>\u56db\u3001association \u5206\u6b65\u67e5\u8be2<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u573a\u666f\uff1a\u5728\u5b9e\u9645\u7684\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u5f80\u5f80\u4f1a\u4ece\u591a\u4e2a\u8868\u91cc\u83b7\u53d6\u6570\u636e\u3002\u6b64\u65f6\u9700\u8981\u7528\u5230\u591a\u8868\u64cd\u4f5c\u3002\u5982\u67e5\u8be2\u7528\u6237\u7684\u4e2a\u4eba\u4fe1\u606f\u53ca\u90e8\u95e8\u4fe1\u606f\u3002<\/p>\n\n\n\n<p>\u5b9e\u4f8b\uff1a<\/p>\n\n\n\n<p>UserMapper.java<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public interface UserMapper {\n  UserInfo getUserInfoById (Integer id);\n}<\/pre>\n\n\n\n<p>DepartmentMapper.java<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public interface DepartmentMapper {\n  Department getDepartmentByUserId (Integer uid);\n}<\/pre>\n\n\n\n<p>UserMapper.xml<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;select id=\"getUserInfo\" resultMap=\"userInfoMap\">\n  SELECT username, age, workno, deptid\n  FROM user WHERE uid = #{id}\n&lt;\/select>\n\n&lt;resultMap id=\"userInfoMap\" type=\"com.cat.pojo.UserInfo\">\n  &lt;result column=\"username\" property=\"uname\"\/>\n  &lt;result column=\"age\" property=\"age\"\/>\n  &lt;result column=\"workno\" property=\"workNo\"\/>\n  \n  &lt;!-- \u5c06\u67e5\u8be2\u51fa\u6765\u7684User\u5bf9\u8c61\u7684deptid\u4f20\u5165\uff0c\u5c01\u88c5\u6210\u5177\u4f53\u7684Department\u4fe1\u606f\uff0c\u5f97\u5230\u7528\u6237\u7684\u4fe1\u606f\u53caDepartment\u4fe1\u606f -->\n  &lt;association property=\"dept\" column=\"deptid\" select=\"com.cat.mapper.DepartmentMapper.getDepartmentByUserId\" \/>\n&lt;\/resultMap><\/pre>\n\n\n\n<p>DepartmentMapper.xml<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;select id=\"getDepartmentByUserId\" parameterType=\"string\" resultType=\"com.cat.pojo.Department\">\n  SELECT department_name, leader FROM department WHERE id = #{deptId}\n&lt;\/select><\/pre>\n\n\n\n<p>SQL\u6267\u884c\u8fc7\u7a0b\uff1a\u6b64\u65f6\u6570\u636e\u7684\u67e5\u8be2\u5c06\u4f1a\u5206\u4e3a\u4e24\u6b65\uff0c\u7b2c\u4e00\u6b65\u5c06\u7528\u6237\u68c0\u7d22\u51fa\u6765\uff0c\u7b2c\u4e8c\u6b65\u518d\u6839\u636e\u6bcf\u4e2a\u7528\u6237\u7684deptid\u67e5\u8be2\u5230\u90e8\u95e8\u4fe1\u606f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001resultMap\u7684\u4f7f\u7528 resultMap \u4e5f\u662f\u5b9a\u4e49\u8fd4\u56de\u503c\u7c7b\u578b\uff0c\u8fd4\u56de\u503c\u4e3a\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u7c7b\u578b\uff0c\u53ef\u7528\u4e8e\u89e3\u51b3Ja [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/8555"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8555"}],"version-history":[{"count":3,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/8555\/revisions"}],"predecessor-version":[{"id":8563,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/8555\/revisions\/8563"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}