{"id":2001,"date":"2017-06-16T11:16:25","date_gmt":"2017-06-16T03:16:25","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=2001"},"modified":"2017-06-16T11:44:44","modified_gmt":"2017-06-16T03:44:44","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":"<h2>\u4e00\u3001SpringBoot\u4e0eMybatis\u96c6\u6210\u6b65\u9aa4\u5982\u4e0b<\/h2>\n<h3>1. \u5f15\u5165mybatis-spring-boot\u76f8\u5173\u4f9d\u8d56<\/h3>\n<p>[java]&lt;!&#8211; mybatis\/spring\u5305 &#8211;&gt;<br \/>\n&lt;dependency&gt;<br \/>\n\t&lt;groupId&gt;org.mybatis.spring.boot&lt;\/groupId&gt;<br \/>\n\t&lt;artifactId&gt;mybatis-spring-boot-starter&lt;\/artifactId&gt;<br \/>\n\t&lt;version&gt;1.2.0&lt;\/version&gt;<br \/>\n&lt;\/dependency&gt;<\/p>\n<p>&lt;!&#8211; mysql\u6570\u636e\u9a71\u52a8 &#8211;&gt;<br \/>\n&lt;dependency&gt;<br \/>\n\t&lt;groupId&gt;mysql&lt;\/groupId&gt;<br \/>\n\t&lt;artifactId&gt;mysql-connector-java&lt;\/artifactId&gt;<br \/>\n&lt;\/dependency&gt;[\/java]<\/p>\n<h3>2. \u5b9e\u73b0mybaits\u76f8\u5173Bean\u7c7b,\u63a5\u53e3\u548c\u6620\u5c04\u6587\u4ef6(\u53ef\u7528\u63d2\u4ef6\u751f\u6210)<\/h3>\n<p><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<h3>3. \u5728application.yml\u6587\u4ef6\u4e2d\u6dfb\u52a0\u6570\u636e\u6e90\u548cmybaits\u6620\u5c04\u6587\u4ef6\u626b\u63cf\u914d\u7f6e<\/h3>\n<p>[java] spring:<br \/>\n  mvc:<br \/>\n    view:<br \/>\n     prefix: \/WEB-INF\/views\/<br \/>\n     suffix: .jsp<br \/>\n  datasource:<br \/>\n    driver-class-name: com.mysql.jdbc.Driver<br \/>\n    url: jdbc:mysql:\/\/localhost:3306\/springboot?characterEncoding=utf8&amp;useSSL=true<br \/>\n    username: root<br \/>\n    password: b123<br \/>\nmybatis:<br \/>\n  mapper-locations: classpath:com\/ittx\/java1608\/sbdemo2\/mapping\/*.xml<\/p>\n<p># springboot\u4e2d\u914d\u7f6emybaits\u65e5\u5fd7\u8f93\u51fa<br \/>\nlogging:<br \/>\n    level:<br \/>\n        com.ittx.java1608.sbdemo2.dao: TRACE<\/p>\n<p>[\/java]<\/p>\n<h3>4. \u7f16\u5199\u5de5\u5177\u7c7b\u5b9e\u73b0sqlSessionFactory\u548cmybatis\u63a5\u53e3\u626b\u63cf<\/h3>\n<p>[java]\/**<br \/>\n * MyBatis\u626b\u63cf\u63a5\u53e3<br \/>\n *  &lt;!&#8211; DAO\u63a5\u53e3\u6240\u5728\u5305\u540d\uff0cSpring\u4f1a\u81ea\u52a8\u67e5\u627e\u5176\u4e0b\u7684\u7c7b &#8211;&gt;<br \/>\n\t&lt;bean class=&quot;org.mybatis.spring.mapper.MapperScannerConfigurer&quot;&gt;<br \/>\n\t\t&lt;property name=&quot;basePackage&quot; value=&quot;com.ittx.java1608.sbdemo2.dao&quot; \/&gt;<br \/>\n\t\t&lt;property name=&quot;sqlSessionFactoryBeanName&quot; value=&quot;sqlSessionFactory&quot;&gt;&lt;\/property&gt;<br \/>\n\t&lt;\/bean&gt;<br \/>\n *\/<br \/>\n@Configuration<br \/>\npublic class MyBatisMapperScannerConfig {<\/p>\n<p>\t@Bean<br \/>\n\tpublic MapperScannerConfigurer mapperScannerConfigurer() {<br \/>\n\t\tMapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();<br \/>\n\t\tmapperScannerConfigurer.setSqlSessionFactoryBeanName(&quot;sqlSessionFactory&quot;);<br \/>\n\t\tmapperScannerConfigurer.setBasePackage(&quot;com.ittx.java1608.sbdemo2.dao&quot;);<br \/>\n\t\treturn mapperScannerConfigurer;<br \/>\n\t}<br \/>\n}[\/java]<\/p>\n<h3>5. \u7ecf\u8fc7\u4ee5\u4e0a\u914d\u7f6espringboot\u5df2\u6210\u529f\u96c6\u6210mybatis.\u7f16\u5199\u6d4b\u8bd5LoginController\u5b9e\u73b0\u767b\u5f55<\/h3>\n<p>[java]@Controller<br \/>\npublic class LoginController {<br \/>\n\t@Autowired<br \/>\n\tprivate UserMapper userMapper;<\/p>\n<p>\t@RequestMapping(&quot;\/login&quot;)<br \/>\n\tpublic String login(){<br \/>\n\t\treturn &quot;login&quot;;<br \/>\n\t}<\/p>\n<p>\t@RequestMapping(&quot;\/login_submit&quot;)<br \/>\n\tpublic String loginSubmit(String username,String password,Model model){<br \/>\n\t\tUser user = userMapper.checkLogin(username, password);<br \/>\n\t\tif(user != null){<br \/>\n\t\t\tmodel.addAttribute(&quot;message&quot;, &quot;\u767b\u5f55\u6210\u529f!&quot;);<br \/>\n\t\t}else{<br \/>\n\t\t\tmodel.addAttribute(&quot;message&quot;, &quot;\u7528\u6237\u540d\u6216\u5bc6\u7801\u9519\u8bef!&quot;);<br \/>\n\t\t}<br \/>\n\t\treturn &quot;login&quot;;<br \/>\n\t}<br \/>\n}[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001SpringBoot\u4e0eMybatis\u96c6\u6210\u6b65\u9aa4\u5982\u4e0b 1. \u5f15\u5165mybatis-spring-boot\u76f8\u5173\u4f9d\u8d56 [&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":[],"_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":5,"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}]}}