Spring Boot入门及集成MyBatis开发Web项目

转载 2016年08月30日 22:30:32

1、Maven构建Spring Boot

创建Maven Web工程,引入spring-boot-starter-parent依赖

<code class="hljs xml has-numbering"><span class="hljs-tag"><<span class="hljs-title">project</span> <span class="hljs-attribute">xmlns</span>=<span class="hljs-value">"http://maven.apache.org/POM/4.0.0"</span> <span class="hljs-attribute">xmlns:xsi</span>=<span class="hljs-value">"http://www.w3.org/2001/XMLSchema-instance"</span>
    <span class="hljs-attribute">xsi:schemaLocation</span>=<span class="hljs-value">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">modelVersion</span>></span>4.0.0<span class="hljs-tag"></<span class="hljs-title">modelVersion</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">groupId</span>></span>xyz.ibenben<span class="hljs-tag"></<span class="hljs-title">groupId</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">artifactId</span>></span>zhongdian<span class="hljs-tag"></<span class="hljs-title">artifactId</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">packaging</span>></span>war<span class="hljs-tag"></<span class="hljs-title">packaging</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">version</span>></span>0.0.1-SNAPSHOT<span class="hljs-tag"></<span class="hljs-title">version</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">name</span>></span>zhongdian Maven Webapp<span class="hljs-tag"></<span class="hljs-title">name</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">url</span>></span>http://maven.apache.org<span class="hljs-tag"></<span class="hljs-title">url</span>></span>

    <span class="hljs-comment"><!-- Inherit defaults from Spring Boot --></span>
    <span class="hljs-tag"><<span class="hljs-title">parent</span>></span>
        <span class="hljs-tag"><<span class="hljs-title">groupId</span>></span>org.springframework.boot<span class="hljs-tag"></<span class="hljs-title">groupId</span>></span>
        <span class="hljs-tag"><<span class="hljs-title">artifactId</span>></span>spring-boot-starter-parent<span class="hljs-tag"></<span class="hljs-title">artifactId</span>></span>
        <span class="hljs-tag"><<span class="hljs-title">version</span>></span>1.4.0.BUILD-SNAPSHOT<span class="hljs-tag"></<span class="hljs-title">version</span>></span>
    <span class="hljs-tag"></<span class="hljs-title">parent</span>></span>

    <span class="hljs-comment"><!-- Add typical dependencies for a web application --></span>
    <span class="hljs-tag"><<span class="hljs-title">dependencies</span>></span>
        <span class="hljs-tag"><<span class="hljs-title">dependency</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">groupId</span>></span>org.springframework.boot<span class="hljs-tag"></<span class="hljs-title">groupId</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">artifactId</span>></span>spring-boot-starter-web<span class="hljs-tag"></<span class="hljs-title">artifactId</span>></span>
        <span class="hljs-tag"></<span class="hljs-title">dependency</span>></span>
    <span class="hljs-tag"></<span class="hljs-title">dependencies</span>></span>



    <span class="hljs-comment"><!-- Package as an executable jar --></span>
    <span class="hljs-tag"><<span class="hljs-title">build</span>></span>
        <span class="hljs-tag"><<span class="hljs-title">plugins</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">plugin</span>></span>
                <span class="hljs-tag"><<span class="hljs-title">groupId</span>></span>org.springframework.boot<span class="hljs-tag"></<span class="hljs-title">groupId</span>></span>
                <span class="hljs-tag"><<span class="hljs-title">artifactId</span>></span>spring-boot-maven-plugin<span class="hljs-tag"></<span class="hljs-title">artifactId</span>></span>
            <span class="hljs-tag"></<span class="hljs-title">plugin</span>></span>
        <span class="hljs-tag"></<span class="hljs-title">plugins</span>></span>
    <span class="hljs-tag"></<span class="hljs-title">build</span>></span>

    <span class="hljs-comment"><!-- Add Spring repositories --></span>
    <span class="hljs-comment"><!-- (you don't need this if you are using a .RELEASE version) --></span>
    <span class="hljs-tag"><<span class="hljs-title">repositories</span>></span>
        <span class="hljs-tag"><<span class="hljs-title">repository</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">id</span>></span>spring-snapshots<span class="hljs-tag"></<span class="hljs-title">id</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">url</span>></span>http://repo.spring.io/snapshot<span class="hljs-tag"></<span class="hljs-title">url</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">snapshots</span>></span>
                <span class="hljs-tag"><<span class="hljs-title">enabled</span>></span>true<span class="hljs-tag"></<span class="hljs-title">enabled</span>></span>
            <span class="hljs-tag"></<span class="hljs-title">snapshots</span>></span>
        <span class="hljs-tag"></<span class="hljs-title">repository</span>></span>
        <span class="hljs-tag"><<span class="hljs-title">repository</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">id</span>></span>spring-milestones<span class="hljs-tag"></<span class="hljs-title">id</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">url</span>></span>http://repo.spring.io/milestone<span class="hljs-tag"></<span class="hljs-title">url</span>></span>
        <span class="hljs-tag"></<span class="hljs-title">repository</span>></span>
    <span class="hljs-tag"></<span class="hljs-title">repositories</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">pluginRepositories</span>></span>
        <span class="hljs-tag"><<span class="hljs-title">pluginRepository</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">id</span>></span>spring-snapshots<span class="hljs-tag"></<span class="hljs-title">id</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">url</span>></span>http://repo.spring.io/snapshot<span class="hljs-tag"></<span class="hljs-title">url</span>></span>
        <span class="hljs-tag"></<span class="hljs-title">pluginRepository</span>></span>
        <span class="hljs-tag"><<span class="hljs-title">pluginRepository</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">id</span>></span>spring-milestones<span class="hljs-tag"></<span class="hljs-title">id</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">url</span>></span>http://repo.spring.io/milestone<span class="hljs-tag"></<span class="hljs-title">url</span>></span>
        <span class="hljs-tag"></<span class="hljs-title">pluginRepository</span>></span>
    <span class="hljs-tag"></<span class="hljs-title">pluginRepositories</span>></span>
<span class="hljs-tag"></<span class="hljs-title">project</span>></span>
</code>

使用spring-boot-starter-parent来构建Spring Boot项目是一个很好的方法,但很多项目本身就是依赖其它的父模块的,再或者spring-boot-starter-parent默认提供的那么多配置和功能我们用不到。

我们也可以使用其它的依赖方式来引入Spring Boot。

<code class="hljs xml has-numbering"><span class="hljs-tag"><<span class="hljs-title">dependencyManagement</span>></span>
     <span class="hljs-tag"><<span class="hljs-title">dependencies</span>></span>
        <span class="hljs-tag"><<span class="hljs-title">dependency</span>></span>
            <span class="hljs-comment"><!-- Import dependency management from Spring Boot --></span>
            <span class="hljs-tag"><<span class="hljs-title">groupId</span>></span>org.springframework.boot<span class="hljs-tag"></<span class="hljs-title">groupId</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">artifactId</span>></span>spring-boot-dependencies<span class="hljs-tag"></<span class="hljs-title">artifactId</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">version</span>></span>1.4.0.BUILD-SNAPSHOT<span class="hljs-tag"></<span class="hljs-title">version</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">type</span>></span>pom<span class="hljs-tag"></<span class="hljs-title">type</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">scope</span>></span>import<span class="hljs-tag"></<span class="hljs-title">scope</span>></span>
        <span class="hljs-tag"></<span class="hljs-title">dependency</span>></span>
    <span class="hljs-tag"></<span class="hljs-title">dependencies</span>></span>
<span class="hljs-tag"></<span class="hljs-title">dependencyManagement</span>></span></code>

这是Spring官网给的demo配置,有兴趣的可以直接跳过去查看:http://docs.spring.io/spring-boot/docs/1.4.0.RC1/reference/htmlsingle/#getting-started-maven-installation

2、Spring Boot项目的代码结构

Spring Boot项目与一般的Java Web项目的代码结构没有太大的区别或要求,但为了减少配置的数量(无配置),Spring Boot也有一些比较好的建议。

<code class="hljs 1c has-numbering">com
 +- example
     +- myproject
         +- Application.java
         <span class="hljs-string">|</span>
         +- domain
         <span class="hljs-string">|   +- Customer.java</span>
         <span class="hljs-string">|   +- CustomerRepository.java</span>
         <span class="hljs-string">|</span>
         +- service
         <span class="hljs-string">|   +- CustomerService.java</span>
         <span class="hljs-string">|</span>
         +- web
             +- CustomerController.java</code>

2.1 不要使用默认的包路径

我们交由Spring管理的类,需要放入一个包下。如下图中的DefaultClass.java是不行的。因为Spring Boot对带注解的类进行扫描的时候,这些默认包路径下的类会出问题。

当然,基于代码规范的要求,一般的程序员都不会这样子构建自己的代码,这里说明是为了真的遇到这种情况出问题时,可以快速地解决问题。

这里写图片描述

2.2 Spring Boot应用入口

<code class="hljs avrasm has-numbering">package <span class="hljs-keyword">com</span><span class="hljs-preprocessor">.example</span><span class="hljs-preprocessor">.myproject</span><span class="hljs-comment">;</span>

import org<span class="hljs-preprocessor">.springframework</span><span class="hljs-preprocessor">.boot</span><span class="hljs-preprocessor">.SpringApplication</span><span class="hljs-comment">;</span>
import org<span class="hljs-preprocessor">.springframework</span><span class="hljs-preprocessor">.boot</span><span class="hljs-preprocessor">.autoconfigure</span><span class="hljs-preprocessor">.EnableAutoConfiguration</span><span class="hljs-comment">;</span>
import org<span class="hljs-preprocessor">.springframework</span><span class="hljs-preprocessor">.context</span><span class="hljs-preprocessor">.annotation</span><span class="hljs-preprocessor">.ComponentScan</span><span class="hljs-comment">;</span>
import org<span class="hljs-preprocessor">.springframework</span><span class="hljs-preprocessor">.context</span><span class="hljs-preprocessor">.annotation</span><span class="hljs-preprocessor">.Configuration</span><span class="hljs-comment">;</span>

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {

    public static void main(String[] args) {
        SpringApplication<span class="hljs-preprocessor">.run</span>(Application<span class="hljs-preprocessor">.class</span>, args)<span class="hljs-comment">;</span>
    }

}</code>

入口类Application带main方法,我们直接运行main方法就能启动Spring Boot项目了,这样极大程序地方便了我们调试程序和项目。

Application类说明自己是Spring Boot的入口类,那么需要加入@Configuration注解。

@EnableAutoConfiguration习惯放在主方法类Application上,当项目运行时,Spring容器去自动查找带特定注解的类,如:带@Entity、@Service等类。

@ComponentScan如果不带basePackage 属性的话,它会自动扫描以入口类所在的包为父节点下所有子包下的类。这也是Spring Boot会提议我们把Application类放于根包路径下。

如果我们的项目和Spring Boot建议的代码结构一样,Application类放在根包路径下。那么我们可以使用@SpringBootApplication来代替上面三个注解。

<code class="hljs java has-numbering"><span class="hljs-keyword">package</span> com.example.myproject;

<span class="hljs-keyword">import</span> org.springframework.boot.SpringApplication;
<span class="hljs-keyword">import</span> org.springframework.boot.autoconfigure.SpringBootApplication;

<span class="hljs-annotation">@SpringBootApplication</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Application</span> {</span>

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span>(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}</code>

3、Spring Boot的Web项目实现

3.1 Application类支持Web应用

<code class="hljs java has-numbering"><span class="hljs-annotation">@SpringBootApplication</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Application</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">SpringBootServletInitializer</span> {</span>

    <span class="hljs-annotation">@Override</span>
    <span class="hljs-keyword">protected</span> SpringApplicationBuilder <span class="hljs-title">configure</span>(SpringApplicationBuilder application) {
        <span class="hljs-keyword">return</span> application.sources(Application.class);
    }

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span>(String[] args) <span class="hljs-keyword">throws</span> Exception {
        SpringApplication.run(Application.class, args);
    }

}</code>

入口类Application继承SpringBootServletInitializer并重写configure方法。运行主方法后,会将我们的web项目打包成war,并默认启动一个端口为8080的tomcat容器来运行我们的Web项目。

3.2 其它服务器软件支持

如果我们不想使用tomcat,而是其它的服务器软件,如Jetty。你需要移除tomcat的依赖,并加入Jetty的依赖。

<code class="hljs xml has-numbering"><span class="hljs-tag"><<span class="hljs-title">dependency</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">groupId</span>></span>org.springframework.boot<span class="hljs-tag"></<span class="hljs-title">groupId</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">artifactId</span>></span>spring-boot-starter-web<span class="hljs-tag"></<span class="hljs-title">artifactId</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">exclusions</span>></span>
        <span class="hljs-tag"><<span class="hljs-title">exclusion</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">groupId</span>></span>org.springframework.boot<span class="hljs-tag"></<span class="hljs-title">groupId</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">artifactId</span>></span>spring-boot-starter-tomcat<span class="hljs-tag"></<span class="hljs-title">artifactId</span>></span>
        <span class="hljs-tag"></<span class="hljs-title">exclusion</span>></span>
    <span class="hljs-tag"></<span class="hljs-title">exclusions</span>></span>
<span class="hljs-tag"></<span class="hljs-title">dependency</span>></span>
<span class="hljs-tag"><<span class="hljs-title">dependency</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">groupId</span>></span>org.springframework.boot<span class="hljs-tag"></<span class="hljs-title">groupId</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">artifactId</span>></span>spring-boot-starter-jetty<span class="hljs-tag"></<span class="hljs-title">artifactId</span>></span>
<span class="hljs-tag"></<span class="hljs-title">dependency</span>></span></code>

3.3 服务器端口更改

增加application.yml配置文件。

这里写图片描述

<code class="hljs vala has-numbering"><span class="hljs-preprocessor"># Server settings  </span>
server:  
    port: <span class="hljs-number">80</span>
    address: <span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span></code>

这里需要注意,yml配置文件是的值属性前面必须有一个空格,如果没有空格,Spring的解析器会忽略此配置项。

3.4 Controller

Spring支持Spring MVC的Controler的使用方式。
请参考:http://blog.csdn.net/p_3er/article/category/2868979

Spring Boot应用中@RestController的Controller带有默认基于Jackson2的对象转JSON功能。如:

<code class="hljs java has-numbering"><span class="hljs-annotation">@RestController</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyController</span> {</span>

    <span class="hljs-annotation">@RequestMapping</span>(<span class="hljs-string">"/thing"</span>)
    <span class="hljs-keyword">public</span> MyThing <span class="hljs-title">thing</span>() {
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> MyThing();
    }

}</code>

3.5 配置编码及JSP支持

<code class="hljs avrasm has-numbering"><span class="hljs-preprocessor"># SPRING PROFILES  </span>
<span class="hljs-label">spring:</span>         
    <span class="hljs-preprocessor"># HTTP ENCODING  </span>
    http:  
        encoding<span class="hljs-preprocessor">.charset</span>: UTF-<span class="hljs-number">8</span>  
        encoding<span class="hljs-preprocessor">.enable</span>: true  
        encoding<span class="hljs-preprocessor">.force</span>: true  
    mvc:
        view<span class="hljs-preprocessor">.prefix</span>: /WEB-INF/jsp/
        view<span class="hljs-preprocessor">.suffix</span>: <span class="hljs-preprocessor">.jsp</span></code>

加入以上配置后,还需要引入用于编译jsp的jasper包依赖。

<code class="hljs xml has-numbering">        <span class="hljs-tag"><<span class="hljs-title">dependency</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">groupId</span>></span>org.apache.tomcat.embed<span class="hljs-tag"></<span class="hljs-title">groupId</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">artifactId</span>></span>tomcat-embed-jasper<span class="hljs-tag"></<span class="hljs-title">artifactId</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">scope</span>></span>provided<span class="hljs-tag"></<span class="hljs-title">scope</span>></span>
        <span class="hljs-tag"></<span class="hljs-title">dependency</span>></span></code>

加入JSP支持配置后,下面a方法和b方法者是跳转到/WEB-INF/jsp/regiester.jsp页面。

<code class="hljs java has-numbering"><span class="hljs-keyword">package</span> xyz.letus.boot.controller;

<span class="hljs-keyword">import</span> java.util.Map;

<span class="hljs-keyword">import</span> javax.servlet.http.HttpServletRequest;

<span class="hljs-keyword">import</span> org.springframework.stereotype.Controller;
<span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;
<span class="hljs-keyword">import</span> org.springframework.web.servlet.ModelAndView;

<span class="hljs-annotation">@Controller</span>
<span class="hljs-annotation">@RequestMapping</span>(<span class="hljs-string">"/page"</span>)
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PageController</span> {</span>

    <span class="hljs-annotation">@RequestMapping</span>(<span class="hljs-string">"/a"</span>)
    <span class="hljs-keyword">public</span> String <span class="hljs-title">b</span>(Map<String, Object> model){
        model.put(<span class="hljs-string">"msg"</span>, <span class="hljs-string">"张三"</span>);     
        <span class="hljs-keyword">return</span> <span class="hljs-string">"regiester"</span>;
    }

    <span class="hljs-annotation">@RequestMapping</span>(<span class="hljs-string">"/b"</span>)
    <span class="hljs-keyword">public</span> ModelAndView <span class="hljs-title">b</span>(HttpServletRequest request){
        ModelAndView view = <span class="hljs-keyword">new</span> ModelAndView();
        view.setViewName(<span class="hljs-string">"regiester"</span>);

        request.setAttribute(<span class="hljs-string">"msg"</span>, <span class="hljs-string">"Davie"</span>);
        <span class="hljs-keyword">return</span> view;
    }
}
</code>

3.6 Spring Boot应用实现热部署

在插件管理中加入springloaded依赖就可以。

<code class="hljs xml has-numbering"> <span class="hljs-comment"><!-- Package as an executable jar --></span>
    <span class="hljs-tag"><<span class="hljs-title">build</span>></span>
        <span class="hljs-tag"><<span class="hljs-title">plugins</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">plugin</span>></span>
                <span class="hljs-tag"><<span class="hljs-title">groupId</span>></span>org.springframework.boot<span class="hljs-tag"></<span class="hljs-title">groupId</span>></span>
                <span class="hljs-tag"><<span class="hljs-title">artifactId</span>></span>spring-boot-maven-plugin<span class="hljs-tag"></<span class="hljs-title">artifactId</span>></span>
                <span class="hljs-tag"><<span class="hljs-title">dependencies</span>></span>
                <span class="hljs-comment"><!-- spring热部署--></span>
                <span class="hljs-tag"><<span class="hljs-title">dependency</span>></span>
                    <span class="hljs-tag"><<span class="hljs-title">groupId</span>></span>org.springframework<span class="hljs-tag"></<span class="hljs-title">groupId</span>></span>
                    <span class="hljs-tag"><<span class="hljs-title">artifactId</span>></span>springloaded<span class="hljs-tag"></<span class="hljs-title">artifactId</span>></span>
                <span class="hljs-tag"></<span class="hljs-title">dependency</span>></span>
            <span class="hljs-tag"></<span class="hljs-title">dependencies</span>></span>
            <span class="hljs-tag"></<span class="hljs-title">plugin</span>></span>
        <span class="hljs-tag"></<span class="hljs-title">plugins</span>></span>
    <span class="hljs-tag"></<span class="hljs-title">build</span>></span></code>

当系统通过 mvn spring-boot:run启动或者 右键application debug 启动java文件时,系统会监视classes文件,当有classes文件被改动时,系统会重新加载类文件,不用重启启动服务。

注:使用application run(非debug模式下),热部署功能会失效。

4、Spring Boot集成MyBatis

4.1 加入基础依赖

MyBatis:

<code class="hljs xml has-numbering"><span class="hljs-comment"><!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --></span>
        <span class="hljs-tag"><<span class="hljs-title">dependency</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">groupId</span>></span>org.mybatis<span class="hljs-tag"></<span class="hljs-title">groupId</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">artifactId</span>></span>mybatis<span class="hljs-tag"></<span class="hljs-title">artifactId</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">version</span>></span>3.4.0<span class="hljs-tag"></<span class="hljs-title">version</span>></span>
        <span class="hljs-tag"></<span class="hljs-title">dependency</span>></span>
</code>

mybatis-spring-boot-starter:

<code class="hljs xml has-numbering"><span class="hljs-comment"><!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --></span>
        <span class="hljs-tag"><<span class="hljs-title">dependency</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">groupId</span>></span>org.mybatis.spring.boot<span class="hljs-tag"></<span class="hljs-title">groupId</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">artifactId</span>></span>mybatis-spring-boot-starter<span class="hljs-tag"></<span class="hljs-title">artifactId</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">version</span>></span>1.1.1<span class="hljs-tag"></<span class="hljs-title">version</span>></span>
        <span class="hljs-tag"></<span class="hljs-title">dependency</span>></span></code>

MySQL:

<code class="hljs xml has-numbering"><span class="hljs-comment"><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --></span>
        <span class="hljs-tag"><<span class="hljs-title">dependency</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">groupId</span>></span>mysql<span class="hljs-tag"></<span class="hljs-title">groupId</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">artifactId</span>></span>mysql-connector-java<span class="hljs-tag"></<span class="hljs-title">artifactId</span>></span>
        <span class="hljs-tag"></<span class="hljs-title">dependency</span>></span></code>

4.2 数据库配置

<code class="hljs avrasm has-numbering"><span class="hljs-preprocessor"># SPRING PROFILES  </span>
<span class="hljs-label">spring:</span>         
    <span class="hljs-preprocessor"># DATASOURCE  </span>
    datasource:  
        driverClass: <span class="hljs-keyword">com</span><span class="hljs-preprocessor">.mysql</span><span class="hljs-preprocessor">.jdbc</span><span class="hljs-preprocessor">.Driver</span>  
        url: jdbc:mysql://<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">3306</span>/hire?useUnicode=true&characterEncoding=utf-<span class="hljs-number">8</span>  
        username: root  
        password: test  </code>

4.3 引入通用Mapper

  • 引入依赖
<code class="hljs xml has-numbering">        <span class="hljs-tag"><<span class="hljs-title">dependency</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">groupId</span>></span>tk.mybatis<span class="hljs-tag"></<span class="hljs-title">groupId</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">artifactId</span>></span>mapper<span class="hljs-tag"></<span class="hljs-title">artifactId</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">version</span>></span>3.3.7<span class="hljs-tag"></<span class="hljs-title">version</span>></span>
        <span class="hljs-tag"></<span class="hljs-title">dependency</span>></span></code>
  • 配置通用Mapper
<code class="hljs avrasm has-numbering">package xyz<span class="hljs-preprocessor">.ibenben</span><span class="hljs-preprocessor">.zhongdian</span><span class="hljs-preprocessor">.common</span><span class="hljs-preprocessor">.configure</span><span class="hljs-comment">;</span>

import org<span class="hljs-preprocessor">.springframework</span><span class="hljs-preprocessor">.boot</span><span class="hljs-preprocessor">.autoconfigure</span><span class="hljs-preprocessor">.AutoConfigureAfter</span><span class="hljs-comment">;</span>
import org<span class="hljs-preprocessor">.springframework</span><span class="hljs-preprocessor">.context</span><span class="hljs-preprocessor">.annotation</span><span class="hljs-preprocessor">.Bean</span><span class="hljs-comment">;</span>
import org<span class="hljs-preprocessor">.springframework</span><span class="hljs-preprocessor">.context</span><span class="hljs-preprocessor">.annotation</span><span class="hljs-preprocessor">.Configuration</span><span class="hljs-comment">;</span>
import tk<span class="hljs-preprocessor">.mybatis</span><span class="hljs-preprocessor">.spring</span><span class="hljs-preprocessor">.mapper</span><span class="hljs-preprocessor">.MapperScannerConfigurer</span><span class="hljs-comment">;</span>

import java<span class="hljs-preprocessor">.util</span><span class="hljs-preprocessor">.Properties</span><span class="hljs-comment">;</span>


@Configuration
public class MyBatisMapperScannerConfig {

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer()<span class="hljs-comment">;</span>
        mapperScannerConfigurer<span class="hljs-preprocessor">.setSqlSessionFactoryBeanName</span>(<span class="hljs-string">"sqlSessionFactory"</span>)<span class="hljs-comment">;</span>
        mapperScannerConfigurer<span class="hljs-preprocessor">.setBasePackage</span>(<span class="hljs-string">"xyz.ibenben.zhongdian.*.dao"</span>)<span class="hljs-comment">;</span>
        Properties properties = new Properties()<span class="hljs-comment">;</span>
        properties<span class="hljs-preprocessor">.setProperty</span>(<span class="hljs-string">"mappers"</span>, <span class="hljs-string">"xyz.ibenben.zhongdian.common.BaseDao"</span>)<span class="hljs-comment">;</span>
        properties<span class="hljs-preprocessor">.setProperty</span>(<span class="hljs-string">"notEmpty"</span>, <span class="hljs-string">"false"</span>)<span class="hljs-comment">;</span>
        properties<span class="hljs-preprocessor">.setProperty</span>(<span class="hljs-string">"IDENTITY"</span>, <span class="hljs-string">"MYSQL"</span>)<span class="hljs-comment">;</span>
        mapperScannerConfigurer<span class="hljs-preprocessor">.setProperties</span>(properties)<span class="hljs-comment">;</span>
        return mapperScannerConfigurer<span class="hljs-comment">;</span>
    }

}
</code>

