JPA hibernate spring repository pgsql java 工程(二):sql檔案匯入資料,測試資料

來源:互聯網
上載者:User

標籤:序列   rac   repos   repo   highlight   oca   location   檔案匯入   nextval   

使用jpa儲存查詢資料都很方便,除了在代碼中加入資料外,可以使用sql進行匯入。目前我只會一種方法,把資料集中在一個sql檔案中。

而且資料在匯入中常常具有先後關係,需要用串列的方式匯入。

 

 

第一步:配置 需要指定spring boot對資料有create的配置,才能自動載入sql檔案

[java] view plain copy
  1. spring.jpa.hibernate.ddl-auto=create  


第二步:將對應的.sql檔案放入src/main/resources下。我採用的是import.sql檔案,如果有多個sql似乎系統會選擇一個檔案名稱排序靠前的檔案讀取。

 

第三步:

在sql寫入執行語句:如參考所示就是pgsql的插入語句。nextval(‘location_seq‘)是指定主鍵的值為對應的自增序列的值。對於jpa中定義了自增

序列作為主鍵,如果採用自訂主索引值方法插入資料,並且沒對相應的序列做處理,會在系統調用Repository方法進行操作的時候發生錯誤。

出現無法儲存,更改的情況!

[sql] view plain copy
    1. insert into location(location_id,location_is_deleted,location_name,location_parent_id,location_position_code,location_town_code,location_level_ll_id) values(nextval(‘location_seq‘),false,‘綏德縣‘,39902,‘610826‘,‘000000‘,3);  
    2.                 insert into location(location_id,location_is_deleted,location_name,location_parent_id,location_position_code,location_town_code,location_level_ll_id) values(nextval(‘location_seq‘),false,‘薛家峁鎮‘,40018,‘610826‘,‘101000‘,4);  
    3.                 insert into location(location_id,location_is_deleted,location_name,location_parent_id,location_position_code,location_town_code,location_level_ll_id) values(nextval(‘location_seq‘),false,‘崔家灣鎮‘,40018,‘610826‘,‘102000‘,4);  
    4.                 insert into location(location_id,location_is_deleted,location_name,location_parent_id,location_position_code,location_town_code,location_level_ll_id) values(nextval(‘location_seq‘),false,‘定仙墕鎮‘,40018,‘610826‘,‘103000‘,4);  
    5.                 insert into location(location_id,location_is_deleted,location_name,location_parent_id,location_position_code,location_town_code,location_level_ll_id) values(nextval(‘location_seq‘),false,‘棗林坪鎮‘,40018,‘610826‘,‘104000‘,4);  
    6.                 insert into location(location_id,location_is_deleted,location_name,location_parent_id,location_position_code,location_town_code,location_level_ll_id) values(nextval(‘location_seq‘),false,‘義合鎮‘,40018,‘610826‘,‘105000‘,4);  
    7.                  

JPA hibernate spring repository pgsql java 工程(二):sql檔案匯入資料,測試資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.