SpringBoot之入门教程-SpringBoot项目搭建

SpringBoot大大的简化了Spring的配置,把Spring从配置炼狱中解救出来了,以前天天配置Spring和Mybatis,Springmvc,Hibernate等整合在一起,感觉用起来还是挺费劲的,没有springboot这样简洁,下面我就简单的演示一下Springboot的使用。简单来说步骤:1、创建一个普通的maven项目,不是webapp,而是jar的这种方式,2、配置pom.xml,3,创建

官网地址

1
https://projects.spring.io/spring-boot/

源码地址

1
https://gitee.com/yellowcong/springboot-demo/tree/master/springboot-demo1

SpringBoot项目搭建

1、创建一个普通的java项目,不是webapp啊

这里写图片描述

2、配置pom.xml

从官网上看到,版本还挺多的,我们就先用1.5.10.RELEASE稳定版
这里写图片描述

下面是配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    <modelversion>4.0.0</modelversion>
 
    <groupid>yellowcong</groupid>
    cas-config</artifactid>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>cas-config</name>
 
    <url>https://maven.apache.org</url>
    <!-- 引用父类依赖 -->
    <parent>
        <groupid>org.springframework.boot</groupid>
        spring-boot-starter-parent</artifactid>
        <version>1.5.10.RELEASE</version>
    </parent>
 
    <properties>
        <project.build.sourceencoding>UTF-8</project.build.sourceencoding>
    </properties>
 
    <dependencies>
        <!-- 添加spring-web的依赖,直接就可以使用springmvc了 -->
        <dependency>
            <groupid>org.springframework.boot</groupid>
            spring-boot-starter-web</artifactid>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <!-- 添加spring的插件, 就可以直接通过 mvn spring-boot:run 运行了 -->
            <plugin>
                <groupid>org.springframework.boot</groupid>
                spring-boot-maven-plugin</artifactid>
            </plugin>
        </plugins>
    </build>
</project>
 
 
</code>

3、创建一个控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<code>package com.yellowcong.controller;
 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
@Controller
@EnableAutoConfiguration
@RequestMapping("/sample")
public class SampleController {
 
    @RequestMapping("/home")
    @ResponseBody
    String home() {
        return "Hello World!";
    }
}
</code>

4、配置启动器

细心的同学肯定发现了,我的controller和启动器不是在一个类,我为啥这么做呢,这样,在同一个包下面的所有controller都可以访问了

大家从下图可以看到,我有两个控制器,一个启动类,我们可以通过一个启动器,来控制房屋呢这两个类
这里写图片描述

1
2
3
4
5
6
7
8
9
10
11
12
13
<code>package com.yellowcong.controller;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
 
@SpringBootApplication
public class ConfigMain {
 
    public static void main(String[] args) {
        SpringApplication.run(ConfigMain.class, args);
    }
}</code>

5、启动运行

方式1

直接运行类,像普通程序一样
这里写图片描述

方式2

通过springboot的插件,执行mvn spring-boot:run 命令,来运行springboot程序
这里写图片描述

设定spring-boot:run 命令
这里写图片描述

启动结果
这里写图片描述

访问服务

两个服务正常访问,说明教程就结束了。
这里写图片描述

posted @ 2019-10-21 13:26  程序员宝典  阅读(427)  评论(0编辑  收藏  举报