{"id":1464,"date":"2022-03-30T20:35:27","date_gmt":"2022-03-30T12:35:27","guid":{"rendered":"http:\/\/www.zyhcoding.club\/?p=1464"},"modified":"2022-03-30T21:56:52","modified_gmt":"2022-03-30T13:56:52","slug":"%e6%98%a0%e5%b0%84%e5%99%a8","status":"publish","type":"post","link":"http:\/\/www.zyhcoding.club\/index.php\/2022\/03\/30\/%e6%98%a0%e5%b0%84%e5%99%a8\/","title":{"rendered":"\u6620\u5c04\u5668"},"content":{"rendered":"<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<code>namespace<\/code>\u6765\u7ed1\u5b9a\u5230\u4e00\u4e2a\u63a5\u53e3\u4e0a\uff0c\u5229\u7528\u63a5\u53e3\u7684\u7279\u6027\uff0c\u53ef\u4ee5\u76f4\u63a5\u6307\u660e\u65b9\u6cd5\u7684\u884c\u4e3a\uff0c\u800c\u5b9e\u9645\u5b9e\u73b0\u5219\u662f\u7531Mybatis\u6765\u5b8c\u6210\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5c06\u6620\u5c04\u5668\u7684\u547d\u540d\u7a7a\u95f4\u4fee\u6539\u4e3a\u6211\u4eec\u7684\u63a5\u53e3\uff0c\u4e00\u822c\u5c06\u63a5\u53e3\u548c\u6620\u5c04\u5668\u6587\u4ef6\u653e\u5728\u4e00\u8d77:<\/p>\n<pre><code class=\"language-xml\">&lt;mapper namespace=&quot;com.test.mapper.TestMapper&quot;&gt;\n    &lt;select id=&quot;selectStudent&quot; resultType=&quot;Student&quot;&gt;\n        SELECT * FROM STUDENT\n    &lt;\/select&gt;\n&lt;\/mapper&gt;<\/code><\/pre>\n<p><code>resultType<\/code>\u8868\u793aSQL\u8bed\u53e5\u7684\u7ed3\u679c\u8fd4\u56de\u5230\u4ec0\u4e48Java\u7c7b\u578b\u91cc\uff0c\u8fd9\u91cc\u53ef\u4ee5\u4f7f\u7528\u522b\u540d(\u5982\u679c\u7c7b\u5b9a\u4e49\u4e86\u522b\u540d\u7684\u8bdd)\u3002\u4f7f\u7528<code>resultType<\/code>\u65f6\uff0cMyBatis \u4f1a\u5728\u5e55\u540e\u81ea\u52a8\u521b\u5efa\u4e00\u4e2a ResultMap\uff0c\u518d\u6839\u636e\u5c5e\u6027\u540d\u6765\u6620\u5c04\u5217\u5230 JavaBean \u7684\u5c5e\u6027\u4e0a\u3002\u5982\u679c\u5217\u540d\u548c\u5c5e\u6027\u540d\u4e0d\u80fd\u5339\u914d\u4e0a\uff0c\u53ef\u4ee5\u5728 SELECT \u8bed\u53e5\u4e2d\u8bbe\u7f6e\u5217\u522b\u540d\uff08\u8fd9\u662f\u4e00\u4e2a\u57fa\u672c\u7684 SQL \u7279\u6027\uff09\u6765\u5b8c\u6210\u5339\u914d\u3002<\/p>\n<pre><code class=\"language-xml\">&lt;select id=&quot;selectUsers&quot; resultType=&quot;User&quot;&gt;\n  select\n    user_id             as &quot;id&quot;,\n    user_name           as &quot;userName&quot;,\n    hashed_password     as &quot;hashedPassword&quot;\n  from some_table\n  where id = #{id}\n&lt;\/select&gt;<\/code><\/pre>\n<p>\u8fd9\u6837\u5b9a\u4e49\u6620\u5c04\u5668\u540e\uff0c\u6211\u4eec\u5c31\u9700\u8981\u4fee\u6539\u4e00\u4e0b\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684mapper\u5b9a\u4e49\uff0c\u4f7f\u7528<code>resource<\/code>\u800c\u4e0d\u662f<code>url<\/code>\u3002<\/p>\n<pre><code class=\"language-xml\">&lt;mappers&gt;\n    &lt;mapper resource=&quot;com\/test\/mapper\/TestMapper.xml&quot;\/&gt;\n&lt;\/mappers&gt;<\/code><\/pre>\n<p>\u63a5\u53e3\u540d\u79f0\u4f5c\u4e3a\u547d\u540d\u7a7a\u95f4\u540e\uff0c\u63a5\u53e3\u5185\u7684\u65b9\u6cd5\u58f0\u660e\u5c31\u4f5c\u4e3a\u6bcf\u4e2aSQL\u8bed\u53e5id\uff0c\u8fd9\u6837\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u63a5\u53e3\u7684\u67d0\u4e2a\u65b9\u6cd5\u6765\u5b9e\u73b0SQL\u8bed\u53e5\u3002<\/p>\n<pre><code class=\"language-java\">public interface TestMapper {\n    List&lt;Student&gt; selectStudent();\n}\n\n\/\/ \u901a\u8fc7\u63a5\u53e3\npublic static void main(String[] args) {\n    try (SqlSession sqlSession = MybatisUtil.getSession(true)){\n        TestMapper testMapper = sqlSession.getMapper(TestMapper.class);\n        List&lt;Student&gt; student = testMapper.selectStudent();\n        student.forEach(System.out::println);\n    }\n}\n<\/code><\/pre>\n<p>\u8fd9\u662f\u4f7f\u7528<code>resource<\/code>\u5b9e\u73b0\u6620\u5c04\u5668\u7684\u65b9\u6cd5\u3002<\/p>\n<h5>\u7ed3\u679c\u6620\u5c04\uff08ResultMap\uff09<\/h5>\n<p>resultMap \u5143\u7d20\u662f MyBatis \u4e2d\u6700\u91cd\u8981\u6700\u5f3a\u5927\u7684\u5143\u7d20\u3002ResultMap \u7684\u8bbe\u8ba1\u601d\u60f3\u662f\uff0c\u5bf9\u7b80\u5355\u7684\u8bed\u53e5\u505a\u5230\u96f6\u914d\u7f6e\uff0c\u5bf9\u4e8e\u590d\u6742\u4e00\u70b9\u7684\u8bed\u53e5\uff0c\u53ea\u9700\u8981\u63cf\u8ff0\u8bed\u53e5\u4e4b\u95f4\u7684\u5173\u7cfb\u5c31\u884c\u4e86\u3002resultMap\u7684\u529f\u80fd\u6bd4resultType\u66f4\u5f3a\u5927\uff0c\u5b83\u662f\u4e00\u4e2a\u663e\u793a\u914d\u7f6e\u3002<\/p>\n<pre><code class=\"language-xml\">&lt;resultMap id=&quot;Test&quot; type=&quot;Student&quot;&gt;\n    &lt;result column=&quot;sid&quot; property=&quot;sid&quot;\/&gt;\n    &lt;result column=&quot;sex&quot; property=&quot;name&quot;\/&gt;\n    &lt;result column=&quot;name&quot; property=&quot;sex&quot;\/&gt;\n&lt;\/resultMap&gt;<\/code><\/pre>\n<p>resultMap\u6709\u4e00\u4e2aid\uff0c\u8868\u793a\u540d\u79f0\uff0c\u53ef\u4ee5\u5728SQL\u8bed\u53e5\u4e2d\u6307\u5b9a\u53ea\u7528\u54ea\u4e2aresultMap\u3002type\u4e3a\u6307\u5b9a\u7684Java\u7c7b\u578b\uff0c\u8fd9\u8868\u793a\u5c06SQL\u67e5\u8be2\u7684\u7ed3\u679c\u8fd4\u56de\u5230\u4ec0\u4e48\u7c7b\u578b\u4e2d\u53bb\uff0c\u800c\u5177\u4f53\u7684\u8fd4\u56de\u7ec6\u8282\u8fd8\u53ef\u4ee5\u518d\u8be6\u7ec6\u8bbe\u5b9a\u3002<br \/>\n<code>result<\/code>\u5c5e\u6027\u4e2dcolumn\u8868\u793aSQL\u7ed3\u679c\u8868\u4e2d\u7684\u67d0\u4e00\u5217\uff0cproperty\u8868\u793a\u5c06\u8be5\u5217\u503c\u8d4b\u7ed9Java\u7c7b\u7684\u67d0\u4e2a\u5b57\u6bb5\u3002<\/p>\n<p><strong>\u7ed3\u679c\u6620\u5c04<\/strong><\/p>\n<ul>\n<li>id - \u4e00\u4e2a ID \u7ed3\u679c\uff1b\u6807\u8bb0\u51fa\u4f5c\u4e3a ID \u7684\u7ed3\u679c\u53ef\u4ee5\u5e2e\u52a9\u63d0\u9ad8\u6574\u4f53\u6027\u80fd<\/li>\n<li>result - \u6ce8\u5165\u5230\u5b57\u6bb5\u6216 JavaBean \u5c5e\u6027\u7684\u666e\u901a\u7ed3\u679c<\/li>\n<li>constructor - \u7528\u4e8e\u5728\u5b9e\u4f8b\u5316\u7c7b\u65f6\uff0c\u6ce8\u5165\u7ed3\u679c\u5230\u6784\u9020\u65b9\u6cd5\u4e2d\n<ul>\n<li>idArg - ID \u53c2\u6570\uff1b\u6807\u8bb0\u51fa\u4f5c\u4e3a ID \u7684\u7ed3\u679c\u53ef\u4ee5\u5e2e\u52a9\u63d0\u9ad8\u6574\u4f53\u6027\u80fd<\/li>\n<li>arg - \u5c06\u88ab\u6ce8\u5165\u5230\u6784\u9020\u65b9\u6cd5\u7684\u4e00\u4e2a\u666e\u901a\u7ed3\u679c<\/li>\n<\/ul>\n<\/li>\n<li>collections - \u4e00\u4e2a\u590d\u6742\u7c7b\u578b\u7684\u96c6\u5408<\/li>\n<li>association - \u4e00\u4e2a\u590d\u6742\u7c7b\u578b\u7684\u5173\u8054\uff1b\u8bb8\u591a\u7ed3\u679c\u5c06\u5305\u88c5\u6210\u8fd9\u79cd\u7c7b\u578b<\/li>\n<\/ul>\n<p><code>id<\/code>\u6807\u7b7e\u7528\u4e8e\u5728\u591a\u6761\u8bb0\u5f55\u4e2d\u8fa8\u522b\u662f\u5426\u4e3a\u540c\u4e00\u4e2a\u5bf9\u8c61\u7684\u6570\u636e\uff0c\u5373\u8868\u4e2d\u6709\u4e00\u5217\u6570\u636e\u5b58\u5728\u591a\u6761\u8bb0\u5f55\u76f8\u7b49\u7684\u60c5\u51b5\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5id\u5c06\u5b83\u4eec\u6620\u5c04\u4e3a\u4e00\u4e2aJava\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u591a\u4e2a\u3002<\/p>\n<p>\u5f53\u6ca1\u6709\u6307\u5b9aconstructor\u6784\u9020\u65b9\u6cd5\u65f6\uff0cMyBatis\u4f1a\u6839\u636eresult\u548cid\u7684\u8bbe\u5b9a\u6765\u8fdb\u884c\u6620\u5c04\u3002\u5982\u679c\u9009\u62e9\u4e86constructor\uff0c\u90a3\u4e48MyBatis\u4f1a\u6839\u636e\u6240\u9009\u62e9\u6784\u9020\u65b9\u6cd5\u6765\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<pre><code class=\"language-xml\">&lt;resultMap id=&quot;test&quot; type=&quot;Student&quot;&gt;\n    &lt;constructor&gt;\n        &lt;arg column=&quot;sid&quot; javaType=&quot;Integer&quot;\/&gt;\n        &lt;arg column=&quot;name&quot; javaType=&quot;String&quot;\/&gt;\n    &lt;\/constructor&gt;\n&lt;\/resultMap&gt;<\/code><\/pre>\n<p>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6307\u5b9a\u6784\u9020\u65b9\u6cd5\u540e\uff0c\u82e5\u6b64\u5b57\u6bb5\u88ab\u586b\u5165\u4e86\u6784\u9020\u65b9\u6cd5\u4f5c\u4e3a\u53c2\u6570\uff0c\u5c06\u4e0d\u4f1a\u901a\u8fc7\u53cd\u5c04\u7ed9\u5b57\u6bb5\u5355\u72ec\u8d4b\u503c\uff0c\u800c\u6784\u9020\u65b9\u6cd5\u4e2d\u6ca1\u6709\u4f20\u5165\u7684\u5b57\u6bb5\uff0c\u4f9d\u7136\u4f1a\u88ab\u53cd\u5c04\u8d4b\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7namespace\u6765\u7ed1\u5b9a\u5230\u4e00\u4e2a\u63a5\u53e3\u4e0a\uff0c\u5229\u7528\u63a5\u53e3\u7684\u7279\u6027\uff0c\u53ef\u4ee5\u76f4\u63a5\u6307\u660e\u65b9\u6cd5\u7684\u884c\u4e3a\uff0c\u800c\u5b9e\u9645\u5b9e\u73b0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[79],"tags":[],"_links":{"self":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/1464"}],"collection":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/comments?post=1464"}],"version-history":[{"count":4,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/1464\/revisions"}],"predecessor-version":[{"id":1471,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/1464\/revisions\/1471"}],"wp:attachment":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/media?parent=1464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/categories?post=1464"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/tags?post=1464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}