简体   繁体   English

用于持久化 API 数据的存储解决方案

[英]Storage solution for persisting the API’s data

I've made a CRUD REST API with Spring Boot, Spring Data JPA and MySQL and everything's okay.我用 Spring Boot、Spring Data JPA 和 MySQL 制作了一个 CRUD REST API,一切正常。 I'm using postman aswell, and everything seems okay, it is connected to my sqldb, it creates in my sql, updates, retrieve and deletes, but whenever I stop it, and start it again, all my data once created is gone.我也在使用 postman,一切似乎都很好,它连接到我的 sqldb,它在我的 sql 中创建、更新、检索和删除,但是每当我停止它并再次启动它时,我创建的所有数据都消失了。 I need a solution for persisting the API's data.我需要一个持久化 API 数据的解决方案。

My application.yaml我的申请.yaml

 spring:
  datasource:
    url: jdbc:mysql://localhost:3306/cloud_vendor?useSSL=false
    username: root
    password: root

  mvc:
    pathmatch:
      matching-strategy: ANT_PATH_MATCHER


  # The SQL dialect makes Hibernate generate better SQL for the chosen database
  jpa:
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQLDialect

#JPA Settings
  jpa.hibernate.ddl_auto: create

My CloudVendorController.java我的CloudVendorController.java

package com.thinkcon.demo.controller;

import com.thinkcon.demo.model.CloudVendor;
import com.thinkcon.demo.service.CloudVendorService;

import java.util.List;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/cloudvendor")
public class CloudVendorController {
    
    CloudVendorService cloudVendorService;

    public CloudVendorController(CloudVendorService cloudVendorService)
    {
        this.cloudVendorService = cloudVendorService;
    }

    @GetMapping("{vendorId}")
    public CloudVendor getCloudVendorDetails(@PathVariable("vendorId") String vendorId){
        return cloudVendorService.getCloudVendor(vendorId);
    }

    @GetMapping()
    public List<CloudVendor> getAllCloudVendorDetails(){
        return cloudVendorService.getAllCloudVendors();
    }

    @PostMapping
    public String createCloudVendorDetails(@RequestBody CloudVendor cloudVendor)
    {
        
        cloudVendorService.createCloudVendor(cloudVendor);
        return "CloudVendor Created Successfully";
    }

    @PutMapping
    public String updateCloudVendorDetails(@RequestBody CloudVendor cloudVendor)
    {
        cloudVendorService.updateCloudVendor(cloudVendor);
        return "CloudVendor updated Successfully";
    }

    @DeleteMapping("{vendorId}")
    public String deleteCloudVendorDetails(@PathVariable("vendorId")String vendorId)
    {
        cloudVendorService.deleteCloudVendor(vendorId);
         return "CloudVendor deleted Successfully";
    }
}

And my model\CloudVendor.java还有我的模型\CloudVendor.java

package com.thinkcon.demo.model;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
@Table(name="cloud_vendor_info")

public class CloudVendor
{
    @Id
    private String vendorId;
    private String vendorName;
    private String vendorAddress;
    private String vendorPhoneNumber;

    

    public CloudVendor(String vendorId, String vendorName, String vendorAddress, String vendorPhoneNumber) {
        this.vendorId = vendorId;
        this.vendorName = vendorName;
        this.vendorAddress = vendorAddress;
        this.vendorPhoneNumber = vendorPhoneNumber;
    }

    

    public CloudVendor() {

    }



    public String getVendorId() {
        return vendorId;
    }
    public String getVendorName() {
        return vendorName;
    }
    public String getVendorAddress() {
        return vendorAddress;
    }
    public String getVendorPhoneNumber() {
        return vendorPhoneNumber;
    }
    public void setVendorId(String vendorId) {
        this.vendorId = vendorId;
    }
    public void setVendorName(String vendorName) {
        this.vendorName = vendorName;
    }
    public void setVendorAddress(String vendorAddress) {
        this.vendorAddress = vendorAddress;
    }
    public void setVendorPhoneNumber(String vendorPhoneNumber) {
        this.vendorPhoneNumber = vendorPhoneNumber;
    }
    
    
}

I need the solution, I can't have my data being restarted all over again everytime I just end it.我需要解决方案,我不能每次结束时都重新启动我的数据。

There are 5 types;有5种类型;

  1. create -> drops existing tables, then creates new tables.创建 -> 删除现有表,然后创建新表。

  2. update -> creates, compares with old ones, if there are changes, then updates and never deletes the existing tables or columns. update -> 创建,与旧的比较,如果有变化,然后更新并且永远不会删除现有的表或列。

  3. create-drop -> similar to create, but it will drop the database after shutdown. create-drop -> 类似于create,但它会在关闭后删除数据库。

  4. validate -> validates whether the tables and columns exist, otherwise it throws an exception for that. validate -> 验证表和列是否存在,否则会抛出异常。

  5. none -> turns off the DDL generation. none -> 关闭 DDL 生成。

So, you will need;所以,你需要;

jpa.hibernate.ddl_auto: update

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM