一、下載安裝JDK
太簡(jiǎn)單不想說(shuō)
二详拙、安裝編譯器
Java主流IDE有eplice和IDEA,我用的是IDEA蔓同,安裝方法太簡(jiǎn)單饶辙,不想說(shuō)
三、第一個(gè)SpringBoot程序hello
1斑粱、終端輸入以下指令弃揽,查看安裝的Java版本
wjc@wangjunhaodeMBP ~ % java -version
java version "14.0.1" 2020-04-14
可以看到我的Java版本為最新的14.0.1,如果沒(méi)有看到自己的Java版本號(hào),重復(fù)一操作
image.png
選擇Spring initiakizr,點(diǎn)擊next即可初始化一個(gè)SpringBoot項(xiàng)目
image.png
設(shè)置項(xiàng)目名稱為hello涌矢,繼續(xù)next
image.png
這里選擇Web-Spring Web掖举,繼續(xù)next
image.png
這里可以選擇項(xiàng)目目錄,然后finish
image.png
這樣一個(gè)SpringBoot項(xiàng)目就創(chuàng)建成功了
image.png
三娜庇、第一個(gè)接口塔次,請(qǐng)求返回hello world!
1名秀、創(chuàng)建一個(gè)Controller類(lèi)HelloController
image.png
2励负、在HelloController中實(shí)現(xiàn)返回hello world!字符串的接口
image.png
3、點(diǎn)擊這個(gè)運(yùn)行按鈕泰偿,啟動(dòng)程序熄守,注意服務(wù)啟動(dòng)的端口為8080
image.png
4蜈垮、然后在游覽器中輸入http://localhost:8080/hello耗跛,即可看到如下效果
image.png
四、通過(guò)接口操作數(shù)據(jù)庫(kù)之增刪改查
上一步我們成功返回了一個(gè)我們寫(xiě)死的字符串hello world攒发!调塌,那么我們?cè)趺赐ㄟ^(guò)接口來(lái)操作數(shù)據(jù)庫(kù)呢。
1惠猿、首先我們要在自己電腦上安裝一個(gè)MySQL羔砾,安裝MySQL是一個(gè)很操蛋的過(guò)程,安裝的方法有很多偶妖,但是可能遇到的問(wèn)題也很多姜凄,慢慢享受。Mac上安裝MySQL參考http://www.reibang.com/p/bded35de93eb
2趾访、創(chuàng)建一個(gè)庫(kù)hello
可以用一些可視化工具創(chuàng)建如:sequel pro等态秧,也可以用指令。
wjc@wangjunhaodeMBP ~ % mysql.server start //啟動(dòng)mysql
wjc@wangjunhaodeMBP ~ % mysql -u root -p //進(jìn)入mysql
mysql> show databases; //查看當(dāng)前用戶下所有庫(kù)
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
| wjc |
+--------------------+
6 rows in set (0.01 sec)
mysql> create database hello; //創(chuàng)建庫(kù)hello
Query OK, 1 row affected (0.00 sec)
mysql> show databases; //發(fā)現(xiàn)hello庫(kù)已經(jīng)創(chuàng)建成功
+--------------------+
| Database |
+--------------------+
| information_schema |
| hello |
| mysql |
| performance_schema |
| sys |
| test |
| wjc |
+--------------------+
7 rows in set (0.01 sec)
3扼鞋、修改pom.xml文件
image.png
4、右鍵pom.xml云头,重新導(dǎo)入maven
image.png
5捐友、新增配置文件application.yml,并配置數(shù)據(jù)庫(kù)
image.png
6溃槐、新增數(shù)據(jù)模型類(lèi)Hellomodel匣砖,記得增加構(gòu)造函數(shù)和set、get方法
image.png
7、新建接口文件HelloRepository
image.png
并且繼承JpaRepository類(lèi)猴鲫,傳兩個(gè)參數(shù) Hellomodel, Integer
image.png
8砌溺、然后就可以在HelloController里編寫(xiě)增刪改查的接口了,HelloController代碼如下:
package com.example.hello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController //表示方法的返回值直接以指定的格式寫(xiě)入Http response body中变隔,而不是解析為跳轉(zhuǎn)路徑规伐。
public class HelloController {
@Autowired
private HelloRepository helloRepository; //實(shí)例化接口
//新增數(shù)據(jù)
@PostMapping("/hello")
public Hellomodel create(@RequestParam("title") String title,
@RequestParam("message") String message){
Hellomodel hellomodel = new Hellomodel();
hellomodel.setTitle(title);
hellomodel.setMessage(message);
return helloRepository.save(hellomodel);
}
//獲取數(shù)據(jù)列表
@GetMapping("/hello")
public List<Hellomodel> list(){
return helloRepository.findAll();
}
//通過(guò)ID查詢item
@GetMapping("/hello/{id}")
public Hellomodel findById(@PathVariable("id") Integer id){
return helloRepository.findById(id).orElse(null);
}
//通過(guò)ID更新
@PutMapping("/hello/{id}")
public Hellomodel update(@PathVariable("id") Integer id,
@RequestParam("title") String title){
Optional<Hellomodel> optional = helloRepository.findById(id);
if(optional.isPresent()){
Hellomodel hellomodel = optional.get();
hellomodel.setTitle(title);
return helloRepository.save(hellomodel);
}
return null;
}
}
9、然后在postman中驗(yàn)證是否正確image.png