簡體   English   中英

如何使用 Spring 數據 Z9CE3D1BD8890F16A0C7C64809359508 將 JSON 數據保存到 MySQL 數據庫中

[英]How to save JSON Data into MySQL DB using Spring Data JPA

Want to save some data's into DB by creating entity class, repository class in spring boot using spring data JPA, but not able to figure out how to save this complex JSON data.

這是實體 class 和 JSON 數據,對我來說具有挑戰性的是表中的組件數據字段,無法弄清楚如何將其寫入數據庫。

public class Component {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int Component_Id;
    private String Component_Name;


    private float Component_Version;
    
    @OneToOne(targetEntity = ComponentData.class, cascade =CascadeType.ALL)
    @JoinColumn(name = "comp_fk", referencedColumnName = "Component_Id")
    private List<ComponentData> componentData;
    private String Module_Name;
    private int Created_By;
    private int Project_Id;
    private LocalDate Uploaded_DateTime;
    private String Mode;
    private String test_case;
    private String activity;
    private String apk;
    private String pkg;
    private String secs;
    private String value;
    private String component_data_id;
    private int hash;
    private LocalDate component_date;
    private int component_date_id;
}

對於 ComponentData,我又創建了一個 class,

@Entity
public class ComponentData {

    @Id
    private int Component_Id;
    private List<Data> data;

    private List<Page> page;

    public int getComponent_Id() {
        return Component_Id;
    }

    public void setComponent_Id(int component_Id) {
        Component_Id = component_Id;
    }


    
}

同樣對於componentData內部的數據,我為兩個對象創建了另外兩個類。

public class Data {

    private String city;
    private String wait;
    private String Click;
    private String email;
    private String phone;
    private String title;
    private String company;

private String lastName;
private String Test_Case;
private String firstname;
private String salutation;
private String checknotify;
}


public class Page {

    private String Index;

    private String Property;

    private String Identifier;

    private String Data_Column;

    private String Description;

    private String Screenshots;

    private String User_Action;

    private String Identifier_Value;


   }

ComponentData 看起來像這樣,我想保存在 MySql 數據庫中。

