1

Spring-JDBC を使用して、ユーザーの Facebook の友達のリストを MySQL データベースに挿入します。

ユーザーの uid を含む最終的な Long と、彼の友人のリストを含む List があります。

私のクエリは次のとおりです。

final String sqlInsert="insert into fb_user_friends(fb_uid,friend_uid) values(?,?)";

SqlParameterSourceUtils を使用してバッチ パラメーターを作成します

SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(friendsList.toArray());

そして、次を使用して挿入を実行します。

int[] insertCounts = this._jdbcTemplate.batchUpdate(sqlInsert,batch);

ここでの問題は、クエリに必要な 2 番目のパラメーターのみがリストに含まれていることです。

friendsList を変更して別の列を追加する必要がありますか、それとも別の方法がありますか?

ありがとう!

4

1 に答える 1

1

これが役立つはずです:http://static.springsource.org/spring/docs/3.0.x/reference/jdbc.html#jdbc-advanced-jdbc


編集:

final Long uid = 1L;
final List<Long> friendList /* = ... */;

int[] updateCounts = jdbcTemplate.batchUpdate(
    "insert into fb_user_friends(fb_uid,friend_uid) values(?,?)",
    new BatchPreparedStatementSetter() {
        public void setValues(final PreparedStatement ps, final int i) throws SQLException {
            ps.setLong(1, uid);
            ps.setLong(2, friendList.get(i));
        }

        public int getBatchSize() {
            return friendList.size();
        }
});

また

final List<User> userList /* = ... */;
SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(userList.toArray());
int[] updateCounts = simpleJdbcTemplate.batchUpdate(
    "insert into fb_user_friends(fb_uid,friend_uid) values(:uid,:friendUid)",
    batch);

@Data // from lombok 
class User {
    private Long uid;
    private Long friendUid;
}

テストされていません。提供されたリンクのサンプルを基にしています

于 2011-06-19T13:18:41.543 に答える