其实MyBatisMapperScannerConfig 是一个MyBatis扫描Mapper接口扫描。

MapperScannerConfigurer根据指定的创建接口或注解创建映射器。我们这里映射了xyz.ibenben.zhongdian.*.dao包下的接口。

使用MapperScannerConfigurer,没有必要去指定SqlSessionFactory或SqlSessionTemplate,因为MapperScannerConfigurer将会创建MapperFactoryBean,之后自动装配。但是,如果你使用了一个以上的DataSource(因此,也是多个的SqlSessionFactory),那么自动装配可能会失效。这种情况下,你可以使用sqlSessionFactory或sqlSessionTemplate属性来设置正确的工厂/模板。

注意的是网络上有些文章中在MapperScannerConfigurer之前还配置了 MyBatisConfig,因为MapperScannerConfigurer会创建MapperFactoryBean,所以我的项目中没有再配置MyBatisConfig。经使用没有出现任何问题。

4.4 通用Mapper的使用(Dao层)

BaseDao:

<code class="hljs axapta has-numbering">package xyz.ibenben.zhongdian.<span class="hljs-keyword">common</span>;

import tk.mybatis.mapper.<span class="hljs-keyword">common</span>.Mapper;
import tk.mybatis.mapper.<span class="hljs-keyword">common</span>.MySqlMapper;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">BaseDao</span><<span class="hljs-title">T</span>> <span class="hljs-inheritance"><span class="hljs-keyword">extends</span></span> <span class="hljs-title">Mapper</span><<span class="hljs-title">T</span>>,<span class="hljs-title">MySqlMapper</span><<span class="hljs-title">T</span>>{</span>

}
</code>

