Kotlin+Vue+Spring Data JPA+MySQL 增查改删

概述:

  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 {
        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 {
        return employeeRepository.findById(employeeId)
                .map { employee -> ResponseEntity.ok(employee) }
                .orElse(ResponseEntity.notFound().build())
    }

    /**
     * 修改员工
     */
    @PutMapping
    fun updateEmployeeById(@Valid @RequestBody newEmployee: Employee): ResponseEntity {

        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 {
        return employeeRepository.findById(employeeId)
                .map { deleteEmployee ->
                    employeeRepository.delete(deleteEmployee)
                    ResponseEntity(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 {
}

你可能感兴趣的:(kotlin,vue.js,jpa,mysql,分页)