2013-07-24 61 views
1

如何在春季JDBC批量更新中设置批量大小以提高性能。以下是我的代码片段。在春季设置批量大小JDBC批量更新

公共无效insertListOfPojos(最终名单myPojoList){

String sql = "INSERT INTO " + "Student " + "(age,name) " + "VALUES " 
     + "(?,?)"; 
try { 
    jdbcTemplateObject.batchUpdate(sql, 
      new BatchPreparedStatementSetter() { 

       @Override 
       public void setValues(PreparedStatement ps, int i) 
         throws SQLException { 

        Student myPojo = myPojoList.get(i); 
        ps.setString(2, myPojo.getName()); 
        ps.setInt(1, myPojo.getAge()); 

       } 

       @Override 
       public int getBatchSize() { 
        return myPojoList.size(); 
       } 
      }); 
} catch (Exception e) { 
    System.out.println("Exception"); 

}}   I just read that in hibernate you can provide your batch size in the configuration xml. Like <property name="hibernate.jdbc.batch_size" value="100"/> . 

我们有在弹簧JDBC类似的东西。

回答

1

没有选择jdbc,看起来像Hibernate;我认为在准备连接字符串时必须查看指定的RDBMS供应商驱动程序选项。

关于你的代码,你必须使用

BatchPreparedStatementSetter.getBatchSize()

JdbcTemplate.batchUpdate(String sql, final Collection<T> batchArgs, final int batchSize, final ParameterizedPreparedStatementSetter<T> pss)