{"id":6988,"date":"2024-11-14T12:03:22","date_gmt":"2024-11-14T04:03:22","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6988\/"},"modified":"2024-11-14T12:03:22","modified_gmt":"2024-11-14T04:03:22","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:\/\/fwq.ai\/blog\/6988\/","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><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173025382357477.jpg\" class=\"aligncenter\" title=\"QueryRunner\u67e5\u8be2\u7ed3\u679c\u4e2d\u5185\u90e8\u7c7b\u4e3a\u7a7a\uff1a\u5982\u4f55\u5229\u7528MyBatis\u5173\u8054\u89e3\u51b3\uff1f\u63d2\u56fe\" alt=\"QueryRunner\u67e5\u8be2\u7ed3\u679c\u4e2d\u5185\u90e8\u7c7b\u4e3a\u7a7a\uff1a\u5982\u4f55\u5229\u7528MyBatis\u5173\u8054\u89e3\u51b3\uff1f\u63d2\u56fe\" \/><\/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<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\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<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\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>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\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>\u4ee5\u4e0a\u5c31\u662fQueryRunner\u67e5\u8be2\u7ed3\u679c\u4e2d\u5185\u90e8\u7c7b\u4e3a\u7a7a\uff1a\u5982\u4f55\u5229\u7528MyBatis\u5173\u8054\u89e3\u51b3\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u89e3\u51b3queryrunner\u67e5\u8be2\u7ed3\u679c\u4e2d\u7684\u5185\u90e8\u7c7b\u4e3a\u7a7a\u7684\u95ee\u9898 \u95ee\u9898\uff1a \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 \u89e3\u51b3\u65b9\u6848\uff1a mybatis association\u7528\u6cd5 \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 \u521b\u5efamapper\u6620\u5c04\u6587\u4ef6\uff1a &lt;mapper namespace=&#8221;com.example.mapper.customermapper&#8221;&gt; &lt;resultmap id=&#8221;customerresultmap&#8221; type=&#8221;com.example.model.customer&#8221;&gt; &lt;result property=&#8221;id&#8221; column=&#8221;id&#8221; \/&gt; &lt;result property=&#8221;name&#8221; column=&#8221;name&#8221; \/&gt; &lt;association property=&#8221;region&#8221; column=&#8221;region_id&#8221; javatype=&#8221;com.example.model.region&#8221; \/&gt; &lt;\/resultmap&gt; &lt;select id=&#8221;selectallcustomers&#8221; resultmap=&#8221;customerresultmap&#8221;&gt; select * from customer &lt;\/select&gt; &lt;\/mapper&gt; \u767b\u5f55\u540e\u590d\u5236 \u5b9a\u4e49\u5b9e\u4f53\u7c7b\uff1a public class customer { private int id; private string name; private region region; \/\/ getter and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-6988","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6988","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=6988"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6988\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6988"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}