关键在于定义参数和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中参数名称对应
效率上尚未深入测试,有机会做个测试看看!

相关文章:

  • 2021-11-29
  • 2021-12-30
  • 2021-11-21
  • 2021-07-22
  • 2021-07-16
  • 2021-05-19
  • 2021-06-19
  • 2021-11-01
猜你喜欢
  • 2021-12-19
  • 2021-06-29
  • 2021-12-14
  • 2021-08-23
  • 2021-05-02
  • 2021-08-25
  • 2022-01-11
相关资源
相似解决方案