如何将参数列表传递给存储过程并在SQL Server中执行批量插入

mac*_*ers 6 sql sql-server spring stored-procedures jdbc

我正在使用Spring JDBCTemplate连接到SQL Server.

我有一个需要插入SQL Server表的对象列表.

我做的是以下内容:

public void batchInsert(final List<Bean> list) {

    final String sql = "insert into temp"
            + "(id, name, amount, location, time, price) "
            + " values (?, ?, ?, ?, ?, ?)";

    getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {

        @Override
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            Bean vo = list.get(i);
            ps.setString(1, vo.getId());
            ps.setString(2, vo.getName());
            ps.setDouble(3, vo.getAmount());
            ps.setString(4, vo.getLocation());
            ps.setString(5, vo.getTime());
            ps.setDouble(6, vo.getPrice());
        }

        @Override
        public int getBatchSize() {
            return list.size();
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

但是现在,我想将参数List<Bean> list传递给存储过程,该存储过程尽可能高效地处理批量插入.

请问如何实现这个?

Ant*_*nio 0

如果您不能使用表值参数,为什么不在存储过程中使用 xml 参数呢?