*Dao:

<code class="hljs avrasm has-numbering">package xyz<span class="hljs-preprocessor">.ibenben</span><span class="hljs-preprocessor">.zhongdian</span><span class="hljs-preprocessor">.system</span><span class="hljs-preprocessor">.dao</span><span class="hljs-comment">;</span>


import java<span class="hljs-preprocessor">.util</span><span class="hljs-preprocessor">.List</span><span class="hljs-comment">;</span>

import org<span class="hljs-preprocessor">.apache</span><span class="hljs-preprocessor">.ibatis</span><span class="hljs-preprocessor">.annotations</span><span class="hljs-preprocessor">.Select</span><span class="hljs-comment">;</span>

import xyz<span class="hljs-preprocessor">.ibenben</span><span class="hljs-preprocessor">.zhongdian</span><span class="hljs-preprocessor">.common</span><span class="hljs-preprocessor">.BaseDao</span><span class="hljs-comment">;</span>
import xyz<span class="hljs-preprocessor">.ibenben</span><span class="hljs-preprocessor">.zhongdian</span><span class="hljs-preprocessor">.system</span><span class="hljs-preprocessor">.entity</span><span class="hljs-preprocessor">.User</span><span class="hljs-comment">;</span>

public interface UserDao extends BaseDao<User>{

