批量保存数据代码
public int executeBatch(String sql, List<?> args) {
if (null == args || 0 == args.size()) {
throw new BaseException("参数为空");
}
PreparedStatement statement = null;
try {
statement = connection.prepareStatement(sql);
int index = 0;
for (int i = 0; i < args.size(); i++) {
index++;
Map<String, ?> maps = SysContext.objectToMap(args.get(i));
int j = 1;
List<String> lists = SQLHelper.getInsertRows(sql);
for (String key : lists) {
statement.setObject(j++, maps.get(StringUtil.convertHump(key)));
}
statement.addBatch();
if (commit_count == index) {
statement.executeBatch();
statement.clearBatch();
index = 0;
}
}
if (0 != index) {
statement.executeBatch();
statement.clearBatch();
}
} catch (SQLException e) {
e.printStackTrace();
throw new BaseException("批量执行保存SQL异常:" + e.getMessage());
} finally {
if (null != statement) {
try {
statement.close();
statement = null;
} catch (Exception e) {
}
}
}
return 1;
}
评论区