SpringBoot-环境搭建

标签(空格分隔): java,SpringBoot

  1. 创建Maven工程

spring-入门搭建-1.png

  1. 编写pom文件

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.6</version>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.12</version>
    </dependency>
</dependencies>
  1. 编写SpringBoot引导类

package com.itheima;

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

@SpringBootApplication
public class MySpringBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(MySpringBootApplication.class, args);
    }
}
  1. 编写Controller

package com.itheima.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.sql.DataSource;

@Controller
public class HelloController {


    @Autowired
    private DataSource dataSource;

    @GetMapping("/hello")
    @ResponseBody
    public String hello() {
        return "hello spring boot";
    }
}
  1. 运行测试

springboot-运行.png

springboot-控制台.png

springboot-测试.png

  1. 注意事项

  1. JDK版本配置
  2. Maven仓库配置

版权声明:本文为yanweifeng原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/yanweifeng/p/13377195.html