⽤JdbcTemplate进⾏批量插⼊或更新操作⽤ 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 = JdbcTemplate().batchUpdate(sql, new MyBatchPreparedStatementSetter(temList));
return ii.length;
}catch (org.springframework.dao.DataAccessException e) {
e.printStackTrace();
throw new Message());
}
}
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 = ((i);
ps.setString(1, ContractCode());
ps.setString(2, CreateDate());
ps.setString(3, EmployeeId());
ps.setString(4, Percent());
}
}jdbctemplate批量insert
⽅法⼆:
/
/插⼊很多书(批量插⼊⽤法)
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 (i).getName();
int (i).getPbYear();
ps.setString(1, name);
ps.setInt(2, pbYear);
}
public int getBatchSize()
{
return tempBook.size();
}
});
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论