    @Select(<span class="hljs-string">"select * from user where state = #{state}"</span>)
    public List<User> selectByState(Integer state)<span class="hljs-comment">;</span>
}

</code>

MyBatis的Dao与其它的ORM框架不一样的是,MyBatis的Dao其实就是Mapper,是一个接口,是通过MapperScannerConfigurer扫描后生成实现的,我们不需要再写Dao接口的实现。

4.5 业务处理及事务(Service层)

<code class="hljs java has-numbering"><span class="hljs-keyword">package</span> xyz.ibenben.zhongdian.system.service;

<span class="hljs-keyword">import</span> xyz.ibenben.zhongdian.system.entity.User;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">UserService</span> {</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">saveUser</span>(User user);
}
</code>
<code class="hljs avrasm has-numbering">package xyz<span class="hljs-preprocessor">.ibenben</span><span class="hljs-preprocessor">.zhongdian</span><span class="hljs-preprocessor">.system</span><span class="hljs-preprocessor">.service</span><span class="hljs-preprocessor">.impl</span><span class="hljs-comment">;</span>

import java<span class="hljs-preprocessor">.util</span><span class="hljs-preprocessor">.List</span><span class="hljs-comment">;</span>

import org<span class="hljs-preprocessor">.apache</span><span class="hljs-preprocessor">.ibatis</span><span class="hljs-preprocessor">.session</span><span class="hljs-preprocessor">.RowBounds</span><span class="hljs-comment">;</span>
import org<span class="hljs-preprocessor">.springframework</span><span class="hljs-preprocessor">.beans</span><span class="hljs-preprocessor">.factory</span><span class="hljs-preprocessor">.annotation</span><span class="hljs-preprocessor">.Autowired</span><span class="hljs-comment">;</span>
import org<span class="hljs-preprocessor">.springframework</span><span class="hljs-preprocessor">.stereotype</span><span class="hljs-preprocessor">.Service</span><span class="hljs-comment">;</span>
import org<span class="hljs-preprocessor">.springframework</span><span class="hljs-preprocessor">.transaction</span><span class="hljs-preprocessor">.annotation</span><span class="hljs-preprocessor">.Transactional</span><span class="hljs-comment">;</span>

