{"id":6517,"date":"2024-08-29T20:32:33","date_gmt":"2024-08-29T12:32:33","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6517"},"modified":"2024-08-29T20:32:33","modified_gmt":"2024-08-29T12:32:33","slug":"mybatis-%e4%b8%bb%e8%a6%81%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6517","title":{"rendered":"MyBatis \u4e3b\u8981\u529f\u80fd"},"content":{"rendered":"<blockquote>\n<p>\u975e\u539f\u521b\u3001 ChatGPT \u95ee\u7b54\u751f\u6210<\/p>\n<\/blockquote>\n<p>MyBatis \u662f\u4e00\u4e2a\u7528\u4e8e Java \u5e94\u7528\u7a0b\u5e8f\u7684\u6301\u4e45\u5c42\u6846\u67b6\uff0c\u4e3b\u8981\u529f\u80fd\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\uff1a<\/p>\n<h3>1. <strong>\u6570\u636e\u6620\u5c04\uff08Data Mapping\uff09<\/strong><\/h3>\n<ul>\n<li><strong>\u529f\u80fd<\/strong>: \u5c06 Java \u5bf9\u8c61\u548c SQL \u6570\u636e\u5e93\u4e2d\u7684\u8bb0\u5f55\u8fdb\u884c\u6620\u5c04\u3002<\/li>\n<li><strong>\u7279\u70b9<\/strong>: \u652f\u6301\u5c06\u67e5\u8be2\u7ed3\u679c\u6620\u5c04\u4e3a Java \u5bf9\u8c61\uff0c\u4ee5\u53ca\u5c06 Java \u5bf9\u8c61\u7684\u5c5e\u6027\u6620\u5c04\u5230\u6570\u636e\u5e93\u8868\u7684\u5217\u3002<\/li>\n<li><strong>\u5b9e\u73b0\u65b9\u5f0f<\/strong>: \u4f7f\u7528 XML \u6216\u6ce8\u89e3\u6765\u5b9a\u4e49 SQL \u6620\u5c04\u6587\u4ef6\u548c Java \u7c7b\u4e4b\u95f4\u7684\u5173\u7cfb\u3002<\/li>\n<\/ul>\n<h3>2. <strong>SQL \u6620\u5c04\uff08SQL Mapping\uff09<\/strong><\/h3>\n<ul>\n<li><strong>\u529f\u80fd<\/strong>: \u63d0\u4f9b\u7075\u6d3b\u7684 SQL \u8bed\u53e5\u6620\u5c04\u529f\u80fd\u3002<\/li>\n<li><strong>\u7279\u70b9<\/strong>:\n<ul>\n<li>\u652f\u6301\u590d\u6742\u7684 SQL \u67e5\u8be2\uff0c\u5305\u62ec\u8fde\u63a5\u67e5\u8be2\u3001\u5b50\u67e5\u8be2\u7b49\u3002<\/li>\n<li>\u5141\u8bb8\u81ea\u5b9a\u4e49 SQL \u8bed\u53e5\uff0c\u907f\u514d\u4e86 JPA\/Hibernate \u4e2d\u7684\u590d\u6742\u67e5\u8be2\u8bed\u8a00\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u5b9e\u73b0\u65b9\u5f0f<\/strong>: \u5728\u6620\u5c04\u6587\u4ef6\u4e2d\u5b9a\u4e49 SQL \u8bed\u53e5\uff0c\u4f7f\u7528\u53c2\u6570\u5360\u4f4d\u7b26\u6765\u52a8\u6001\u6784\u9020 SQL\u3002<\/li>\n<\/ul>\n<h3>3. <strong>\u52a8\u6001 SQL\uff08Dynamic SQL\uff09<\/strong><\/h3>\n<ul>\n<li><strong>\u529f\u80fd<\/strong>: \u6839\u636e\u4e0d\u540c\u7684\u6761\u4ef6\u52a8\u6001\u751f\u6210 SQL \u8bed\u53e5\u3002<\/li>\n<li><strong>\u7279\u70b9<\/strong>: \u5141\u8bb8\u5728\u8fd0\u884c\u65f6\u6839\u636e\u8f93\u5165\u7684\u6761\u4ef6\u751f\u6210\u4e0d\u540c\u7684 SQL \u8bed\u53e5\u3002<\/li>\n<li><strong>\u5b9e\u73b0\u65b9\u5f0f<\/strong>: \u4f7f\u7528 <code>&lt;if&gt;<\/code>, <code>&lt;choose&gt;<\/code>, <code>&lt;foreach&gt;<\/code> \u7b49\u6807\u7b7e\u5728 XML \u6620\u5c04\u6587\u4ef6\u4e2d\u5b9a\u4e49\u52a8\u6001 SQL \u903b\u8f91\u3002<\/li>\n<\/ul>\n<h3>4. <strong>\u4e8b\u52a1\u7ba1\u7406\uff08Transaction Management\uff09<\/strong><\/h3>\n<ul>\n<li><strong>\u529f\u80fd<\/strong>: \u652f\u6301\u4e8b\u52a1\u7684\u7ba1\u7406\u548c\u63a7\u5236\u3002<\/li>\n<li><strong>\u7279\u70b9<\/strong>: \u53ef\u4ee5\u4e0e Spring \u7b49\u6846\u67b6\u96c6\u6210\uff0c\u5b9e\u73b0\u4e8b\u52a1\u7684\u81ea\u52a8\u63d0\u4ea4\u3001\u56de\u6eda\u7b49\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u5b9e\u73b0\u65b9\u5f0f<\/strong>: \u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u6216\u6ce8\u89e3\u6765\u7ba1\u7406\u4e8b\u52a1\uff0c\u4e5f\u652f\u6301\u624b\u52a8\u63a7\u5236\u4e8b\u52a1\u7684\u63d0\u4ea4\u548c\u56de\u6eda\u3002<\/li>\n<\/ul>\n<h3>5. <strong>\u7f13\u5b58\u652f\u6301\uff08Caching\uff09<\/strong><\/h3>\n<ul>\n<li><strong>\u529f\u80fd<\/strong>: \u63d0\u4f9b\u4e00\u7ea7\u7f13\u5b58\u548c\u4e8c\u7ea7\u7f13\u5b58\u673a\u5236\u3002<\/li>\n<li><strong>\u7279\u70b9<\/strong>:\n<ul>\n<li><strong>\u4e00\u7ea7\u7f13\u5b58<\/strong>: \u9ed8\u8ba4\u5f00\u542f\uff0c\u751f\u547d\u5468\u671f\u662f\u6bcf\u4e2a SqlSession\uff0c\u4f1a\u7f13\u5b58\u67e5\u8be2\u7ed3\u679c\uff0c\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<li><strong>\u4e8c\u7ea7\u7f13\u5b58<\/strong>: \u53ef\u4ee5\u5728\u4e0d\u540c\u7684 SqlSession \u4e4b\u95f4\u5171\u4eab\u7f13\u5b58\uff0c\u9700\u8981\u663e\u5f0f\u914d\u7f6e\u548c\u7ba1\u7406\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u5b9e\u73b0\u65b9\u5f0f<\/strong>: \u901a\u8fc7 XML \u914d\u7f6e\u7f13\u5b58\u8bbe\u7f6e\uff0c\u652f\u6301\u81ea\u5b9a\u4e49\u7f13\u5b58\u5b9e\u73b0\u3002<\/li>\n<\/ul>\n<h3>6. <strong>\u6620\u5c04\u6587\u4ef6\uff08Mapping Files\uff09<\/strong><\/h3>\n<ul>\n<li><strong>\u529f\u80fd<\/strong>: \u5b9a\u4e49 SQL \u8bed\u53e5\u548c Java \u65b9\u6cd5\u7684\u6620\u5c04\u5173\u7cfb\u3002<\/li>\n<li><strong>\u7279\u70b9<\/strong>:\n<ul>\n<li>\u4f7f\u7528 XML \u6587\u4ef6\u6216\u6ce8\u89e3\u914d\u7f6e SQL \u8bed\u53e5\u3002<\/li>\n<li>\u901a\u8fc7\u6620\u5c04\u6587\u4ef6\u914d\u7f6e\u53c2\u6570\u3001\u7ed3\u679c\u7c7b\u578b\u3001SQL \u8bed\u53e5\u7b49\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u5b9e\u73b0\u65b9\u5f0f<\/strong>:\n<ul>\n<li><strong>XML \u6620\u5c04\u6587\u4ef6<\/strong>: \u5b9a\u4e49 SQL \u8bed\u53e5\u3001\u53c2\u6570\u548c\u7ed3\u679c\u6620\u5c04\u3002<\/li>\n<li><strong>\u6ce8\u89e3<\/strong>: \u5728 Java \u7c7b\u7684\u65b9\u6cd5\u4e0a\u4f7f\u7528\u6ce8\u89e3\u6765\u5b9a\u4e49 SQL \u8bed\u53e5\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>7. <strong>\u63d2\u4ef6\u673a\u5236\uff08Plugin Mechanism\uff09<\/strong><\/h3>\n<ul>\n<li><strong>\u529f\u80fd<\/strong>: \u63d0\u4f9b\u5bf9 MyBatis \u6267\u884c\u8fc7\u7a0b\u7684\u81ea\u5b9a\u4e49\u6269\u5c55\u3002<\/li>\n<li><strong>\u7279\u70b9<\/strong>: \u53ef\u4ee5\u521b\u5efa\u81ea\u5b9a\u4e49\u63d2\u4ef6\uff0c\u6269\u5c55 MyBatis \u7684\u529f\u80fd\uff0c\u5982\u81ea\u5b9a\u4e49\u62e6\u622a\u5668\u3002<\/li>\n<li><strong>\u5b9e\u73b0\u65b9\u5f0f<\/strong>: \u5b9e\u73b0 <code>Interceptor<\/code> \u63a5\u53e3\u5e76\u6ce8\u518c\u5230 MyBatis \u914d\u7f6e\u4e2d\u3002<\/li>\n<\/ul>\n<h3>8. <strong>\u81ea\u52a8\u751f\u6210\u4ee3\u7801\uff08Code Generation\uff09<\/strong><\/h3>\n<ul>\n<li><strong>\u529f\u80fd<\/strong>: \u652f\u6301\u901a\u8fc7\u5de5\u5177\u81ea\u52a8\u751f\u6210\u57fa\u7840\u7684 CRUD \u4ee3\u7801\u3002<\/li>\n<li><strong>\u7279\u70b9<\/strong>: \u51cf\u5c11\u624b\u52a8\u7f16\u5199 SQL \u6620\u5c04\u548c Java \u7c7b\u7684\u5de5\u4f5c\u91cf\u3002<\/li>\n<li><strong>\u5b9e\u73b0\u65b9\u5f0f<\/strong>: \u4f7f\u7528 MyBatis Generator \u6216\u5176\u4ed6\u5de5\u5177\uff0c\u6839\u636e\u6570\u636e\u5e93\u8868\u7ed3\u6784\u751f\u6210\u5bf9\u5e94\u7684 Mapper \u63a5\u53e3\u548c XML \u6620\u5c04\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<h3>9. <strong>\u590d\u6742\u6620\u5c04\uff08Complex Mapping\uff09<\/strong><\/h3>\n<ul>\n<li><strong>\u529f\u80fd<\/strong>: \u652f\u6301\u590d\u6742\u7684\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\u3002<\/li>\n<li><strong>\u7279\u70b9<\/strong>: \u5141\u8bb8\u6620\u5c04\u4e00\u5bf9\u4e00\u3001\u4e00\u5bf9\u591a\u3001\u591a\u5bf9\u591a\u7684\u5173\u7cfb\u3002<\/li>\n<li><strong>\u5b9e\u73b0\u65b9\u5f0f<\/strong>: \u4f7f\u7528 <code>&lt;resultMap&gt;<\/code> \u6807\u7b7e\u5728 XML \u6587\u4ef6\u4e2d\u914d\u7f6e\u590d\u6742\u7684\u7ed3\u679c\u6620\u5c04\u5173\u7cfb\u3002<\/li>\n<\/ul>\n<h3>\u603b\u7ed3<\/h3>\n<p>MyBatis \u63d0\u4f9b\u4e86\u7075\u6d3b\u7684 SQL \u6620\u5c04\u548c\u6570\u636e\u6301\u4e45\u5316\u529f\u80fd\uff0c\u901a\u8fc7\u52a8\u6001 SQL\u3001\u4e8b\u52a1\u7ba1\u7406\u3001\u7f13\u5b58\u652f\u6301\u7b49\u7279\u6027\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u9ad8\u6548\u5730\u5904\u7406\u6570\u636e\u5e93\u64cd\u4f5c\u3002\u5b83\u53ef\u4ee5\u4e0e\u5176\u4ed6\u6846\u67b6\u5982 Spring \u8fdb\u884c\u96c6\u6210\uff0c\u4ee5\u63d0\u4f9b\u66f4\u5b8c\u6574\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u975e\u539f\u521b\u3001 ChatGPT \u95ee\u7b54\u751f\u6210 MyBatis \u662f\u4e00\u4e2a\u7528\u4e8e Java \u5e94\u7528\u7a0b\u5e8f\u7684\u6301\u4e45\u5c42\u6846\u67b6\uff0c\u4e3b\u8981\u529f\u80fd\u5305\u62ec\u4ee5&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6517\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[123],"tags":[153],"class_list":["post-6517","post","type-post","status-publish","format-standard","hentry","category-123","tag-mybatis"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6517","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6517"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6517\/revisions"}],"predecessor-version":[{"id":6518,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6517\/revisions\/6518"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}