{"id":2001,"date":"2017-06-16T11:16:25","date_gmt":"2017-06-16T03:16:25","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=2001"},"modified":"2026-08-18T11:23:50","modified_gmt":"2026-08-18T03:23:50","slug":"springboot%e4%b8%8emybatis%e9%9b%86%e6%88%90","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=2001","title":{"rendered":"SpringBoot\u4e0eMybatis\u96c6\u6210"},"content":{"rendered":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<p style=\"margin: 1em 0px;\">\u5728 Spring Boot \u91cc\u63a5 MyBatis\uff0c\u6838\u5fc3\u5c31\u4e94\u6b65\uff1a<strong style=\"color: #4870ac;\">starter \u4f9d\u8d56<\/strong>\u3001\u5b9e\u4f53 \/ Mapper \/ XML\u3001<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">application.yml<\/code> \u6570\u636e\u6e90\u3001Mapper \u626b\u63cf\u3001\u518d\u5199\u4e00\u4e2a\u767b\u5f55 Controller \u9a8c\u8bc1\u3002\u4e0b\u9762\u6309\u539f\u6587\u5de5\u7a0b <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">com.ittx.java1608.sbdemo2<\/code> \u8d70\u4e00\u904d\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u5305\u540d\u91cc\u7684 mybaits \u662f\u539f\u6587\u76ee\u5f55\u62fc\u5199\uff0c\u914d\u7f6e\u8def\u5f84\u4fdd\u6301\u539f\u6837\uff0c\u907f\u514d\u5bf9\u4e0d\u4e0a\u626b\u63cf\u3002<\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e00\u3001\u5f15\u5165\u4f9d\u8d56<\/h2>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">&lt;!-- mybatis\/spring \u5305 --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.mybatis.spring.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;mybatis-spring-boot-starter&lt;\/artifactId&gt;\n    &lt;version&gt;1.2.0&lt;\/version&gt;\n&lt;\/dependency&gt;\n\n&lt;!-- mysql \u6570\u636e\u9a71\u52a8 --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;mysql&lt;\/groupId&gt;\n    &lt;artifactId&gt;mysql-connector-java&lt;\/artifactId&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e8c\u3001Bean\u3001\u63a5\u53e3\u548c\u6620\u5c04\u6587\u4ef6<\/h2>\n<p style=\"margin: 1em 0px;\">\u5b9e\u4f53\u3001Dao \u63a5\u53e3\u548c Mapper XML \u53ef\u7528\u63d2\u4ef6\u751f\u6210\u3002\u5de5\u7a0b\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<p style=\"margin: 1em 0px;\"><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2017\/06\/mybaits.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2002\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2017\/06\/mybaits.png\" alt=\"mybaits\" width=\"312\" height=\"319\" \/><\/a><\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e09\u3001application.yml\uff1a\u6570\u636e\u6e90\u548c Mapper \u626b\u63cf<\/h2>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">spring:\n  mvc:\n    view:\n     prefix: \/WEB-INF\/views\/\n     suffix: .jsp\n  datasource:\n    driver-class-name: com.mysql.jdbc.Driver\n    url: jdbc:mysql:\/\/localhost:3306\/springboot?characterEncoding=utf8&amp;useSSL=true\n    username: root\n    password: b123\nmybatis:\n  mapper-locations: classpath:com\/ittx\/java1608\/sbdemo2\/mapping\/*.xml\n\n# springboot \u4e2d\u914d\u7f6e mybatis \u65e5\u5fd7\u8f93\u51fa\nlogging:\n    level:\n        com.ittx.java1608.sbdemo2.dao: TRACE<\/code><\/pre>\n<table style=\"display: table; text-align: left; overflow: auto; border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; overflow-wrap: break-word; word-break: break-all;\" width=\"800\">\n<thead>\n<tr>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u914d\u7f6e\u9879<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u4f5c\u7528<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\"><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">spring.datasource.*<\/code><\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">JDBC \u8fde\u63a5 MySQL<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\"><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">mybatis.mapper-locations<\/code><\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u626b\u63cf mapping \u76ee\u5f55\u4e0b\u7684 XML<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\"><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">logging.level...dao: TRACE<\/code><\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u63a7\u5236\u53f0\u6253\u5370 Mapper SQL<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u56db\u3001MapperScannerConfigurer<\/h2>\n<p style=\"margin: 1em 0px;\">\u76f8\u5f53\u4e8e\u4f20\u7edf xml \u91cc\u7684 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">MapperScannerConfigurer<\/code>\uff1a\u544a\u8bc9 Spring \u53bb\u54ea\u4e2a\u5305\u627e Dao \u63a5\u53e3\uff0c\u5e76\u7ed1\u5230 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">sqlSessionFactory<\/code>\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\/**\n * MyBatis \u626b\u63cf\u63a5\u53e3\n *  &lt;!-- DAO \u63a5\u53e3\u6240\u5728\u5305\u540d\uff0cSpring \u4f1a\u81ea\u52a8\u67e5\u627e\u5176\u4e0b\u7684\u7c7b --&gt;\n    &lt;bean class=\"org.mybatis.spring.mapper.MapperScannerConfigurer\"&gt;\n        &lt;property name=\"basePackage\" value=\"com.ittx.java1608.sbdemo2.dao\" \/&gt;\n        &lt;property name=\"sqlSessionFactoryBeanName\" value=\"sqlSessionFactory\"&gt;&lt;\/property&gt;\n    &lt;\/bean&gt;\n *\/\n@Configuration\npublic class MyBatisMapperScannerConfig {\n\n    @Bean\n    public MapperScannerConfigurer mapperScannerConfigurer() {\n        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();\n        mapperScannerConfigurer.setSqlSessionFactoryBeanName(\"sqlSessionFactory\");\n        mapperScannerConfigurer.setBasePackage(\"com.ittx.java1608.sbdemo2.dao\");\n        return mapperScannerConfigurer;\n    }\n}<\/code><\/pre>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e94\u3001\u7528\u767b\u5f55\u63a5\u53e3\u9a8c\u8bc1<\/h2>\n<p style=\"margin: 1em 0px;\">\u914d\u7f6e\u5b8c\u6210\u540e\uff0c\u6ce8\u5165 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">UserMapper<\/code> \u505a\u767b\u5f55\u6821\u9a8c\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">@Controller\npublic class LoginController {\n    @Autowired\n    private UserMapper userMapper;\n\n    @RequestMapping(\"\/login\")\n    public String login() {\n        return \"login\";\n    }\n\n    @RequestMapping(\"\/login_submit\")\n    public String loginSubmit(String username, String password, Model model) {\n        User user = userMapper.checkLogin(username, password);\n        if (user != null) {\n            model.addAttribute(\"message\", \"\u767b\u5f55\u6210\u529f!\");\n        } else {\n            model.addAttribute(\"message\", \"\u7528\u6237\u540d\u6216\u5bc6\u7801\u9519\u8bef!\");\n        }\n        return \"login\";\n    }\n}<\/code><\/pre>\n<blockquote style=\"display: block; font-size: 0.9em; border-left: 3px solid #4870ac; padding: 0.5em 1em; margin: 0px; background: #f6f8fa; color: #40464f;\">\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1aSpring Boot \u96c6\u6210 MyBatis\uff1astarter + \u6570\u636e\u6e90 yml + mapper-locations + \u63a5\u53e3\u5305\u626b\u63cf\uff1b\u7528\u4e00\u6b21\u771f\u5b9e\u67e5\u8be2\uff08\u4f8b\u5982\u767b\u5f55\uff09\u786e\u8ba4 SQL \u80fd\u6253\u51fa\u6765\u3001\u7ed3\u679c\u80fd\u5c01\u88c5\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Spring Boot \u91cc\u63a5 MyBatis\uff0c\u6838\u5fc3\u5c31\u4e94\u6b65\uff1astarter \u4f9d\u8d56\u3001\u5b9e\u4f53 \/ Mapper \/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[93],"tags":[188,184,186],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2001"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2001"}],"version-history":[{"count":3,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2001\/revisions"}],"predecessor-version":[{"id":2014,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2001\/revisions\/2014"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}