jdbc批量插入数据

//插入很多书(批量插入用法)
public void insertBooks(List<Book> book)
{
  final List<Book> tempBook=book;
  String sql="insert into book(name,pbYear) values(?,?)";
  jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter()
  {
   public void setValues(PreparedStatement ps,int i)throws SQLException
   {
    String name=tempBook.get(i).getName();
    int pbYear=tempBook.get(i).getPbYear();
    ps.setString(1, name);
    ps.setInt(2, pbYear);
   }
   public int getBatchSize()
   {
    return tempBook.size();
   }
  });
   
}

posted @ 2019-07-29 09:02  汤姆汀  阅读(915)  评论(0编辑  收藏  举报