登录
首页 >  数据库 >  MySQL

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

来源:SegmentFault

时间:2023-01-20 16:57:41 488浏览 收藏

怎么入门数据库编程?需要学习哪些知识点?这是新手们刚接触编程时常见的问题;下面golang学习网就来给大家整理分享一些知识点,希望能够给初学者一些帮助。本篇文章就来介绍《Kotlin+Vue+Spring Data JPA+MySQL 增查改删》,涉及到MySQL、分页、vue.js、kotlin、jpa,有需要的可以收藏一下

概述:
  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+Spring Data JPA+MySQL 增查改删》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>