{"id":272267,"date":"2024-05-16T13:12:22","date_gmt":"2024-05-16T05:12:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/272267.html"},"modified":"2024-05-16T13:12:34","modified_gmt":"2024-05-16T05:12:34","slug":"springboot-%e9%a1%b9%e7%9b%ae%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-mybatis","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/272267.html","title":{"rendered":"springboot \u9879\u76ee\u4e2d\u5982\u4f55\u4f7f\u7528 Mybatis"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24194719\/8763b445-6f48-40d4-8cf0-99009098fe7d.webp\" alt=\"springboot \u9879\u76ee\u4e2d\u5982\u4f55\u4f7f\u7528 Mybatis\" \/><\/p>\n<p><p>\u5728Spring Boot\u9879\u76ee\u4e2d\u4f7f\u7528MyBatis\u53ef\u4ee5\u6781\u5927\u5730\u7b80\u5316\u6570\u636e\u5e93\u64cd\u4f5c\u548c\u589e\u52a0\u9879\u76ee\u7684\u5f00\u53d1\u6548\u7387\u3002<strong>\u5173\u952e\u6b65\u9aa4\u5305\u62ec\u5f15\u5165MyBatis\u4f9d\u8d56\u3001\u914d\u7f6e\u6570\u636e\u6e90\u3001\u521b\u5efa\u5b9e\u4f53\u548cMapper\u63a5\u53e3\u3001\u7f16\u5199Mapper XML\u6587\u4ef6<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u5f15\u5165MyBatis\u4f9d\u8d56<\/strong>\u662f\u57fa\u7840\u4e14\u5173\u952e\u7684\u4e00\u6b65\uff0c\u56e0\u4e3a\u5b83\u80fd\u786e\u4fdd\u9879\u76ee\u62e5\u6709MyBatis\u63d0\u4f9b\u7684\u6240\u6709\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u8be6\u7ec6\u6765\u8bf4\uff0c\u5f15\u5165MyBatis\u4f9d\u8d56\u610f\u5473\u7740\u5728\u9879\u76ee\u7684<code>pom.xml<\/code>\u6587\u4ef6\u4e2d\u52a0\u5165\u4e0eMyBatis\u76f8\u5173\u7684Maven\u4f9d\u8d56\u3002\u5bf9\u4e8eSpring Boot\u9879\u76ee\uff0c\u901a\u5e38\u63a8\u8350\u4f7f\u7528<code>mybatis-spring-boot-starter<\/code>\uff0c\u8fd9\u662f\u4e00\u4e2a\u5b98\u65b9\u63d0\u4f9b\u7684\u542f\u52a8\u5668\uff0c\u5b83\u80fd\u81ea\u52a8\u914d\u7f6eMyBatis\u4e0eSpring Boot\u7684\u96c6\u6210\uff0c\u7b80\u5316\u4e86\u9879\u76ee\u7684\u914d\u7f6e\u5de5\u4f5c\u3002\u5f15\u5165\u8fd9\u4e2a\u4f9d\u8d56\u540e\uff0c\u9879\u76ee\u5c31\u53ef\u4ee5\u4f7f\u7528MyBatis\u8fdb\u884c\u6570\u636e\u5e93<\/p>\n<p>\u64cd\u4f5c\u4e86\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5f15\u5165MYBATIS\u4f9d\u8d56<\/h2>\n<\/p>\n<p><p>\u8981\u5728Spring Boot\u9879\u76ee\u4e2d\u4f7f\u7528MyBatis\uff0c\u9996\u5148\u9700\u8981\u5728\u9879\u76ee\u7684<code>pom.xml<\/code>\u6587\u4ef6\u4e2d\u52a0\u5165MyBatis-Spring-Boot-Starter\u7684\u4f9d\u8d56\u3002\u8fd9\u4e2a\u4f9d\u8d56\u5c06\u4f1a\u81ea\u52a8\u914d\u7f6eMyBatis\u4e0eSpring Boot\u7684\u96c6\u6210\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e00\u5957\u9ed8\u8ba4\u914d\u7f6e\uff0c\u8ba9\u5f00\u53d1\u8005\u80fd\u5feb\u901f\u5f00\u59cb\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-xml\">&lt;dependency&gt;<\/p>\n<p>    &lt;groupId&gt;org.mybatis.spring.boot&lt;\/groupId&gt;<\/p>\n<p>    &lt;artifactId&gt;mybatis-spring-boot-starter&lt;\/artifactId&gt;<\/p>\n<p>    &lt;version&gt;2.1.4&lt;\/version&gt;<\/p>\n<p>&lt;\/dependency&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5f15\u5165\u4f9d\u8d56\u540e\uff0cMyBatis\u4f1a\u4e0eSpring Boot\u81ea\u52a8\u96c6\u6210\uff0c\u4f60\u53ea\u9700\u8981\u8fdb\u884c\u6700\u7b80\u5355\u7684\u914d\u7f6e\u5373\u53ef\u5f00\u59cb\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u914d\u7f6e\u6570\u636e\u6e90<\/h2>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u9700\u8981\u5728<code>application.properties<\/code>\u6216<code>application.yml<\/code>\u6587\u4ef6\u4e2d\u914d\u7f6e\u6570\u636e\u5e93\u7684\u6570\u636e\u6e90\u3002Spring Boot\u652f\u6301\u591a\u79cd\u7c7b\u578b\u7684\u6570\u636e\u6e90\u914d\u7f6e\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u4e0eMyBatis\u96c6\u6210\u3002\u8fd9\u4e00\u6b65\u786e\u4fdd\u4e86Spring Boot\u5e94\u7528\u80fd\u591f\u8fde\u63a5\u5230\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-yaml\">spring:<\/p>\n<p>  datasource:<\/p>\n<p>    url: jdbc:mysql:\/\/localhost:3306\/\u6570\u636e\u5e93\u540d?useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=UTC<\/p>\n<p>    username: \u7528\u6237\u540d<\/p>\n<p>    password: \u5bc6\u7801<\/p>\n<p>    driver-class-name: com.mysql.cj.jdbc.Driver<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b63\u786e\u914d\u7f6e\u6570\u636e\u6e90\u662f\u786e\u4fdd\u5e94\u7528\u80fd\u591f\u987a\u5229\u8fd0\u884c\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u521b\u5efa\u5b9e\u4f53\u548cMapper\u63a5\u53e3<\/h2>\n<\/p>\n<p><p>\u6709\u4e86\u6570\u636e\u6e90\uff0c\u4e0b\u4e00\u6b65\u662f\u6839\u636e\u6570\u636e\u5e93\u8868\u7ed3\u6784\u521b\u5efa\u76f8\u5e94\u7684\u5b9e\u4f53\uff08Entity\uff09\u7c7b\u3002\u5b9e\u4f53\u7c7b\u662f\u6570\u636e\u5e93\u8868\u5728Java\u4e2d\u7684\u6620\u5c04\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">public class User {<\/p>\n<p>    private Integer id;<\/p>\n<p>    private String name;<\/p>\n<p>    private String em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l;<\/p>\n<p>    \/\/ Getters and Setters omitted for brevity<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b9e\u4f53\u7c7b\u521b\u5efa\u5b8c\u6bd5\u540e\uff0c\u63a5\u4e0b\u6765\u9700\u8981\u521b\u5efaMapper\u63a5\u53e3\u3002Mapper\u63a5\u53e3\u5b9a\u4e49\u4e86\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u65b9\u6cd5\uff0cMyBatis\u4f1a\u81ea\u52a8\u5b9e\u73b0\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">@Mapper<\/p>\n<p>public interface UserMapper {<\/p>\n<p>    User selectUser(Integer id);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u7f16\u5199Mapper XML\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u867d\u7136MyBatis\u652f\u6301\u6ce8\u89e3\u65b9\u5f0f\u5b9a\u4e49SQL\u8bed\u53e5\uff0c\u4f46\u5728\u590d\u6742SQL\u7684\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528XML\u914d\u7f6e\u6620\u5c04\u6587\u4ef6\u4f1a\u66f4\u52a0\u65b9\u4fbf\u3002\u5728<code>resources<\/code>\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u4e0eMapper\u63a5\u53e3\u540c\u540d\u7684XML\u6587\u4ef6\uff08\u6539\u540e\u7f00\u540d\u4e3a<code>.xml<\/code>\uff09\uff0c\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u7f16\u5199SQL\u8bed\u53e5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;<\/p>\n<p>&lt;!DOCTYPE mapper PUBLIC &quot;-\/\/mybatis.org\/\/DTD Mapper 3.0\/\/EN&quot; &quot;http:\/\/mybatis.org\/dtd\/mybatis-3-mapper.dtd&quot;&gt;<\/p>\n<p>&lt;mapper namespace=&quot;com.example.demo.mapper.UserMapper&quot;&gt;<\/p>\n<p>    &lt;select id=&quot;selectUser&quot; resultType=&quot;com.example.demo.entity.User&quot;&gt;<\/p>\n<p>        SELECT * FROM user WHERE id = #{id}<\/p>\n<p>    &lt;\/select&gt;<\/p>\n<p>&lt;\/mapper&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u96c6\u6210\u6d4b\u8bd5<\/h2>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u8fdb\u884c\u96c6\u6210\u6d4b\u8bd5\u786e\u4fdd\u6240\u6709\u914d\u7f6e\u6b63\u786e\u65e0\u8bef\u3002\u53ef\u4ee5\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u6216\u4f7f\u7528Spring Boot\u63d0\u4f9b\u7684\u6d4b\u8bd5\u529f\u80fd\u6765\u6d4b\u8bd5Mapper\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">@SpringBootTest<\/p>\n<p>public class MyBatisTest {<\/p>\n<p>    @Autowired<\/p>\n<p>    private UserMapper userMapper;<\/p>\n<p>    @Test<\/p>\n<p>    public void testSelectUser() {<\/p>\n<p>        User user = userMapper.selectUser(1);<\/p>\n<p>        Assert.assertNotNull(&quot;\u67e5\u8be2\u7ed3\u679c\u4e0d\u4e3a\u7a7a&quot;, user);<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u6837\u7684\u6d41\u7a0b\uff0c\u53ef\u4ee5\u5728Spring Boot\u9879\u76ee\u4e2d\u987a\u5229\u5730\u4f7f\u7528MyBatis\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3002\u4ece\u5f15\u5165\u4f9d\u8d56\u5f00\u59cb\uff0c\u6bcf\u4e00\u6b65\u90fd\u4e3a\u540e\u7eed\u7684\u5f00\u53d1\u5960\u5b9a\u4e86\u57fa\u7840\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u590d\u6742SQL\u548c\u5927\u578b\u9879\u76ee\u65f6\uff0cMyBatis\u5c55\u73b0\u51fa\u4e86\u5b83\u5f3a\u5927\u7684\u529f\u80fd\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u9075\u5faa\u8fd9\u4e9b\u6b65\u9aa4\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u9ad8\u6548\u3001\u51c6\u786e\u5730\u96c6\u6210MyBatis\uff0c\u5145\u5206\u5229\u7528Spring Boot\u548cMyBatis\u7684\u7279\u6027\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u9879\u76ee\u7684\u8d28\u91cf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q\uff1aSpringBoot\u9879\u76ee\u4e2d\u5982\u4f55\u96c6\u6210Mybatis\uff1f<\/strong><br \/>\nA\uff1a\u96c6\u6210Mybatis\u6709\u4e24\u79cd\u65b9\u5f0f\uff1aXML\u914d\u7f6e\u65b9\u5f0f\u548c\u6ce8\u89e3\u65b9\u5f0f\u3002XML\u914d\u7f6e\u65b9\u5f0f\u9700\u8981\u5728application.properties\u6587\u4ef6\u4e2d\u914d\u7f6e\u76f8\u5173Mybatis\u5c5e\u6027\uff0c\u5728resources\u76ee\u5f55\u4e0b\u521b\u5efamybatis-config.xml\u548cmapper\u6620\u5c04\u6587\u4ef6\uff1b\u800c\u6ce8\u89e3\u65b9\u5f0f\u5219\u662f\u5728Mapper\u63a5\u53e3\u4e0a\u4f7f\u7528@Mapper\u6ce8\u89e3\uff0c\u65e0\u9700\u989d\u5916\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n<p><strong>Q\uff1a\u5982\u4f55\u5728SpringBoot\u9879\u76ee\u4e2d\u4f7f\u7528Mybatis\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff1f<\/strong><br \/>\nA\uff1a\u9996\u5148\uff0c\u5728SpringBoot\u4e3b\u914d\u7f6e\u7c7b\u4e0a\u6dfb\u52a0@EnableTransactionManagement\u6ce8\u89e3\u5f00\u542f\u4e8b\u52a1\u7ba1\u7406\uff1b\u7136\u540e\u5728Mapper\u63a5\u53e3\u4e0a\u4f7f\u7528@Mapper\u6ce8\u89e3\u6807\u8bc6\u4e3aMybatis\u7684Mapper\u63a5\u53e3\uff1b\u6700\u540e\uff0c\u5728Service\u5c42\u6216Controller\u5c42\u4e2d\u6ce8\u5165Mapper\u63a5\u53e3\uff0c\u5e76\u8c03\u7528\u5176\u4e2d\u7684\u65b9\u6cd5\u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3002<\/p>\n<p><strong>Q\uff1a\u5728SpringBoot\u9879\u76ee\u4e2d\uff0c\u5982\u4f55\u914d\u7f6e\u591a\u6570\u636e\u6e90\u5e76\u5728Mybatis\u4e2d\u4f7f\u7528\uff1f<\/strong><br \/>\nA\uff1a\u8981\u914d\u7f6e\u591a\u6570\u636e\u6e90\uff0c\u9996\u5148\u9700\u5728application.properties\u6587\u4ef6\u4e2d\u914d\u7f6e\u591a\u4e2a\u6570\u636e\u6e90\u7684\u76f8\u5173\u5c5e\u6027\uff1b\u7136\u540e\uff0c\u5728SpringBoot\u4e3b\u914d\u7f6e\u7c7b\u4e0a\u4f7f\u7528@Primary\u6ce8\u89e3\u6807\u8bc6\u9ed8\u8ba4\u6570\u636e\u6e90\uff1b\u63a5\u7740\uff0c\u521b\u5efa\u591a\u4e2a\u6570\u636e\u6e90\u5bf9\u5e94\u7684SqlSessionFactory\u548cTransactionManager\uff1b\u6700\u540e\uff0c\u5728Mapper\u63a5\u53e3\u4e0a\u4f7f\u7528@Qualifier\u6ce8\u89e3\u6307\u5b9a\u4f7f\u7528\u7684\u6570\u636e\u6e90\uff0c\u6765\u5b9e\u73b0\u5728Mybatis\u4e2d\u4f7f\u7528\u591a\u6570\u636e\u6e90\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Spring Boot\u9879\u76ee\u4e2d\u4f7f\u7528MyBatis\u53ef\u4ee5\u6781\u5927\u5730\u7b80\u5316\u6570\u636e\u5e93\u64cd\u4f5c\u548c\u589e\u52a0\u9879\u76ee\u7684\u5f00\u53d1\u6548\u7387\u3002\u5173\u952e\u6b65\u9aa4\u5305\u62ec\u5f15\u5165 [&hellip;]","protected":false},"author":3,"featured_media":272284,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/272267"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=272267"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/272267\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/272284"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=272267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=272267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=272267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}