概述:
- 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> {
}