不废话,直接上代码
给自己做记录
给自己做记录
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
/**
* 批量更新或者插入
* 参数:sql为更新或者插入语句 list 是要进行批量处理的数据集合
* msg 是定位是插入还是更新, username可不考虑 业务需要
*/
public String updateprojectdata(String sql, List list, String msg,String Username) {
JdbcTemplate jdbctemplate = getJdbcTemplate();
int[] sucess = jdbctemplate.batchUpdate(sql,
new MyBatchPreparedStatementSetter(list,msg,Username));
if (sucess.length == 0) {
return "导入失败";
} else {
return "导入成功";
}
}
/**
* 处理批量插入或更新的回调类
*
*/
private class MyBatchPreparedStatementSetter implements
BatchPreparedStatementSetter {
final List temList;
final String msg;
final String Username;
/** 通过构造函数把要插入的数据传递进来处理 */
public MyBatchPreparedStatementSetter(List list, String msg,String Username) {
temList = list;
this.msg = msg;
this.Username=Username;
}
public int getBatchSize() {
return temList.size();
}
public void setValues(PreparedStatement ps, int i) throws SQLException {
ProjectData pd = (ProjectData) temList.get(i);
if (msg.equals("insert")) {
ps.setInt(1, pd.getI_cid().intValue());
ps.setString(2, pd.getI_subcomp_id());
} else if (msg.equals("update")) {
ps.setString(1, pd.getI_country_id());
ps.setString(2, pd.getV_cellname_cn());
}
}
}