spring--jdbc的批量操作

来源:互联网 发布:cp1308写频软件 编辑:程序博客网 时间:2024/05/20 08:24

 /**
  * 批量更新
  */
 public int[] batchUpdate(final List<User> userList) {
  
  String sql="update user set name=? where id=?";

 

 //BatchPreparedStatementSetter该接口需要有两个方法;


  int[] count=jdbcTempalte.batchUpdate(sql, new BatchPreparedStatementSetter(){

 

   //需要更新的次数
   public int getBatchSize() {
    
    return userList.size();


   }

 

   //为ps赋值
   public void setValues(PreparedStatement ps, int i)


     throws SQLException {
    
    ps.setString(1, userList.get(i).getName());


    ps.setInt(2, userList.get(i).getId());


   }
   
  });
  return count;
 }

原创粉丝点击