From 10039cfdc49e16f21514f2ad934f169dda9844aa Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 28 Jun 2023 11:15:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20ResultMap=20=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhouxy/plusone/commons/jdbc/ResultMap.java | 2 +- .../commons/jdbc/SimpleJdbcTemplate.java | 17 +++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/jdbc/ResultMap.java b/src/main/java/xyz/zhouxy/plusone/commons/jdbc/ResultMap.java index de89740..1f078c8 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/jdbc/ResultMap.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/jdbc/ResultMap.java @@ -24,5 +24,5 @@ import com.google.common.annotations.Beta; @Beta @FunctionalInterface public interface ResultMap { - T map(ResultSet rs) throws SQLException; + T map(ResultSet rs, int rowNumber) throws SQLException; } 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 4234ff3..0600529 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/jdbc/SimpleJdbcTemplate.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/jdbc/SimpleJdbcTemplate.java @@ -100,8 +100,9 @@ public class SimpleJdbcTemplate { } try (ResultSet rs = stmt.executeQuery()) { List result = new ArrayList<>(); + int rowNumber = 0; while (rs.next()) { - T e = resultMap.map(rs); + T e = resultMap.map(rs, rowNumber++); result.add(e); } return result; @@ -114,7 +115,7 @@ public class SimpleJdbcTemplate { return (list.isEmpty()) ? Optional.empty() : Optional.ofNullable(list.get(0)); } - public static final ResultMap> mapResultMap = rs -> { + public static final ResultMap> mapResultMap = (rs, rowNumber) -> { Map result = new HashMap<>(); ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount(); @@ -133,7 +134,7 @@ public class SimpleJdbcTemplate { return queryFirst(sql, params, mapResultMap); } - public static final ResultMap recordResultMap = rs -> { + public static final ResultMap recordResultMap = (rs, rowNumber) -> { DbRecord result = new DbRecord(); ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount(); @@ -153,26 +154,26 @@ public class SimpleJdbcTemplate { } public Optional queryToString(String sql, Object... params) throws SQLException { - return queryFirst(sql, params, (ResultSet rs) -> rs.getString(1)); + return queryFirst(sql, params, (ResultSet rs, int rowNumber) -> rs.getString(1)); } public OptionalInt queryToInt(String sql, Object... params) throws SQLException { - Optional result = queryFirst(sql, params, (ResultSet rs) -> rs.getInt(1)); + Optional result = queryFirst(sql, params, (ResultSet rs, int rowNumber) -> rs.getInt(1)); return OptionalUtil.toOptionalInt(result); } public OptionalLong queryToLong(String sql, Object... params) throws SQLException { - Optional result = queryFirst(sql, params, (ResultSet rs) -> rs.getLong(1)); + Optional result = queryFirst(sql, params, (ResultSet rs, int rowNumber) -> rs.getLong(1)); return OptionalUtil.toOptionalLong(result); } public OptionalDouble queryToDouble(String sql, Object... params) throws SQLException { - Optional result = queryFirst(sql, params, (ResultSet rs) -> rs.getDouble(1)); + Optional result = queryFirst(sql, params, (ResultSet rs, int rowNumber) -> rs.getDouble(1)); return OptionalUtil.toOptionalDouble(result); } public Optional queryToBigDecimal(String sql, Object... params) throws SQLException { - return queryFirst(sql, params, (ResultSet rs) -> rs.getBigDecimal(1)); + return queryFirst(sql, params, (ResultSet rs, int rowNumber) -> rs.getBigDecimal(1)); } public int update(String sql, Object... params) throws SQLException {