標籤:序列 rac repos repo highlight oca location 檔案匯入 nextval
使用jpa儲存查詢資料都很方便,除了在代碼中加入資料外,可以使用sql進行匯入。目前我只會一種方法,把資料集中在一個sql檔案中。
而且資料在匯入中常常具有先後關係,需要用串列的方式匯入。
第一步:配置 需要指定spring boot對資料有create的配置,才能自動載入sql檔案
[java] view plain copy
- spring.jpa.hibernate.ddl-auto=create
第二步:將對應的.sql檔案放入src/main/resources下。我採用的是import.sql檔案,如果有多個sql似乎系統會選擇一個檔案名稱排序靠前的檔案讀取。
第三步:
在sql寫入執行語句:如參考所示就是pgsql的插入語句。nextval(‘location_seq‘)是指定主鍵的值為對應的自增序列的值。對於jpa中定義了自增
序列作為主鍵,如果採用自訂主索引值方法插入資料,並且沒對相應的序列做處理,會在系統調用Repository方法進行操作的時候發生錯誤。
出現無法儲存,更改的情況!
[sql] view plain copy
- 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);
- 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);
- 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);
- 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);
- 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);
- 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);
-
JPA hibernate spring repository pgsql java 工程(二):sql檔案匯入資料,測試資料