spring给我们提供了jdbc的统一封装,和其它的orm不同,它是对jdbc api的封装,相当于我们自己写的java bean,而没有对对象模型进行封装(比如将po封装为pojo)。它保持了jdbc数据对象的原始性,具有极大程度的灵活性,其性能也极高。
public interface BatchPreparedStatementSetter{
public int getBatchSize();
public void setValues(PreparedStatement ps,int i);
}
public int getBatchSize();用来返回批次的大小
- public void batchInsertRows(String sql,final List<Object[]> dataSet) throws Exception{
- BatchPreparedStatementSetter setter=new BatchPreparedStatementSetter(){
- public int getBatchSize(){
- return dataSet.size();
- }
- public void setValues(PreparedStatement ps,int i){
- Object[] obj = dataSet.get(i);
- int nextId = getNextId();
- try{
- ps.setLong(1,nextId);
- ps.setLong(2,Integer.parseInt(obj[0].toString()));
- ps.setLong(3,Integer.parseInt(obj[1].toString()));
- ps.setString(4,(String)obj[2]);}
- catch(Exception e){
- e.printStackTrace();
- }
- }
- };
- jdbcTemplate.batchUpdate(sql,setter);
- }
- 转自:http://blog.csdn.net/zhouzheng034/article/details/7843707