61 lines
2.0 KiB
Markdown
Raw Normal View History

2018-12-25 18:40:48 +08:00
---
2019-02-21 23:34:11 +08:00
id: "2018-08-13-10-38"
2019-02-23 20:44:31 +08:00
date: "2018/08/13 10:38:00"
2019-02-21 23:34:11 +08:00
title: "springboot搭建"
tags: ["java", "spring","springboot","idea"]
categories:
- "java"
- "spring boot学习"
2018-12-25 18:40:48 +08:00
---
2018-11-25 12:37:20 +08:00
2018-12-25 18:40:48 +08:00
  前面的博客有说到 spring boot 搭建见另一篇博文,其实那篇博文还没写,现在来填个坑。我们使用 spring initializr 来构建idea 和 eclipse 都支持这种方式,构建过程类似,这里以 idea 为例,详细记录构建过程。
### 1.选择 spring initializr
2018-11-25 12:37:20 +08:00
2019-01-07 10:31:40 +08:00
![选择spring initializr](https://raw.githubusercontent.com/FleyX/files/master/blogImg/springboot%E6%90%AD%E5%BB%BA/20190107100435.png)
2018-11-25 12:37:20 +08:00
next
#### 2.设置参数
2019-01-07 10:31:40 +08:00
![设置参数](https://raw.githubusercontent.com/FleyX/files/master/blogImg/springboot%E6%90%AD%E5%BB%BA/20190107100509.png)
2018-11-25 12:37:20 +08:00
next
#### 3.选择依赖
2018-12-25 18:40:48 +08:00
  在这里选择 spring boot 版本和 web 依赖(忽略 sql 的依赖,如有需要[点击这里](f),单独将 mybatis 的整合),后面也可手动编辑 pom 文件修改增加删除依赖
2018-11-25 12:37:20 +08:00
2019-01-07 10:31:40 +08:00
![依赖选择](https://raw.githubusercontent.com/FleyX/files/master/blogImg/springboot%E6%90%AD%E5%BB%BA/20190107100609.png)
2018-11-25 12:37:20 +08:00
2018-12-25 18:40:48 +08:00
这里我们选择 web 搭建一个简单的 REST 风格 demo。然后 next。
2018-11-25 12:37:20 +08:00
2019-02-23 16:59:40 +08:00
<!-- more -->
2018-12-25 18:40:48 +08:00
#### 4.设置项目存放地址
2018-11-25 12:37:20 +08:00
2019-01-07 10:31:40 +08:00
![设置项目存放地址](https://raw.githubusercontent.com/FleyX/files/master/blogImg/springboot%E6%90%AD%E5%BB%BA/20190107100653.png)
2018-11-25 12:37:20 +08:00
2018-12-25 18:40:48 +08:00
这样就成功构建了一个 springboot 项目。
2018-11-25 12:37:20 +08:00
#### 5.测试
2018-12-25 18:40:48 +08:00
&emsp;&emsp;现在新建一个 controller 包,包下新建一个 HelloController,创建之后项目目录结构如下:
2018-11-25 12:37:20 +08:00
2019-01-07 10:31:40 +08:00
![项目目录结构](https://raw.githubusercontent.com/FleyX/files/master/blogImg/springboot%E6%90%AD%E5%BB%BA/20190107100803.png)
2018-11-25 12:37:20 +08:00
2018-12-25 18:40:48 +08:00
HelloController 代码如下:
2018-11-25 12:37:20 +08:00
```java
@RestController
@RequestMapping("/home")
public class HelloController{
@GetMapping("/hello")
public String sayHello(){
return "hello";
}
}
```
2019-01-07 10:31:40 +08:00
然后运行项目,访问 [localhost:8080/home/hello](localhost:8080/home/hello) 即可看到 hello 字符串。