如何使用需要2个参数的查询进行batchUpdate,并且只有其中一个存储在列表中

我使用Spring-JDBC在我的MySQL数据库中为用户插入facebook好友列表。

我有一个包含用户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); 

这里的问题是列表只包含查询所需的第二个参数。

我是否必须修改friendsList以将其添加到另一列或是否有其他方法?

谢谢!

这应该有所帮助: http : //static.springsource.org/spring/docs/3.0.x/reference/jdbc.html#jdbc-advanced-jdbc


编辑:

 final Long uid = 1L; final List 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 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; } 

未经测试,改编自提供的链接样本