New to Spring, I am trying to insert a List
into a table. Until now I have been using the SqlParameterSource
for b
There is another way to awoid @SuppressWarnings("unchecked")
.
public static final String INSERT_INTO = "INSERT INTO {0} ({1}) VALUES ({2})";
private NamedParameterJdbcTemplate template;
template.batchUpdate(insertQuery(rowMaps.get(0)), batchArgs(mapRows));
/**
* Create SQL instruction INSERT from Map record
*
* @return literal INSERT INTO [schema].[prefix][table_name] (column1, column2, column3, ...)
* VALUES (value1, value2, value3, ...);
*/
public String insertQuery(Map rowMap) {
String schemaTable = Objects.isNull(getSchema()) ? table : getSchema() + "." + table;
String splittedColumns = String.join(",", rowMap.keySet());
String splittedValues = rowMap.keySet().stream()
.map(s -> ":" + s).collect(Collectors.joining(","));
return MessageFormat.format(INSERT_INTO, schemaTable, splittedColumns, splittedValues);
}
private MapSqlParameterSource[] batchArgs(List
Best regards