Spring BatchSqlUpdate.updateByNamedParam例子

关键在于定义参数和sql语句,代码如下:

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);
}
// 生成插入到当前数据库的有关脚本
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有多种构造函数,具体可以看官方文档
2)由于是命名参数,所以sql的参数必须定义为 冒号+名称,例如:name,:sex.
3)SqlParameter中参数的名称必须和sql中的对应
4)updateByNamedParam中Map的key名称必须和 sql中参数名称对应
效率上尚未深入测试,有机会做个测试看看!

posted @ 2019-12-19 19:44  正在战斗中  阅读(1012)  评论(0编辑  收藏  举报