2017-06-08 50 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一样。如果您需要返回更新的行数,您可以创建包装类,并将其存储并返回。

相关问题