import xyz<span class="hljs-preprocessor">.ibenben</span><span class="hljs-preprocessor">.zhongdian</span><span class="hljs-preprocessor">.system</span><span class="hljs-preprocessor">.dao</span><span class="hljs-preprocessor">.TaskDao</span><span class="hljs-comment">;</span>
import xyz<span class="hljs-preprocessor">.ibenben</span><span class="hljs-preprocessor">.zhongdian</span><span class="hljs-preprocessor">.system</span><span class="hljs-preprocessor">.dao</span><span class="hljs-preprocessor">.UserDao</span><span class="hljs-comment">;</span>
import xyz<span class="hljs-preprocessor">.ibenben</span><span class="hljs-preprocessor">.zhongdian</span><span class="hljs-preprocessor">.system</span><span class="hljs-preprocessor">.entity</span><span class="hljs-preprocessor">.Task</span><span class="hljs-comment">;</span>
import xyz<span class="hljs-preprocessor">.ibenben</span><span class="hljs-preprocessor">.zhongdian</span><span class="hljs-preprocessor">.system</span><span class="hljs-preprocessor">.entity</span><span class="hljs-preprocessor">.User</span><span class="hljs-comment">;</span>
import xyz<span class="hljs-preprocessor">.ibenben</span><span class="hljs-preprocessor">.zhongdian</span><span class="hljs-preprocessor">.system</span><span class="hljs-preprocessor">.service</span><span class="hljs-preprocessor">.UserService</span><span class="hljs-comment">;</span>


