Spring BatchSqlUpdate.updateByNamedParam例子

The key is to define the parameters and the sql statement, as follows:

int dstColCount=dstColNamesList.size();
String insSql="insert into "+tableName+"("+dstTableInsColSql+")  values("+dstTableInsValueSql+")";
BatchSqlUpdate bsu = new BatchSqlUpdate ();
bsu.setDataSource(jdbcService.getJdbcTempalte().getDataSource());
bsu.setSql (insSql);
bsu.setBatchSize(1000);
for (int i = 0; i < dstColCount; i++) {
    SqlParameter sp=new SqlParameter(dstColNamesList.get(i),java.sql.Types.VARCHAR);
    bsu.declareParameter (sp);
}
// generates a script into the current database about 
the while (srcRecords.next ()) {                
     for ( int I = 0; I <dstColCount; I ++ ) {
        paramsMap.put(dstColNamesList.get(i), srcRecords.getString(srcColNamesList.get(i)));
    }
    bsu.updateByNamedParam(paramsMap);
}
1) SqlParameter There are many constructors, you can see the official documents specifically 
2) because it is named parameters, the parameters must be defined as the colon sql + name, for example: name,:. Sex
. 3) the SqlParameter name and parameters must be in sql corresponding to
4) updateByNamedParam Map in key names and must sql parameter corresponding to the name
on the efficiency of the test has not been thorough, have the opportunity to do a test to see!

Guess you like

Origin www.cnblogs.com/lzfhope/p/12069758.html