zoukankan      html  css  js  c++  java
  • Kotlin+Vue+Spring Data JPA+MySQL 增查改删

    概述:

    1. Kotlin为后端开发语言,持久层是Spring Data JPA
    2. 前后端分离,进行简单增查改删(CRUD)
    3. 前端使用VUE
    4. 数据库使用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> {
    }
    

     

  • 相关阅读:
    【Udacity】线性回归方程 Regression
    【Android】Warning :uninstalling will remove the application data!
    【Udacity】数据的差异性:值域、IQR、方差和标准差
    docker部署一个简单的mian.py项目文件
    ubuntu安装goland
    Go 语言变量、常量
    Go 语言数据类型
    linux 之文件重命名
    linux 下新建文件自动加锁的解决办法
    go之linux安装
  • 原文地址:https://www.cnblogs.com/JavaWeiBianCheng/p/13277909.html
Copyright © 2011-2022 走看看