@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserDao userDao<span class="hljs-comment">;</span>
    @Autowired
    private TaskDao taskDao<span class="hljs-comment">;</span>

    @Transactional
    public void saveUser(User user){
        user = userDao<span class="hljs-preprocessor">.selectByPrimaryKey</span>(<span class="hljs-number">1</span>)<span class="hljs-comment">;</span>
        user<span class="hljs-preprocessor">.setUsername</span>(<span class="hljs-string">"5566"</span>)<span class="hljs-comment">;</span>
        userDao<span class="hljs-preprocessor">.updateByPrimaryKey</span>(user)<span class="hljs-comment">;</span>
//      int i = <span class="hljs-number">10</span>/<span class="hljs-number">0</span><span class="hljs-comment">;</span>
        Task task = new Task()<span class="hljs-comment">;</span>
        task<span class="hljs-preprocessor">.setName</span>(<span class="hljs-string">"task 100"</span>)<span class="hljs-comment">;</span>
        task<span class="hljs-preprocessor">.setDescript</span>(<span class="hljs-string">"task100 descriot"</span>)<span class="hljs-comment">;</span>
        task<span class="hljs-preprocessor">.setState</span>(<span class="hljs-number">1</span>)<span class="hljs-comment">;</span>
        taskDao<span class="hljs-preprocessor">.insert</span>(task)<span class="hljs-comment">;</span>

        Task temp = new Task()<span class="hljs-comment">;</span>
        task<span class="hljs-preprocessor">.setState</span>(<span class="hljs-number">1</span>)<span class="hljs-comment">;</span>
        List<Task> list = taskDao<span class="hljs-preprocessor">.selectByRowBounds</span>(temp, new RowBounds(<span class="hljs-number">2</span>, <span class="hljs-number">12</span>))<span class="hljs-comment">;</span>
        System<span class="hljs-preprocessor">.out</span><span class="hljs-preprocessor">.println</span>(list<span class="hljs-preprocessor">.size</span>())<span class="hljs-comment">;</span>
        for(Task t : list){
            System<span class="hljs-preprocessor">.out</span><span class="hljs-preprocessor">.println</span>(t<span class="hljs-preprocessor">.getName</span>())<span class="hljs-comment">;</span>
        }

        List<User> users = userDao<span class="hljs-preprocessor">.selectByState</span>(<span class="hljs-number">1</span>)<span class="hljs-comment">;</span>
        for(User u : users){
            System<span class="hljs-preprocessor">.out</span><span class="hljs-preprocessor">.println</span>(u<span class="hljs-preprocessor">.getUsername</span>())<span class="hljs-comment">;</span>
        }
    }
}
</code>

