什么軟件做高級網(wǎng)站網(wǎng)絡推廣企業(yè)
?查看頁面原型,明確需求需求
頁面原型
?需求分析
閱讀接口文檔
接口文檔鏈接如下:
https://docs.qq.com/doc/DUkRiTWVaUmFVck9N
思路分析
用戶發(fā)送請求,交由對應的Controller類進行處理,Controller類調(diào)用service實現(xiàn)查詢部門功能,對應的service業(yè)務層調(diào)用對應的mapper接口,通過mapper接口查詢數(shù)據(jù)庫執(zhí)行select * from dept;SQL語句,將查詢結(jié)果返回給service,service將查詢結(jié)果返回給Controller類,Controller將查詢結(jié)果封裝在統(tǒng)一查詢結(jié)果Result類中,最終響應給前端。
功能接口的開發(fā)
?控制層(Controller)
具體代碼如下
package com.example.tlias.controller;import com.example.tlias.pojo.Dept;
import com.example.tlias.pojo.Result;
import com.example.tlias.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;
import java.util.logging.Logger;@RestController
@Slf4j // 日志注解
public class DeptController {@Autowired// 注入service對象private DeptService deptService;// 獲取日志記錄對象// todo 查詢部門信息// 指定請求路徑及方式// @RequestMapping(value = "/depts", method = RequestMethod.GET)// 上述注解的簡化@GetMapping("depts")public Result list() {log.info("查詢?nèi)坎块T數(shù)據(jù)");// 調(diào)用service查詢部門信息List<Dept> deptList = deptService.list();return Result.success(deptList);}
}
業(yè)務層(Service)
具體代碼如下
Service接口
package com.example.tlias.service;import com.example.tlias.pojo.Dept;import java.util.List;public interface DeptService {/*** 查詢?nèi)坎块T數(shù)據(jù)** @return*/List<Dept> list();
}
?Service接口實現(xiàn)類
package com.example.tlias.service.impl;import com.example.tlias.mapper.DeptMapper;
import com.example.tlias.pojo.Dept;
import com.example.tlias.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class DeptServiceImpl implements DeptService {@Autowiredprivate DeptMapper deptMapper;@Overridepublic List<Dept> list() {return deptMapper.list();}
}
持久層(Mapper)
package com.example.tlias.mapper;import com.example.tlias.pojo.Dept;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;import java.util.List;@Mapper
public interface DeptMapper {/*** 查詢?nèi)康牟块T數(shù)據(jù)** @return*/@Select("select * from dept")List<Dept> list();
}
接口測試
使用Postman來進行接口測試,首先啟動SpringBoot項目,然后再在Postman中發(fā)送對應請求
具體運行結(jié)果如下
{"code": 1,"msg": "success","data": [{"id": 1,"name": "學工部","createTime": "2023-08-07T15:44:50","updateTime": "2023-08-07T15:44:50"},{"id": 2,"name": "教研部","createTime": "2023-08-07T15:44:50","updateTime": "2023-08-07T15:44:50"},{"id": 3,"name": "咨詢部","createTime": "2023-08-07T15:44:50","updateTime": "2023-08-07T15:44:50"},{"id": 4,"name": "就業(yè)部","createTime": "2023-08-07T15:44:50","updateTime": "2023-08-07T15:44:50"},{"id": 5,"name": "人事部","createTime": "2023-08-07T15:44:50","updateTime": "2023-08-07T15:44:50"}]
}
由于在控制類中使用的注解為@RestController(@Controller`和`@ResponseBody`注解的組合),控制類會自動將返回給前端的結(jié)果轉(zhuǎn)換為JSON格式的數(shù)據(jù)。測試成功
?