{"id":1603,"date":"2024-11-30T00:30:15","date_gmt":"2024-11-29T16:30:15","guid":{"rendered":"https:\/\/www.fanyamin.com\/wordpress\/?p=1603"},"modified":"2024-11-30T00:32:41","modified_gmt":"2024-11-29T16:32:41","slug":"mybatis-spring-boot-starter-%e6%98%af%e6%80%8e%e4%b9%88%e5%81%9a%e7%9a%84","status":"publish","type":"post","link":"https:\/\/www.fanyamin.com\/wordpress\/?p=1603","title":{"rendered":"mybatis spring boot starter \u662f\u600e\u4e48\u505a\u7684"},"content":{"rendered":"<h2>Overview<\/h2>\n<p>\u8981\u4e0e Spring \u4e00\u8d77\u4f7f\u7528 MyBatis\uff0c\u4f60\u81f3\u5c11\u9700\u8981\u4e00\u4e2a SqlSessionFactory \u548c\u4e00\u4e2a mapper \u63a5\u53e3\u3002<\/p>\n<p>MyBatis-Spring-Boot-Starter \u5c06\u4f1a\uff1a<\/p>\n<ul>\n<li>\u81ea\u52a8\u63a2\u6d4b\u5b58\u5728\u7684 DataSource<\/li>\n<li>\u5c06\u4f7f\u7528 SqlSessionFactoryBean \u521b\u5efa\u5e76\u6ce8\u518c\u4e00\u4e2a SqlSessionFactory \u7684\u5b9e\u4f8b\uff0c\u5e76\u5c06\u63a2\u6d4b\u5230\u7684 DataSource \u4f5c\u4e3a\u6570\u636e\u6e90<\/li>\n<li>\u5c06\u521b\u5efa\u5e76\u6ce8\u518c\u4e00\u4e2a\u4ece SqlSessionFactory \u4e2d\u5f97\u5230\u7684 SqlSessionTemplate \u7684\u5b9e\u4f8b<\/li>\n<li>\u81ea\u52a8\u626b\u63cf\u4f60\u7684 mapper\uff0c\u5c06\u5b83\u4eec\u4e0e SqlSessionTemplate \u76f8\u5173\u8054\uff0c\u5e76\u5c06\u5b83\u4eec\u6ce8\u518c\u5230Spring \u7684\u73af\u5883\uff08context\uff09\u4e2d\u53bb\uff0c\u8fd9\u6837\u5b83\u4eec\u5c31\u53ef\u4ee5\u88ab\u6ce8\u5165\u5230\u4f60\u7684 bean \u4e2d<\/li>\n<\/ul>\n<p>MyBatis-Spring-Boot-Starter \u662f\u4e00\u4e2a\u5f00\u7bb1\u5373\u7528\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u7528\u4e8e\u5c06 MyBatis \u4e0e Spring Boot \u96c6\u6210\u3002\u5b83\u7b80\u5316\u4e86 MyBatis \u7684\u914d\u7f6e\u548c\u4f7f\u7528\uff0c\u540c\u65f6\u5145\u5206\u5229\u7528\u4e86 Spring \u7684\u4f9d\u8d56\u6ce8\u5165\u548c\u751f\u547d\u5468\u671f\u7ba1\u7406\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u5b83\u7684\u5b9e\u73b0\u539f\u7406\u7684\u8be6\u7ec6\u89e3\u6790\uff1a<\/p>\n<ol>\n<li>\n<h2>\u81ea\u52a8\u914d\u7f6e\u7684\u5b9e\u73b0<\/h2>\n<p>MyBatis-Spring-Boot-Starter \u5229\u7528\u4e86 Spring Boot \u7684\u81ea\u52a8\u914d\u7f6e\u673a\u5236\uff0c\u901a\u8fc7 @EnableAutoConfiguration \u548c spring.factories \u6587\u4ef6\u5b9e\u73b0\u81ea\u52a8\u5316\u914d\u7f6e\u3002<\/p>\n<ul>\n<li>\n<p>spring.factories<br \/>\nMyBatis-Spring-Boot-Starter \u7684 spring.factories \u6587\u4ef6\u4e2d\u5305\u542b\u5982\u4e0b\u914d\u7f6e\uff1a<\/p>\n<pre><code>org.springframework.boot.autoconfigure.EnableAutoConfiguration=\\\norg.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration<\/code><\/pre>\n<p>\u8fd9\u610f\u5473\u7740 Spring Boot \u542f\u52a8\u65f6\u4f1a\u52a0\u8f7d MybatisAutoConfiguration\uff0c\u81ea\u52a8\u914d\u7f6e MyBatis\u3002<\/p>\n<\/li>\n<li>\n<p>MybatisAutoConfiguration<br \/>\n\u8be5\u7c7b\u7684\u804c\u8d23\u662f\u81ea\u52a8\u521b\u5efa\u548c\u914d\u7f6e\u6838\u5fc3 MyBatis \u7ec4\u4ef6\uff0c\u5305\u62ec\uff1a<\/p>\n<\/li>\n<li>\n<p>SqlSessionFactory\uff1a\u8d1f\u8d23\u751f\u6210\u548c\u7ba1\u7406\u6570\u636e\u5e93\u8fde\u63a5\u4f1a\u8bdd\u3002<\/p>\n<\/li>\n<li>\n<p>SqlSessionTemplate\uff1aSpring \u7ba1\u7406\u7684\u7ebf\u7a0b\u5b89\u5168\u7684 MyBatis \u64cd\u4f5c\u7c7b\u3002<\/p>\n<\/li>\n<li>\n<p>Mapper \u63a5\u53e3\u7684\u52a8\u6001\u4ee3\u7406\uff1a\u5c06 Mapper \u63a5\u53e3\u6ce8\u5165\u5230 Spring \u5bb9\u5668\u4e2d\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<h2>SqlSessionFactory \u7684\u521b\u5efa<\/h2>\n<p>MybatisAutoConfiguration \u4f1a\u81ea\u52a8\u914d\u7f6e SqlSessionFactory\u3002\u5176\u6838\u5fc3\u6b65\u9aa4\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6\u548c\u914d\u7f6e\u4fe1\u606f<\/li>\n<li>\u5982\u679c\u7528\u6237\u63d0\u4f9b\u4e86\u81ea\u5b9a\u4e49\u7684 MyBatis \u914d\u7f6e\u6587\u4ef6\uff08mybatis.config-location\uff09\uff0c\u5219\u52a0\u8f7d\u7528\u6237\u7684\u914d\u7f6e\u6587\u4ef6\u3002<\/li>\n<li>\u5982\u679c\u7528\u6237\u672a\u6307\u5b9a\u914d\u7f6e\u6587\u4ef6\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u914d\u7f6e\u3002<\/li>\n<li>\u52a0\u8f7d\u6570\u636e\u5e93\u6620\u5c04\u6587\u4ef6\uff08Mapper XML\uff09<br \/>\nMybatisProperties \u4e2d\u7684 mapper-locations \u5c5e\u6027\u6307\u5b9a\u4e86 Mapper XML \u6587\u4ef6\u7684\u8def\u5f84\u3002\u8fd9\u4e9b XML \u6587\u4ef6\u4f1a\u88ab ResourcePatternResolver \u52a0\u8f7d\u5e76\u7ed1\u5b9a\u5230 SqlSessionFactory\u3002<\/li>\n<li>\u96c6\u6210\u63d2\u4ef6<br \/>\n\u5982\u679c\u6709\u81ea\u5b9a\u4e49\u7684\u62e6\u622a\u5668\uff08\u5982\u5206\u9875\u63d2\u4ef6\uff09\uff0c\u5b83\u4eec\u4f1a\u901a\u8fc7 MybatisConfiguration \u52a0\u5165\u5230 MyBatis \u914d\u7f6e\u4e2d\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<h2>Mapper \u63a5\u53e3\u7684\u52a8\u6001\u4ee3\u7406<\/h2>\n<p>MyBatis \u63d0\u4f9b\u4e86 MapperProxy \u7528\u4e8e\u52a8\u6001\u4ee3\u7406\u63a5\u53e3\uff0cSpring Boot Starter \u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5c06\u5176\u4e0e Spring \u7ba1\u7406\u7684 Bean \u6ce8\u5165\u673a\u5236\u96c6\u6210\uff1a<\/p>\n<ul>\n<li>\n<p>\u626b\u63cf Mapper \u63a5\u53e3<br \/>\n@MapperScan \u6ce8\u89e3\u4f1a\u626b\u63cf\u6307\u5b9a\u5305\u4e2d\u7684 Mapper \u63a5\u53e3\uff0c\u5e76\u5c06\u8fd9\u4e9b\u63a5\u53e3\u4ea4\u7531 Spring \u7ba1\u7406\u3002<br \/>\n\u4f8b\u5982\uff1a<\/p>\n<pre><code>@MapperScan(&quot;com.example.mapper&quot;)\npublic class Application {}<\/code><\/pre>\n<\/li>\n<li>\n<p>\u6ce8\u518c Mapper \u52a8\u6001\u4ee3\u7406\u5bf9\u8c61<br \/>\nSpring \u5c06 Mapper \u63a5\u53e3\u52a8\u6001\u4ee3\u7406\u4e3a MapperProxy \u5bf9\u8c61\uff0c\u5e76\u901a\u8fc7 SqlSessionTemplate \u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u3002<\/p>\n<\/li>\n<li>\n<p>\u81ea\u52a8\u6ce8\u5165<br \/>\n\u5728\u670d\u52a1\u7c7b\u4e2d\u76f4\u63a5\u901a\u8fc7 @Autowired \u6ce8\u5165 Mapper \u63a5\u53e3\u5373\u53ef\u4f7f\u7528\uff0c\u800c\u65e0\u9700\u624b\u52a8\u521b\u5efa\u5bf9\u8c61\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<h2>\u4e8b\u52a1\u7ba1\u7406<\/h2>\n<\/li>\n<\/ol>\n<p>MyBatis-Spring-Boot-Starter \u5b8c\u5168\u517c\u5bb9 Spring \u7684\u4e8b\u52a1\u7ba1\u7406\u673a\u5236\uff1a<\/p>\n<ul>\n<li>\u58f0\u660e\u5f0f\u4e8b\u52a1<br \/>\n\u4f7f\u7528 @Transactional \u6ce8\u89e3\u7ba1\u7406\u4e8b\u52a1\uff0cSpring \u4f1a\u901a\u8fc7\u4e8b\u52a1\u62e6\u622a\u5668\u5c06\u4e8b\u52a1\u7684\u5f00\u542f\u3001\u63d0\u4ea4\u6216\u56de\u6eda\u59d4\u6258\u7ed9 MyBatis\u3002<\/li>\n<li>\u4e8b\u52a1\u96c6\u6210\u70b9<br \/>\nSpring \u901a\u8fc7 DataSourceTransactionManager \u7ba1\u7406\u6570\u636e\u5e93\u4e8b\u52a1\uff0c\u800c SqlSessionTemplate \u4f1a\u611f\u77e5\u4e8b\u52a1\u7684\u5b58\u5728\uff0c\u786e\u4fdd\u4e0e\u4e8b\u52a1\u7ba1\u7406\u5668\u4fdd\u6301\u4e00\u81f4\u3002<\/li>\n<\/ul>\n<ol start=\"5\">\n<li>\n<h2>MyBatis \u914d\u7f6e\u7684\u5b9a\u5236\u5316<\/h2>\n<p>MyBatis-Spring-Boot-Starter \u652f\u6301\u9ad8\u5ea6\u7684\u914d\u7f6e\u5b9a\u5236\u5316\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u81ea\u5b9a\u4e49\u884c\u4e3a\uff1a<\/p>\n<ul>\n<li>\u914d\u7f6e\u5c5e\u6027<br \/>\n\u901a\u8fc7 application.properties \u6216 application.yml \u6587\u4ef6\uff0c\u8bbe\u7f6e\u4e0e MyBatis \u76f8\u5173\u7684\u5c5e\u6027\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>mybatis:<br \/>\nconfig-location: classpath:mybatis-config.xml<br \/>\nmapper-locations: classpath:mapper\/*.xml<br \/>\ntype-aliases-package: com.example.domain<\/p>\n<ul>\n<li>\u8986\u76d6\u9ed8\u8ba4\u914d\u7f6e<br \/>\n\u5982\u679c\u9700\u8981\u66f4\u590d\u6742\u7684\u81ea\u5b9a\u4e49\u914d\u7f6e\uff0c\u53ef\u4ee5\u5b9e\u73b0 SqlSessionFactoryBeanCustomizer \u63a5\u53e3\uff0c\u624b\u52a8\u4fee\u6539 SqlSessionFactoryBean \u7684\u5c5e\u6027\u3002<\/li>\n<li>\u52a8\u6001 SQL<br \/>\n\u4f7f\u7528 MyBatis \u7684\u52a8\u6001 SQL \u529f\u80fd\uff0c\u901a\u8fc7\u6761\u4ef6\u52a8\u6001\u751f\u6210 SQL \u67e5\u8be2\u8bed\u53e5\u3002<\/li>\n<\/ul>\n<ol start=\"6\">\n<li>\n<h2>\u63d2\u4ef6\u673a\u5236<\/h2>\n<p>MyBatis \u652f\u6301\u81ea\u5b9a\u4e49\u63d2\u4ef6\uff08Interceptor\uff09\uff0c\u53ef\u4ee5\u62e6\u622a\u4ee5\u4e0b\u56db\u79cd\u64cd\u4f5c\uff1a<\/p>\n<ul>\n<li>Executor\uff1a\u6267\u884c SQL \u8bed\u53e5\u3002<\/li>\n<li>ParameterHandler\uff1a\u5904\u7406 SQL \u53c2\u6570\u3002<\/li>\n<li>ResultSetHandler\uff1a\u5904\u7406\u7ed3\u679c\u96c6\u3002<\/li>\n<li>StatementHandler\uff1a\u5904\u7406 SQL \u8bed\u53e5\u7684\u6784\u5efa\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>Spring Boot \u4f1a\u81ea\u52a8\u52a0\u8f7d\u7528\u6237\u5b9a\u4e49\u7684\u63d2\u4ef6\uff0c\u53ea\u9700\u5728\u914d\u7f6e\u4e2d\u6ce8\u518c\u63d2\u4ef6\u7c7b\u3002<\/p>\n<ol start=\"7\">\n<li>\n<h2>\u4e0e Spring Boot DevTools \u7684\u96c6\u6210<\/h2>\n<p>MyBatis-Spring-Boot-Starter \u652f\u6301 Spring Boot \u7684\u5f00\u53d1\u5de5\u5177\uff08DevTools\uff09\uff0c\u5728\u4ee3\u7801\u70ed\u91cd\u8f7d\u65f6\u80fd\u591f\u6b63\u786e\u5237\u65b0 Mapper \u63a5\u53e3\u548c\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n<\/li>\n<li>\n<h2>\u4f18\u70b9\u603b\u7ed3<\/h2>\n<ul>\n<li>\u7b80\u5316\u914d\u7f6e\uff1a\u65e0\u9700\u624b\u52a8\u914d\u7f6e DataSource\u3001SqlSessionFactory \u548c Mapper\u3002<\/li>\n<li>\u4e0e Spring \u5b8c\u7f8e\u96c6\u6210\uff1a\u652f\u6301\u58f0\u660e\u5f0f\u4e8b\u52a1\u3001\u4f9d\u8d56\u6ce8\u5165\u548c Spring \u751f\u547d\u5468\u671f\u7ba1\u7406\u3002<\/li>\n<li>\u7075\u6d3b\u6027\uff1a\u652f\u6301\u9ad8\u5ea6\u5b9a\u5236\u5316\u7684 MyBatis \u914d\u7f6e\u3002<\/li>\n<li>\u5f00\u7bb1\u5373\u7528\uff1a\u63d0\u4f9b\u9ed8\u8ba4\u914d\u7f6e\uff0c\u51cf\u5c11\u4e86\u5f00\u53d1\u590d\u6742\u5ea6\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h2>Reference<\/h2>\n<ul>\n<li><a href=\"https:\/\/github.com\/mybatis\/spring-boot-starter\/blob\/master\/mybatis-spring-boot-autoconfigure\/src\/site\/zh_CN\/markdown\/index.md\">https:\/\/github.com\/mybatis\/spring-boot-starter\/blob\/master\/mybatis-spring-boot-autoconfigure\/src\/site\/zh_CN\/markdown\/index.md<\/a><\/li>\n<li><a href=\"https:\/\/mybatis.org\/spring-boot-starter\/mybatis-spring-boot-autoconfigure\/zh_CN\/index.html\">https:\/\/mybatis.org\/spring-boot-starter\/mybatis-spring-boot-autoconfigure\/zh_CN\/index.html<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Overview \u8981\u4e0e Spring \u4e00\u8d77\u4f7f\u7528 MyBatis\uff0c\u4f60\u81f3\u5c11\u9700\u8981\u4e00\u4e2a SqlSessionFactory \u548c\u4e00\u4e2a mapper \u63a5\u53e3\u3002 MyBatis-Spring-Boot-Starter \u5c06\u4f1a\uff1a \u81ea\u52a8\u63a2\u6d4b\u5b58\u5728\u7684 DataSource \u5c06\u4f7f\u7528 SqlSessionFactoryBean \u521b\u5efa\u5e76\u6ce8\u518c\u4e00\u4e2a SqlSessionFactory \u7684\u5b9e\u4f8b\uff0c\u5e76\u5c06\u63a2\u6d4b\u5230\u7684 DataSource \u4f5c\u4e3a\u6570\u636e\u6e90 \u5c06\u521b\u5efa\u5e76\u6ce8\u518c\u4e00\u4e2a\u4ece SqlSessionFactory \u4e2d\u5f97\u5230\u7684 SqlSessionTemplate \u7684\u5b9e\u4f8b \u81ea\u52a8\u626b\u63cf\u4f60\u7684 mapper\uff0c\u5c06\u5b83\u4eec\u4e0e SqlSessionTemplate \u76f8\u5173\u8054\uff0c\u5e76\u5c06\u5b83\u4eec\u6ce8\u518c\u5230Spring \u7684\u73af\u5883\uff08context\uff09\u4e2d\u53bb\uff0c\u8fd9\u6837\u5b83\u4eec\u5c31\u53ef\u4ee5\u88ab\u6ce8\u5165\u5230\u4f60\u7684 bean \u4e2d MyBatis-Spring-Boot-Starter \u662f\u4e00\u4e2a\u5f00\u7bb1\u5373\u7528\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u7528\u4e8e\u5c06 MyBatis \u4e0e Spring Boot \u96c6\u6210\u3002\u5b83\u7b80\u5316\u4e86 MyBatis \u7684\u914d\u7f6e\u548c\u4f7f\u7528\uff0c\u540c\u65f6\u5145\u5206\u5229\u7528\u4e86 Spring \u7684\u4f9d\u8d56\u6ce8\u5165\u548c\u751f\u547d\u5468\u671f\u7ba1\u7406\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u5b83\u7684\u5b9e\u73b0\u539f\u7406\u7684\u8be6\u7ec6\u89e3\u6790\uff1a \u81ea\u52a8\u914d\u7f6e\u7684\u5b9e\u73b0 MyBatis-Spring-Boot-Starter \u5229\u7528\u4e86 Spring Boot \u7684\u81ea\u52a8\u914d\u7f6e\u673a\u5236\uff0c\u901a\u8fc7 @EnableAutoConfiguration \u548c spring.factories \u6587\u4ef6\u5b9e\u73b0\u81ea\u52a8\u5316\u914d\u7f6e\u3002 spring.factories MyBatis-Spring-Boot-Starter [&hellip;] <a class=\"read-more\" href=\"https:\/\/www.fanyamin.com\/wordpress\/?p=1603\" title=\"Permanent Link to: mybatis spring boot starter \u662f\u600e\u4e48\u505a\u7684\">&rarr;Read&nbsp;more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1603","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1603"}],"collection":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1603"}],"version-history":[{"count":5,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1603\/revisions"}],"predecessor-version":[{"id":1608,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1603\/revisions\/1608"}],"wp:attachment":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}