基本的Spring Boot项目

先按照 Spring Boot 官方文档中的介绍,使用 Maven 搭建一个基本的 Spring Boot 项目。

创建一个空的 Maven 项目,然后编辑 pom.xml 文件如下。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>study-mybatis2</artifactId>
        <groupId>com.zccoder</groupId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ch10-boot</artifactId>

    <properties>
        <spring.boot.version>1.5.20.RELEASE</spring.boot.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.2.0</version>
        </dependency>

        <dependency>
            <groupId>com.zccoder</groupId>
            <artifactId>ch02-xml</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

在 tk.mybatis.springboot 包下面新建 Application 类,代码如下:

package cn.liaozh.mybatis2.ch10.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(BootApplication.class, args);
    }
}

写到这里的时候,实际上 Spring Boot 就已经可以启动了,但是我们没有提供任何有效的请求,如果直接访问就会提示请求出错,所以,接下来要在 tk.mybatis.springboot.controller 包下面新建 IndexController 类。

package cn.liaozh.mybatis2.ch10.boot.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {

    @RequestMapping("/")
    String home(){
        return "Hello World";
    }

}

刚刚配置了 / 的映射,有了这个基本的 Controller 后,以 Java 应用的方式启动 Application 类,默认情况下的端口为 8080,访问 http://localhost:8080 就可以看到页面显示如下内容。

Hello World!

到这一步时可以看到,虽然没有写很多代码,但是最基础的 HTTP 服务已经实现了。

在前面的依赖配置中,我们还添加了 spring-boot-starter-jdbc 和 MySQL 驱动。在继续下一节前,先把数据源配置好,在 src/main/resources 目录下新建 application.properties,写入如下内容。

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis2?characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root

通过上面的属性配置数据库的连接信息后,Spring Boot 就可以自动配置数据源了。准备好数据源后,接下来开始集成 MyBatis。