{"id":200912,"date":"2025-05-06T10:23:24","date_gmt":"2025-05-06T02:23:24","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200912\/"},"modified":"2025-05-06T10:23:24","modified_gmt":"2025-05-06T02:23:24","slug":"queryrunner%e6%9f%a5%e8%af%a2%e7%bb%93%e6%9e%9c%e4%b8%ad%e5%86%85%e9%83%a8%e7%b1%bb%e4%b8%ba%e7%a9%ba%ef%bc%9a%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8mybatis%e5%85%b3%e8%81%94%e8%a7%a3%e5%86%b3%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200912\/","title":{"rendered":"QueryRunner\u67e5\u8be2\u7ed3\u679c\u4e2d\u5185\u90e8\u7c7b\u4e3a\u7a7a\uff1a\u5982\u4f55\u5229\u7528MyBatis\u5173\u8054\u89e3\u51b3\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>QueryRunner\u67e5\u8be2\u7ed3\u679c\u4e2d\u5185\u90e8\u7c7b\u4e3a\u7a7a\uff1a\u5982\u4f55\u5229\u7528MyBatis\u5173\u8054\u89e3\u51b3\uff1f<\/h1>\n<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762\u4e3b\u673a\u5b9d\u8d1d\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6570\u636e\u5e93\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300aQueryRunner\u67e5\u8be2\u7ed3\u679c\u4e2d\u5185\u90e8\u7c7b\u4e3a\u7a7a\uff1a\u5982\u4f55\u5229\u7528MyBatis\u5173\u8054\u89e3\u51b3\uff1f\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241120\/1732077196673d668c7d20c.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u89e3\u51b3queryrunner\u67e5\u8be2\u7ed3\u679c\u4e2d\u7684\u5185\u90e8\u7c7b\u4e3a\u7a7a\u7684\u95ee\u9898<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5982\u4f55\u907f\u514dqueryrunner\u67e5\u8be2\u8fd4\u56de\u7684\u7c7b\u4e2d\u7684\u5185\u90e8\u7c7b\u4e3a\u7a7a\uff0c\u4f8b\u5982\u5728customer\u7c7b\u4e2d\u5d4c\u5165region\u7c7b\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p><strong>mybatis association\u7528\u6cd5<\/strong><\/p>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528mybatis\u7684association\u7528\u6cd5\u6765\u5173\u8054customer\u8868\u548cregion\u8868\u3002mybatis\u901a\u8fc7xml\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u6620\u5c04\u4fe1\u606f\u6765\u5b8c\u6210\u8868\u548c\u7c7b\u7684\u5173\u8054\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5f53\u67e5\u8be2customer\u8868\u65f6\uff0c\u81ea\u52a8\u52a0\u8f7d\u5173\u8054\u7684region\u8868\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528mybatis association\u7528\u6cd5\uff1a<\/p>\n<ol>\n<li><strong>\u521b\u5efamapper\u6620\u5c04\u6587\u4ef6\uff1a<\/strong><\/li>\n<\/ol>\n<pre>&lt;mapper namespace=\"com.example.mapper.customermapper\"&gt;\n  &lt;resultmap id=\"customerresultmap\" type=\"com.example.model.customer\"&gt;\n    &lt;result property=\"id\" column=\"id\" \/&gt;\n    &lt;result property=\"name\" column=\"name\" \/&gt;\n    &lt;association property=\"region\" column=\"region_id\" javatype=\"com.example.model.region\" \/&gt;\n  &lt;\/resultmap&gt;\n\n  &lt;select id=\"selectallcustomers\" resultmap=\"customerresultmap\"&gt;\n    select * from customer\n  &lt;\/select&gt;\n&lt;\/mapper&gt;<\/pre>\n<ol>\n<li><strong>\u5b9a\u4e49\u5b9e\u4f53\u7c7b\uff1a<\/strong><\/li>\n<\/ol>\n<pre>public class customer {\n  private int id;\n  private string name;\n  private region region;\n  \/\/ getter and setter methods\n}\n\npublic class region {\n  private int id;\n  private string name;\n  \/\/ getter and setter methods\n}<\/pre>\n<ol>\n<li><strong>\u4f7f\u7528mybatis\u8bbf\u95ee\u6570\u636e\uff1a<\/strong><\/li>\n<\/ol>\n<pre>SqlSession session = sqlSessionFactory.openSession();\ntry {\n  CustomerMapper customerMapper = session.getMapper(CustomerMapper.class);\n  List&lt;Customer&gt; customers = customerMapper.selectAllCustomers();\n  for (Customer customer : customers) {\n    System.out.println(customer.getName() + \" is located in \" + customer.getRegion().getName());\n  }\n} finally {\n  session.close();\n}<\/pre>\n<p>\u901a\u8fc7\u4f7f\u7528mybatis\u7684association\u7528\u6cd5\uff0c\u53ef\u4ee5\u5728\u67e5\u8be2customer\u8868\u65f6\u81ea\u52a8\u52a0\u8f7d\u5173\u8054\u7684region\u6570\u636e\uff0c\u4ece\u800c\u907f\u514d\u5185\u90e8\u7c7b\u4e3a\u7a7a\u7684\u95ee\u9898\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300aQueryRunner\u67e5\u8be2\u7ed3\u679c\u4e2d\u5185\u90e8\u7c7b\u4e3a\u7a7a\uff1a\u5982\u4f55\u5229\u7528MyBatis\u5173\u8054\u89e3\u51b3\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>QueryRunner\u67e5\u8be2\u7ed3\u679c\u4e2d&#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-200912","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200912","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=200912"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200912\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}