{"id":200212,"date":"2025-05-06T09:05:55","date_gmt":"2025-05-06T01:05:55","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200212\/"},"modified":"2025-05-06T09:05:55","modified_gmt":"2025-05-06T01:05:55","slug":"%e4%bd%bf%e7%94%a8-queryrunner-%e6%9f%a5%e8%af%a2-customer-%e6%97%b6%ef%bc%8c%e5%a6%82%e4%bd%95%e9%81%bf%e5%85%8d%e5%86%85%e9%83%a8%e7%b1%bb-region-%e4%b8%ba-null%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200212\/","title":{"rendered":"\u4f7f\u7528 QueryRunner \u67e5\u8be2 Customer \u65f6\uff0c\u5982\u4f55\u907f\u514d\u5185\u90e8\u7c7b Region \u4e3a Null\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u4f7f\u7528 QueryRunner \u67e5\u8be2 Customer \u65f6\uff0c\u5982\u4f55\u907f\u514d\u5185\u90e8\u7c7b Region \u4e3a Null\uff1f<\/h1>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6570\u636e\u5e93\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u4f7f\u7528 QueryRunner \u67e5\u8be2 Customer \u65f6\uff0c\u5982\u4f55\u907f\u514d\u5185\u90e8\u7c7b Region \u4e3a Null\uff1f\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241028\/1730115055671f75ef514a1.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u4f7f queryrunner \u8fd4\u56de\u7684\u7c7b\u4e2d\u7684\u5185\u90e8\u7c7b\u4e0d\u4e3a null<\/strong><\/p>\n<p>\u4f60\u5728\u4f7f\u7528 queryrunner \u65f6\u9047\u5230\u4e86\u4e00\u4e2a\u95ee\u9898\uff0c\u5373\u4ece customer \u8868\u67e5\u8be2\u51fa\u6765\u7684 customer \u7c7b\u7684\u5185\u90e8\u7c7b region \u4e3a null\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528 mybatis association \u529f\u80fd\u3002<\/p>\n<p>mybatis association \u5141\u8bb8\u4f60\u901a\u8fc7 xml \u914d\u7f6e\u6587\u4ef6\u5c06\u8868\u4e4b\u95f4\u7684\u5173\u7cfb\u6620\u5c04\u5230\u7c7b\u4e2d\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<p><strong>1. \u5b9a\u4e49 customer \u548c region \u7c7b\uff1a<\/strong><\/p>\n<pre>public class customer {\n    private int id;\n    private string name;\n    private region region;\n}\n\npublic class region {\n    private int id;\n    private string name;\n}<\/pre>\n<p><strong>2. \u521b\u5efa xml \u914d\u7f6e\u6587\u4ef6\uff1a<\/strong><\/p>\n<pre>&lt;mapper namespace=\"customermapper\"&gt;\n    &lt;resultmap id=\"customerresult\" type=\"customer\"&gt;\n        &lt;id property=\"id\" column=\"id\"\/&gt;\n        &lt;result property=\"name\" column=\"name\"\/&gt;\n        &lt;association property=\"region\" column=\"region_id\" javatype=\"region\"\/&gt;\n    &lt;\/resultmap&gt;\n\n    &lt;select id=\"selectallcustomers\" resultmap=\"customerresult\"&gt;\n        select * from customer;\n    &lt;\/select&gt;\n&lt;\/mapper&gt;<\/pre>\n<p>\u5728 xml \u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c&lt;association&gt; \u5143\u7d20\u5b9a\u4e49\u4e86 customer \u7c7b\u4e0e region \u7c7b\u4e4b\u95f4\u7684\u5173\u8054\u5173\u7cfb\u3002\u5b83\u6307\u5b9a\u4e86 customer \u8868\u4e2d region_id \u5217\u4e0e region \u7c7b\u7684 id \u5c5e\u6027\u4e4b\u95f4\u7684\u6620\u5c04\u3002<\/p>\n<p><strong>3. \u4f7f\u7528 mybatis \u67e5\u8be2\u6570\u636e\uff1a<\/strong><\/p>\n<pre>\/\/ \u521b\u5efa\u4e00\u4e2a MyBatis SessionFactory\nSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(dataSource);\n\n\/\/ \u6253\u5f00\u4e00\u4e2a Session\nSession session = sessionFactory.openSession();\n\n\/\/ \u4f7f\u7528 MyBatis \u67e5\u8be2\u6570\u636e\nList&lt;Customer&gt; customers = session.selectList(\"selectAllCustomers\");\n\n\/\/ \u5173\u95ed Session\nsession.close();<\/pre>\n<p>\u4f7f\u7528 mybatis \u67e5\u8be2\u540e\uff0ccustomer \u5bf9\u8c61\u4e2d\u7684 region \u5c5e\u6027\u5c06\u4e0d\u518d\u4e3a null\uff0c\u800c\u662f\u4f1a\u5305\u542b\u5173\u8054\u7684 region \u5bf9\u8c61\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u4f7f\u7528 QueryRunner \u67e5\u8be2 Customer \u65f6\uff0c\u5982\u4f55\u907f\u514d\u5185\u90e8\u7c7b Region \u4e3a Null\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 QueryRunner \u67e5&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200212","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200212","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=200212"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200212\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}