springboot入門-搭建web服務
? 作為如今java EE 主流開發框架,了解springboot 的開發規則,對我們進行代碼審計工作具有重要意義。
一、springboot 搭建基本web服務
1、idea創建springboot項目

目前springboot 項目通過官網生成的項目,最低要求jdk版本17

理論上這里我們可以先不添加任何依賴,等項目搭建起來也可以添加依賴。
創建即可:

可以刪除無關項,一個最簡單的springboot - web開發項目就搭建起來了

2、maven 和 jdk 設置
? maven 版本要高一點

? jdk 版本至少是17


3、啟動
application.properties
spring.application.name=springboot_demo
server.port=8081
寫一個基本的controller
package com.kaix.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping("/index")
public String index(@RequestParam String param) {
return "參數是: " + param;
}
}
啟動:


訪問controller 定義好的路徑:

二、Service + Dao

1、Service
添加service 層代碼
package com.kaix.service;
public interface TestService {
public String test(String param);
}
實現類
package com.kaix.service.impl;
import com.kaix.service.TestService;
import org.springframework.stereotype.Service;
@Service
public class TestServiceImpl implements TestService {
@Override
public String test(String param) {
return "server 方法執行: hello: " + param;
}
}
controller
package com.kaix.controller;
import com.kaix.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private TestService testService;
@GetMapping("/index")
public String index(@RequestParam String param) {
return "參數是: " + param;
}
// 測試 testService
@GetMapping("/index1")
public String index1(@RequestParam String param) {
return testService.test(param);
}
}

2、Dao
? 這里數據庫交互使用的mybatis
application.yml配置mysql
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/demo
username: root
password: root
接口:
package com.kaix.dao;
import com.kaix.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface UserDao {
@Select("select * from user where id = #{id}")
public User queryById(Integer id);
}
實體類:
package com.kaix.domain;
public class User {
private int id;
private String name;
private String sex;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Test{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
使用測試單元測試:

測試類
@Autowired
private UserDao userDao;
@Test
void queryById() {
User user1 = userDao.queryById(1);
System.out.println(user1);
}
三、springboot常用注解
1、springboot 基礎注解
| 注解 | 說明 |
|---|---|
@SpringBootApplication |
項目啟動入口注解,等同于 @Configuration + @EnableAutoConfiguration + @ComponentScan |
@RestController |
標注為 REST 控制器,返回 JSON 數據(一般都使用這個,因為不用視圖) |
@Controller |
標注為普通控制器(返回視圖) |
@Service |
標注服務類(業務邏輯層) |
@Component |
標注通用組件類(通常用于工具類、配置類) |
@Repository |
標注持久層類(如 DAO,帶異常轉換) |
@Autowired |
自動注入 Bean |
2、Controller 相關
| 注解 | 說明 |
|---|---|
@RequestMapping |
請求映射,可以標注類或方法 |
@GetMapping / @PostMapping 等 |
請求方式簡化版注解 |
@RequestParam |
獲取 URL 中的參數,如 ?id=1 |
@PathVariable |
獲取路徑中的參數,如 /user/{id} |
@RequestBody |
獲取請求體中的 JSON 數據并自動轉換為對象 |
@ResponseBody |
表示返回內容為響應體(@RestController 自動包含它) |
3、mybatis 相關
| 注解 | 說明 |
|---|---|
@Mapper |
標記-接口-為 MyBatis 的 Mapper(Spring Boot 自動識別) |
@Select |
編寫查詢 SQL |
@Insert |
編寫插入 SQL |
@Update |
編寫更新 SQL |
@Delete |
編寫刪除 SQL |
@Results, @Result, @One, @Many |
復雜結果映射關系(例如手動映射列到字段) |
浙公網安備 33010602011771號