Java 學習記錄135 — 員工管理系統專案練習-Create Spring Rest Controller

package controller;import model.Employee;
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.RestController;
import repository.EmployeeRepository;
import java.util.List;@RestController
@RequestMapping("/api/v1")
public class EmployeeController {
@Autowired
private EmployeeRepository employeeRepository;
// get all employees
@GetMapping("/employees")
public List<Employee> getAllEmployees() {
return employeeRepository.findAll();
}
}
@Autowired
private EmployeeRepository employeeRepository;
private EmployeeRepository employeeRepository;@Autowired
public void setEmployeeRepository(EmployeeRepository employeeRepository) {
this.employeeRepository = employeeRepository;
}
  • 到 application.properties 文件裡,填上資料庫相關資訊
  • 新增 model 資料夾,裡面添加 table 的結構
  • 新增 repository 資料夾,裡面添加對應 table 的 repository ,extends JpaRepository 來使用人家寫好的常用方法
  • 新增 exception 資料夾,裡面添加自定義的 exception
  • 新增 controller 資料夾,裡面寫對應 table 的 API

--

--

記錄成為軟體工程師的過程

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
張小雄

張小雄

記錄成為軟體工程師的過程