forked from plusone/plusone-commons
参数的构建和输出移动到 SimpleJdbcTemplate 中。
parent
b2374e810b
commit
6947846c84
|
@ -23,13 +23,17 @@ import java.sql.ResultSet;
|
|||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalDouble;
|
||||
import java.util.OptionalInt;
|
||||
import java.util.OptionalLong;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
|
@ -45,6 +49,59 @@ public class SimpleJdbcTemplate {
|
|||
return new JdbcExecutorBak(conn);
|
||||
}
|
||||
|
||||
public static Object[] buildParams(final Object... params) {
|
||||
return Arrays.stream(params)
|
||||
.map(p -> {
|
||||
if (p instanceof Optional) {
|
||||
return OptionalUtil.orElseNull((Optional<?>) p);
|
||||
}
|
||||
if (p instanceof OptionalInt) {
|
||||
OptionalInt _p = ((OptionalInt) p);
|
||||
return _p.isPresent() ? _p.getAsInt() : null;
|
||||
}
|
||||
if (p instanceof OptionalLong) {
|
||||
OptionalLong _p = ((OptionalLong) p);
|
||||
return _p.isPresent() ? _p.getAsLong() : null;
|
||||
}
|
||||
if (p instanceof OptionalDouble) {
|
||||
OptionalDouble _p = ((OptionalDouble) p);
|
||||
return _p.isPresent() ? _p.getAsDouble() : null;
|
||||
}
|
||||
return p;
|
||||
})
|
||||
.toArray();
|
||||
}
|
||||
|
||||
public static <T> List<Object[]> buildBatchParams(final Collection<T> c, Function<T, Object[]> function) {
|
||||
return c.stream().map(function).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static String paramsToString(Object[] params) {
|
||||
return Arrays.toString(params);
|
||||
}
|
||||
|
||||
public static String paramsToString(final Collection<Object[]> params) {
|
||||
if (params == null) {
|
||||
return "null";
|
||||
}
|
||||
if (params.isEmpty()) {
|
||||
return "[]";
|
||||
}
|
||||
int iMax = params.size() - 1;
|
||||
StringBuilder b = new StringBuilder();
|
||||
b.append('[');
|
||||
int i = 0;
|
||||
for (Object[] p : params) {
|
||||
b.append(Arrays.toString(p));
|
||||
if (i == iMax) {
|
||||
return b.append(']').toString();
|
||||
}
|
||||
b.append(',');
|
||||
i++;
|
||||
}
|
||||
return b.append(']').toString();
|
||||
}
|
||||
|
||||
private SimpleJdbcTemplate() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
|
|
|
@ -55,7 +55,7 @@ class SimpleJdbcTemplateTests {
|
|||
@Test
|
||||
void testQuery() throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
Object[] params = MoreArrays.asObjectArray("501533", "501554", "544599");
|
||||
Object[] params = SimpleJdbcTemplate.buildParams("501533", "501554", "544599");
|
||||
String sql = newSql()
|
||||
.SELECT("*")
|
||||
.FROM("test_table")
|
||||
|
@ -103,16 +103,16 @@ class SimpleJdbcTemplateTests {
|
|||
}
|
||||
|
||||
final String sql = "INSERT INTO test_table(id, created_by, create_time, updated_by, update_time, status) VALUES(?, ?, ?, ?, ?, ?)";
|
||||
final List<Object[]> params = recordList.stream()
|
||||
.map(r -> new Object[] {
|
||||
OptionalUtil.orElseNull(r.getValueAsString("id")),
|
||||
OptionalUtil.orElseNull(r.getValueAsString("created_by")),
|
||||
OptionalUtil.orElseNull(r.getValueAsString("create_time")),
|
||||
OptionalUtil.orElseNull(r.getValueAsString("updated_by")),
|
||||
OptionalUtil.orElseNull(r.getValueAsString("update_time")),
|
||||
OptionalUtil.orElseNull(r.getValueAsString("status"))
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
final List<Object[]> params = SimpleJdbcTemplate.buildBatchParams(
|
||||
recordList,
|
||||
r -> SimpleJdbcTemplate.buildParams(
|
||||
r.getValueAsString("id"),
|
||||
r.getValueAsString("created_by"),
|
||||
r.getValueAsString("create_time"),
|
||||
r.getValueAsString("updated_by"),
|
||||
r.getValueAsString("update_time"),
|
||||
r.getValueAsString("status")));
|
||||
log.info("params: {}", SimpleJdbcTemplate.paramsToString(params));
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
SimpleJdbcTemplate.connect(conn).tx(() -> {
|
||||
SimpleJdbcTemplate.connect(conn).batchUpdate(sql, params, 20);
|
||||
|
|
Loading…
Reference in New Issue