Vue+Spring Data JPA+MySQL 增查改删

视频讲解:

https://www.bilibili.com/video/BV16i4y1G7i2/

工程概述:

  1. 前后端分离,进行简单增查改删(CRUD)
  2. 前端使用VUE
  3. 后端使用Spring Data JPA
  4. 数据库使用MySQL

Vue+Spring Data JPA+MySQL 增查改删

EmployeeController.java

package com.deepincoding.springdatajpamysqlcrud.controller;

import com.deepincoding.springdatajpamysqlcrud.entity.Employee;
import com.deepincoding.springdatajpamysqlcrud.model.BaseResponse;
import com.deepincoding.springdatajpamysqlcrud.service.EmployeeService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
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.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/api/employee")
@Log4j2
public class EmployeeController extends BaseController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping
    public BaseResponse<String> save(@RequestBody Employee employee){
          employeeService.save(employee);
          return new BaseResponse<>(SUCCESS,"保存成功");
    }

    @GetMapping("/{id}")
    public BaseResponse<Optional<Employee>> findById(@PathVariable Long id){
        Optional<Employee> employee = employeeService.findById(id);
        return new BaseResponse<>(SUCCESS,"获取成功",employee);
    }

    @GetMapping
    public BaseResponse<Page<Employee>> findAll(@PageableDefault(sort = {"id"},direction = Sort.Direction.DESC ,size = 3) Pageable pageable){
        log.info("pageable:{}",pageable.getPageNumber());
        Page<Employee> employees = employeeService.findAll(pageable);
        return new BaseResponse<>(SUCCESS,"获取成功",employees);
    }

    @PutMapping
    public BaseResponse<String> update(@RequestBody Employee employee){
        employeeService.save(employee);
        return new BaseResponse<>(SUCCESS,"修改成功");
    }

    @DeleteMapping("/{id}")
    public BaseResponse<String> delete(@PathVariable Long id){
        employeeService.delete(id);
        return new BaseResponse<>(SUCCESS,"删除成功");
    }

}

CorsConfig.java

package com.deepincoding.springdatajpamysqlcrud.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CorsConfig implements WebMvcConfigurer {
    /**
     * 跨域配置
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("*");
    }
}

Employee.java

package com.deepincoding.springdatajpamysqlcrud.entity;

import com.deepincoding.springdatajpamysqlcrud.enums.Gender;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;


import javax.persistence.*;

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Employee {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private long id;
    private String name;
    @Enumerated(EnumType.STRING)
    private Gender gender;
    private Integer age;
    private String introduce;

}

EmployeeServiceImpl.java

package com.deepincoding.springdatajpamysqlcrud.service;

import com.deepincoding.springdatajpamysqlcrud.entity.Employee;
import com.deepincoding.springdatajpamysqlcrud.repositories.EmployeeRep;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Pageable;

import java.util.Optional;


@Service
public class EmployeeServiceImpl implements  EmployeeService{

    @Autowired
    private EmployeeRep employeeRep;

    @Override
    public void save(Employee employee) {
        employeeRep.save(employee);
    }

    @Override
    public Optional<Employee> findById(Long id) {
        return employeeRep.findById(id);
    }

    @Override
    public void update(Employee employee) {
        employeeRep.save(employee);
    }

    @Override
    public void delete(Long id) { employeeRep.deleteById(id); }

    @Override
    public Page<Employee> findAll(Pageable pageable) {
        return employeeRep.findAll(pageable);
    }
}

本文使用 mdnice 排版

原文 

https://segmentfault.com/a/1190000023088564

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » Vue+Spring Data JPA+MySQL 增查改删

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址