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 {