2017-06-08 47 views
0

我需要在我的spring boot webapp中執行數據庫插入。到目前爲止,我所擁有的是一系列體育比賽和各自的信息。在一個函數中使用JDBC執行數據插入到多個表中

一場比賽可以在同一天的不同時間舉行。 拳頭惱人的是我如何存儲這個時間是DB。這裏需要一張新桌子嗎? (我創建了一個日期表

我搜索周圍,我仍然不知道如何的 競爭信息的插入和它的日期在同一時間結合起來,我插入功能。

我的插入功能需要一些作品,我需要somme的幫助。

answer是好的,但它並沒有填補我的要求

我的數據基礎架構:

CREATE TABLE competition ( 
    competition_id integer PRIMARY KEY, 
    nom varchar(128) NOT NULL, 
); 


CREATE TABLE date ( 
    id integer PRIMARY KEY, 
    date_time timestamptz, 
    competition_id integer REFERENCES competition (competition_id) 
); 

JSON數據:

{ 
    "id": "420", 
    "name": "SOCCER", 
    "dates": [ 
     "2016-05-12T03:00:00.000Z" 
     "2016-05-12T04:00:00.000Z" 
     "2016-05-12T05:00:00.000Z" 
    ] 
}, 
{ 
    "id": "220", 
    "name": "BASKETBALL", 
    "dates": [ 
     "2016-05-12T03:00:00.000Z" 
     "2016-05-12T04:00:00.000Z" 
    ] 
} 

我的競賽級別:

public class Competition{ 
    private int id; 
    private String name; 
    private String[] dates; 
    // setters ... getters 
} 

功能插入數據:

private static final String INSERT_STMT = 
     " insert into competition (id, name)" 
    + " values (?, ?)" 
    ; 


public int insert(Competition competition) { 
    return jdbcTemplate.update(conn -> { 
     PreparedStatement ps = conn.prepareStatement(INSERT_STMT); 
     ps.setInt(1, competition.getId()); 
     ps.setString(2, competition.getName()); 
     return ps; 
    }); 


    // insert also in date table ??? 
    } 

回答

1

首先我會做的日期 - 表自動遞增的ID,所以你不必給一個ID爲每一個日期,這個基地的查詢去:

private static final String INSERT_DATES = "INSERT INTO date (date_time, competition_id) VALUES "; 

,再建這樣的陳述:

public int insert(Competition competition) { 
    // All local variables must be final because the lambdas will be executed at a undefined time 
    final int id = competition.getId(); 
    final String name = competition.getName(); 

    final String[] dates = competition.getDates(); 
    final String dateValueStr = String.join(", ", Collections.nCopies(dates.length, "(?, ?)")); 

    // Execute Updates 
    int updatedRows1 = jdbcTemplate.update(conn -> { 
     PreparedStatement ps = conn.prepareStatement(INSERT_STMT); 
     ps.setInt(1, id); 
     ps.setString(2, name); 
     return ps; 
    }); 

    if (updatedRows1 < 1) 
    { 
     // Something went wrong 
     return -1; 
    } 

    int updatedRows2 = jdbcTemplate.update(conn -> { 
     PreparedStatement ps = conn.prepareStatement(INSERT_DATES + dateValueStr); 

     int idx = 1; 
     for (String date : dates) 
     { 
      ps.setString(idx, date); // date_time 
      idx++; 
      ps.setInt(idx, competitionID); // competition_id 
      idx++; 
     } 

     return ps; 
    }); 

    if (updatedRows2 < 1) 
    { 
     // Something went wrong 
     // Rollback the first insert, idk how its made 
     return -1; 
    } 

    return updatedRows1 + updatedRows2; 
} 
+0

所以,如果我添加這在我的插入函數,它不會影響第一條語句。他們會一個接一個地執行。 –

+0

您應該在兩次調用jdbcTemplate.update的過程中執行此操作。 每個查詢一個調用。 –

+0

您的意思是,我需要構建兩個插入函數,或者只保留一個,並且對jdbcTemplate.update執行兩次調用。 –

2

首先,如果你需要數據的一致性,那麼你應該換用交易插入語句。爲了將數據插入到多個表中,您應該執行多個插入語句,就像使用sql一樣。如果您需要返回更新的行數,您可以創建包裝類,並將其存儲並返回。

相關問題