{"Data": [{"city": "Mumbai", "wait": "2", "Click": "1", "email": "deepankarb@quinnox.com", "phone": "+91(9821115591)", "title": "VP", "company": "Quinnox", "lastname": "Bandopadhya", "Test_Case": "valid", "firstname": "Deepankar", "salutation": "Mr.", "checknotify": "\"Lead \"\"+checktitle+\" \"+checkfirstname+\" \"+checklastname+\"\" was created.\""}, 

{"city": "", "wait": "", "Click": "", "email": "", "phone": "", "title": "", "company": "", "lastname": "", "Test_Case": "T34", "firstname": "", "salutation": "", "checknotify": ""}, 

{"city": "", "wait": "", "Click": "", "email": "", "phone": "", "title": "", "company": "", "lastname": "Salunke", "Test_Case": "T35", "firstname": "", "salutation": "", "checknotify": ""}, 

{"city": "", "wait": "", "Click": "", "email": "", "phone": "", "title": "", "company": "", "lastname": "", "Test_Case": "T36", "firstname": "", "salutation": "", "checknotify": ""},

{"city": "", "wait": "", "Click": "1", "email": "", "phone": "", "title": "", "company": "", "lastname": "", "Test_Case": "invalid", "firstname": "", "salutation": "", "checknotify": ""}], 

"Page": [{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "Click", "Description": "This is for Test", "Screenshots": "Y", "User_Action": "CLICKJS", "Identifier_Value": "//[text()=''Leads'']"}, 

{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "", "Description": "", "Screenshots": "", "User_Action": "CLICK", "Identifier_Value": "//[text()=\"New\"]"}, 

{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "email", "Description": "", "Screenshots": "", "User_Action": "SET", "Identifier_Value": "//[text()=\"New Lead\"]//following::input[1]"}, 

{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "salutation", "Description": "", "Screenshots": "", "User_Action": "SET", "Identifier_Value": "//[text()=\"New Lead\"]//following::input[2]"}, 

{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "", "Description": "", "Screenshots": "", "User_Action": "CLICK", "Identifier_Value": "//[text()=\"Mr.\"]"}, 

{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "checktitle", "Description": "", "Screenshots": "", "User_Action": "GET", "Identifier_Value": "//[text()=\"New Lead\"]//following::input[2]"}, 

{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "firstname", "Description": "", "Screenshots": "", "User_Action": "SET", "Identifier_Value": "//[text()=\"New Lead\"]//following::input[3]"},

 {"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "checkfirstname", "Description": "", "Screenshots": "", "User_Action": "GET", "Identifier_Value": "//[text()=\"New Lead\"]//following::input[3]"},

 {"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "lastname", "Description": "", "Screenshots": "Y", "User_Action": "SET", "Identifier_Value": "//[text()=\"Last Name\"]//following::input[1]"}, 

{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "checklastname", "Description": "", "Screenshots": "Y", "User_Action": "GET", "Identifier_Value": "//[text()=\"Last Name\"]//following::input[1]"}, 

{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "company", "Description": "", "Screenshots": "Y", "User_Action": "SET", "Identifier_Value": "//[text()=\"Lead Information\"]//following::input[5]"}, 

{"Index": "", "Property": "", "Identifier": "", "Data_Column": "checknotify", "Description": "", "Screenshots": "", "User_Action": "MODIFYDYNAMICVALUE", "Identifier_Value": ""}, 

{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "title", "Description": "", "Screenshots": "", "User_Action": "SET", "Identifier_Value": "//[text()=\"New Lead\"]//following::input[6]"}, 

{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "", "Description": "", "Screenshots": "", "User_Action": "CLICK", "Identifier_Value": "//[text()=\"New Lead\"]//following::input[7]"}, 

{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "", "Description": "", "Screenshots": "", "User_Action": "CLICK", "Identifier_Value": "//[text()=\"VP Level\"]"}, 

{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "", "Description": "", "Screenshots": "", "User_Action": "SCROLLTO", "Identifier_Value": "//[text()=\"Phone Information\"]"}, 

{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "phone", "Description": "", "Screenshots": "", "User_Action": "SET", "Identifier_Value": "//[text()=\"Phone Information\"]//following::input[1]"}, 

{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "", "Description": "", "Screenshots": "", "User_Action": "SCROLLTO", "Identifier_Value": "//[text()=\"Lead Source\"]"}, 

{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "", "Description": "", "Screenshots": "", "User_Action": "CLICK", "Identifier_Value": "//[text()=\"Lead Source\"]//following::input[1]"},

 {"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "", "Description": "", "Screenshots": "", "User_Action": "CLICK", "Identifier_Value": "//[text()=\"Referral: Board or SLT\"]"},

 {"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "", "Description": "", "Screenshots": "", "User_Action": "SCROLLTO", "Identifier_Value": "//[text()=\"Company Information\"]"},

 {"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "company", "Description": "", "Screenshots": "", "User_Action": "SET", "Identifier_Value": "//[text()=\"Company Information\"]//following::input[1]"}, 

{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "", "Description": "", "Screenshots": "", "User_Action": "CLICK", "Identifier_Value": "//[text()=\"Market Segment\"]//following::input[1]"}, 

{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "", "Description": "", "Screenshots": "", "User_Action": "CLICK", "Identifier_Value": "//[text()=\"Small Enterprise (<$250 million)\"]"},

 {"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "", "Description": "", "Screenshots": "", "User_Action": "SCROLLTO", "Identifier_Value": "//[text()=\"Industry Segment\"]"},

{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "", "Description": "", "Screenshots": "", "User_Action": "CLICK", "Identifier_Value": "//[text()=\"Industry Segment\"]//following::input[1]"}, 

{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "", "Description": "", "Screenshots": "", "User_Action": "CLICK", "Identifier_Value": "//[text()=\"Retail\"]"}, 

{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "", "Description": "", "Screenshots": "", "User_Action": "CLICK", "Identifier_Value": "//[text()=\"Industry Sub-Segment\"]//following::input[1]"}, 

{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "", "Description": "", "Screenshots": "", "User_Action": "CLICK", "Identifier_Value": "//[text()=\"Food Stores\"]"}, 

{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "", "Description": "", "Screenshots": "", "User_Action": "SCROLLTO", "Identifier_Value": "//[text()=\"City\"]"}, 

{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "city", "Description": "", "Screenshots": "", "User_Action": "SET", "Identifier_Value": "//[text()=\"City\"]//following::input[1]"},


 {"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "", "Description": "", "Screenshots": "Y", "User_Action": "CLICK", "Identifier_Value": "//[text()=\"Save & New\"]//following::button"}, 


{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "checknotify", "Description": "", "Screenshots": "Y", "User_Action": "DYNAMICVERIFY", "Identifier_Value": "//[@data-aura-class=\"forceActionsText\"]"},


 {"Index": "", "Property": "", "Identifier": "", "Data_Column": "wait", "Description": "", "Screenshots": "", "User_Action": "WAIT", "Identifier_Value": ""}, 

{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "", "Description": "", "Screenshots": "", "User_Action": "CLICK", "Identifier_Value": "//[text()=\"Edit\"]//following::button[1]"}, 


{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "", "Description": "", "Screenshots": "", "User_Action": "CLICK", "Identifier_Value": "//[text()=\"Delete\"]"}, 

{"Index": "", "Property": "", "Identifier": "XPATH", "Data_Column": "", "Description": "", "Screenshots": "", "User_Action": "CLICK", "Identifier_Value": "//*[text()=\"Are you sure you want to delete this lead?\"]//following::button[2]"}]}

有2個不同的問題。 首先是將您的 Json 轉換為 Bean 類(此過程稱為反序列化)。 If your send your Json string as an input to your Spring Boot Rest Controller Spring Boot will automatically attempt to convert your Json into an instance of the class that is listed as a method parameter in your controller. 如果您從其他地方訪問您的 Json,那么您需要自己將其轉換為您的類的實例。 為此,您可以使用 Json Jackson 庫(Spring Boot 默認),或 Gson 或任何其他 ZEED833842 庫43385B808A6CEE6。 我在 Json-Jackson 庫上編寫了自己的東西包裝器,它可以簡化一些序列化/反序列化過程。 您可以在此處查看 class JsonUtils。 這個 class 是我編寫和維護的 MgntUtils 庫的一部分。 您可以在Maven 工件github上獲得它。

創建實體實例后,您現在可以使用標准 JPA 將它們保存到數據庫中。 如果這些實例是從 Json 或任何其他方式創建的,這並不重要。 所以你使用 JPA 來保存你的實體實例而不是 Json

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM