From 6947846c842adb6d044222b5c48f03680291fcb0 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 7 Jun 2023 11:28:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=82=E6=95=B0=E7=9A=84=E6=9E=84=E5=BB=BA?= =?UTF-8?q?=E5=92=8C=E8=BE=93=E5=87=BA=E7=A7=BB=E5=8A=A8=E5=88=B0=20Simple?= =?UTF-8?q?JdbcTemplate=20=E4=B8=AD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/jdbc/SimpleJdbcTemplate.java | 57 +++++++++++++++++++ .../commons/util/SimpleJdbcTemplateTests.java | 22 +++---- 2 files changed, 68 insertions(+), 11 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/jdbc/SimpleJdbcTemplate.java b/src/main/java/xyz/zhouxy/plusone/commons/jdbc/SimpleJdbcTemplate.java index 2f2345e..ae96a16 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/jdbc/SimpleJdbcTemplate.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/jdbc/SimpleJdbcTemplate.java @@ -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 List buildBatchParams(final Collection c, Function 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 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"); } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/SimpleJdbcTemplateTests.java b/src/test/java/xyz/zhouxy/plusone/commons/util/SimpleJdbcTemplateTests.java index 8502654..1cc22af 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/util/SimpleJdbcTemplateTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/util/SimpleJdbcTemplateTests.java @@ -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 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 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);