Spring Boot集成MyBatis后,实现事物管理的方法很简单,只需要在业务方法前面加上@Transactional注解就可以了。

上面方法中用了一个被除数为0的表达式来进行测试事务。

5、完整源码下载

https://github.com/ibenben/spring-boot-mybatis-demo

相关文章推荐

spring-boot-mybatis-demo

【约定优于配置】spring-boot建议orm采用JPA,因为jpa符合spring-boot简化配置理念。但是最近在使用jpa的过程中各种不爽,特别是多表联合查询的时候,也可能是我对jpa还没深入...

【系统学习SpringBoot】springBoot整合Mybatis

学了几天spring boot ,感觉spring boot整合了spring mvc 和spring的有点,,屏蔽了大量的配置文件(虽然spring可以使用注解,但是spring boot让配置更简...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Spring Boot 集成MyBatis

Spring Boot 集成MyBatis在配置MyBatis前,我们先配置一个druid数据源。Spring Boot 集成druiddruid有很多个配置选项,使用Spring Boot 的Con...
  • isea533
  • isea533
  • 2015年12月27日 15:29
  • 232728

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Spring boot Mybatis 整合(完整版)

本项目使用的环境: 1.开发工具:Intellij IDEA 2017.3 2.jdk:1.7.0_79 3.maven:3.3.9步骤: 1.创建一个springboot项目: 2.创...

SpringBoot+Mybatis框架Demo

1、使用Intellij IDEA 2016.3 新建一个SpringBoot项目 这样就创建了一个简单的SpringBoot项目了。 2、添加jar包 UTF-8 UTF-8 ...

spring boot 整合mybatis 通过官方mybatis-spring-boot-starter

pom.xml

MyBatis入门demo

最近要学习使用MyBatis,参照网上的资料和官方的教程,根据自己的理解,记录一下。 MyBatisJar包下载地址:http://download.csdn.net/detail/joling...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Spring Boot入门及集成MyBatis开发Web项目
举报原因:
原因补充:

(最多只允许输入30个字)