KotlinVueSpring-Data-JPAMySQL-增查改删

44次阅读

共计 2671 个字符,预计需要花费 7 分钟才能阅读完成。

概述:

  1. Kotlin 为后端开发语言, 长久层是 Spring Data JPA
  2. 前后端拆散,进行简略增查改删(CRUD)
  3. 前端应用 VUE
  4. 数据库应用 MySQL

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> {}

正文完
 0