Kotlin+Vue+Spring Data JPA+MySQL 增查改刪
阿新 • • 發佈:2020-07-10
概述:
- Kotlin為後端開發語言,持久層是Spring Data JPA
- 前後端分離,進行簡單增查改刪(CRUD)
- 前端使用VUE
- 資料庫使用MySQL
往期內容
# | 內容 |
---|---|
01 | React+Spring Boot JPA+MySQL 增查改刪 |
02 | Vue+Spring Boot JPA+MySQL 增查改刪 增查改刪 |
03 | Vue+Spring Boot 檔案操作,上傳、預覽和刪除 |
04 | Thymeleaf+Spring Boot 檔案操作,上傳、預覽和刪除 |
Vue前端程式碼,不再重複。以下是Kotlin後臺程式碼
#EmployeeController.kt
package com.example.kotlinjpacrud.controller
import com.example.kotlinjpacrud.entity.Employee
import com.example.kotlinjpacrud.repositories.EmployeeRepository
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Sort
import org.springframework.data .web.PageableDefault
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import javax.validation.Valid
@RestController
@RequestMapping("/api/employee")
class EmployeeController(private val employeeRepository: EmployeeRepository) {
/**
* 獲取所有員工分頁
* 以欄位Id為降序
* 沒有為3條記錄
*/
@GetMapping
fun getAllEmployees(@PageableDefault(sort = ["id"], direction = Sort.Direction.DESC, size = 3) pageable: Pageable): Page<Employee> {
return employeeRepository.findAll(pageable)
}
/**
* 新增員工
*/
@PostMapping
fun createEmployee(@Valid @RequestBody employee: Employee): Employee {
return employeeRepository.save(employee)
}
/**
* 根據ID獲取員工
*/
@GetMapping("/{id}")
fun getEmployeeById(@PathVariable(value = "id") employeeId: Long): ResponseEntity<Employee> {
return employeeRepository.findById(employeeId)
.map { employee -> ResponseEntity.ok(employee) }
.orElse(ResponseEntity.notFound().build())
}
/**
* 修改員工
*/
@PutMapping
fun updateEmployeeById(@Valid @RequestBody newEmployee: Employee): ResponseEntity<Employee> {
return employeeRepository.findById(newEmployee.id)
.map { existingArticle ->
val updatedArticle: Employee = existingArticle
.copy(name = newEmployee.name,
gender = newEmployee.gender,
age = newEmployee.age,
introduce = newEmployee.introduce)
ResponseEntity.ok().body(employeeRepository.save(updatedArticle))
}.orElse(ResponseEntity.notFound().build())
}
/**
* 根據ID刪除
*/
@DeleteMapping("/{id}")
fun deleteEmployeeById(@PathVariable(value = "id") employeeId: Long): ResponseEntity<Void> {
return employeeRepository.findById(employeeId)
.map { deleteEmployee ->
employeeRepository.delete(deleteEmployee)
ResponseEntity<Void>(HttpStatus.OK)
}.orElse(ResponseEntity.notFound().build())
}
}
#Employee.kt
package com.example.kotlinjpacrud.entity
import com.example.kotlinjpacrud.enums.Gender
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.Id
@Entity
data class Employee(
@Id @GeneratedValue
var id: Long =0,
var name: String ="",
var gender: Gender = Gender.MALE,
var age: Int =0,
var introduce: String =""
)
#EmployeeRepository.tk
package com.example.kotlinjpacrud.repositories
import com.example.kotlinjpacrud.entity.Employee
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
@Repository
interface EmployeeRepository :JpaRepository<Employee,Long> {
}