标签:
JdbcTemplate
public int insertContractAch(List list) throws DataAccessException {
final List temList = list;
String sql = "insert into contract_ach_t " +
" values(?,to_date(?,‘yyyy-mm-dd‘),?,?) ";
try{
int[] ii = this.getJdbcTemplate().batchUpdate(sql, new MyBatchPreparedStatementSetter(temList));
return ii.length;
}catch (org.springframework.dao.DataAccessException e) {
e.printStackTrace();
throw new DataAccessException(e.getMessage());
}
}
private class MyBatchPreparedStatementSetter implements BatchPreparedStatementSetter{
final List temList;
public MyBatchPreparedStatementSetter(List list){
temList = list;
}
public int getBatchSize() {
return temList.size();
}
public void setValues(PreparedStatement ps, int i)
throws SQLException {
ContractAchVO contractAchVO = (ContractAchVO)temList.get(i);
ps.setString(1, contractAchVO.getContractCode());
ps.setString(2, contractAchVO.getCreateDate());
ps.setString(3, contractAchVO.getEmployeeId());
ps.setString(4, contractAchVO.getPercent());
}
}
2.JPA data,Hibernate的思路
public void batchSave(String sql,int batchSize,List<Object[]> list) throws SQLException{
if (CollectionUtils.isEmpty(list)){
return;
}
SessionImplementor session =em.unwrap(SessionImplementor.class);
Connection connection = session.connection();
PreparedStatement st = connection.prepareStatement(sql);
for(int i=0;i<list.size();i++){
Object[] params = list.get(i);
if (ArrayUtils.isEmpty(params)){
continue;
}
for (int j=1;j<=params.length;j++){
if(params[j-1] instanceof String)
st.setString(j,String.valueOf(params[j-1]));
else if(params[j-1] instanceof Date)
st.setDate(j, new java.sql.Date( ((Date) params[j-1]).getTime()));
else if(params[j-1] instanceof BigDecimal)
st.setBigDecimal(j, (BigDecimal) params[j-1]);
else if(params[j-1] ==null)
st.setNull(j, java.sql.Types.VARCHAR);
else
st.setString(j, String.valueOf(params[j-1]));
}
st.addBatch();
if(i > 0 && i%batchSize==0){
st.executeBatch();
st.clearBatch();
}
}
st.executeBatch();
}
标签:
原文地址:http://my.oschina.net/grossofans/blog/517428