Vue+SpringDataJPA+MySQL增查改删
视频讲解:
https://www.bilibili.com/video/BV16i4y1G7i2/
工程概述:
前后端分离,进行简单增查改删(CRUD)
前端使用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 publicclassEmployeeControllerextendsBaseController{ @Autowiredprivate EmployeeService employeeService; @PostMappingpublic BaseResponse<String> save(@RequestBody Employee employee){ employeeService.save(employee); returnnew BaseResponse<>(SUCCESS,"保存成功"); } @GetMapping("/{id}") public BaseResponse<Optional<Employee>> findById(@PathVariable Long id){ Optional<Employee> employee = employeeService.findById(id); returnnew BaseResponse<>(SUCCESS,"获取成功",employee); } @GetMappingpublic 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); returnnew BaseResponse<>(SUCCESS,"获取成功",employees); } @PutMappingpublic BaseResponse<String> update(@RequestBody Employee employee){ employeeService.save(employee); returnnew BaseResponse<>(SUCCESS,"修改成功"); } @DeleteMapping("/{id}") public BaseResponse<String> delete(@PathVariable Long id){ employeeService.delete(id); returnnew 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;
@ConfigurationpublicclassCorsConfigimplementsWebMvcConfigurer{ /** * 跨域配置 * @param registry */@OverridepublicvoidaddCorsMappings(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@AllArgsConstructorpublicclassEmployee{ @Id@GeneratedValue(strategy= GenerationType.AUTO) privatelong 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; @ServicepublicclassEmployeeServiceImplimplementsEmployeeService{ @Autowiredprivate EmployeeRep employeeRep; @Overridepublicvoidsave(Employee employee){ employeeRep.save(employee); } @Overridepublic Optional<Employee> findById(Long id){ return employeeRep.findById(id); } @Overridepublicvoidupdate(Employee employee){ employeeRep.save(employee); } @Overridepublicvoiddelete(Long id){ employeeRep.deleteById(id); } @Overridepublic Page<Employee> findAll(Pageable pageable){ return employeeRep.findAll(pageable); } }
以上是 Vue+SpringDataJPA+MySQL增查改删 的全部内容, 来源链接: utcz.com/z/518025.html