第一步:新建Spring Initializr项目
(1)选择Spring Initializr
(2)选择SDK,点击【Next】
(3)填写项目信息,点击【Next】
(4)勾选Web依赖
(5)勾选SQL依赖,点击【Next】
(6)选择项目存放本机位置,点击【Finish】
(7)创建完之后,【Import Changes】等待配置。默认项目结构如下:
第二步:代码测试
(1)在【main/java/com/example/springboot】包下新建一个【HelloController】
源代码:
package com.example.springboot;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 测试控制器
*
* @author:@ChuaiWi
* @date:2020-04-22
*/
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "Hello SpringBoot!";
}
}
(2)在【main/resources/application.properties】中配置端口:8080
(3)在【main/java/com/example/springboot】包下的【SpringbootApplication】中补充启动类头部声明
否则会报Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured.
的错误。翻译过来就是:未能配置数据源:未指定“url”属性,无法配置嵌入的数据源。
当然这个有其它解决方案,以后遇到再说……
第三步:运行程序
(1)运行【main/java/com/example/springboot】包下的【SpringbootApplication】启动类
(2)查看控制台
(3)输入网址【http://localhost:8080/hello】,查看页面显示
初步搭建SpringBoot,运行完成!