修改 ResultMap 接口。

feature/net-util
ZhouXY108 2023-06-28 11:15:20 +08:00
parent 9a078396cd
commit 10039cfdc4
2 changed files with 10 additions and 9 deletions

View File

@ -24,5 +24,5 @@ import com.google.common.annotations.Beta;
@Beta @Beta
@FunctionalInterface @FunctionalInterface
public interface ResultMap<T> { public interface ResultMap<T> {
T map(ResultSet rs) throws SQLException; T map(ResultSet rs, int rowNumber) throws SQLException;
} }

View File

@ -100,8 +100,9 @@ public class SimpleJdbcTemplate {
} }
try (ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
List<T> result = new ArrayList<>(); List<T> result = new ArrayList<>();
int rowNumber = 0;
while (rs.next()) { while (rs.next()) {
T e = resultMap.map(rs); T e = resultMap.map(rs, rowNumber++);
result.add(e); result.add(e);
} }
return result; return result;
@ -114,7 +115,7 @@ public class SimpleJdbcTemplate {
return (list.isEmpty()) ? Optional.empty() : Optional.ofNullable(list.get(0)); return (list.isEmpty()) ? Optional.empty() : Optional.ofNullable(list.get(0));
} }
public static final ResultMap<Map<String, Object>> mapResultMap = rs -> { public static final ResultMap<Map<String, Object>> mapResultMap = (rs, rowNumber) -> {
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
ResultSetMetaData metaData = rs.getMetaData(); ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount(); int columnCount = metaData.getColumnCount();
@ -133,7 +134,7 @@ public class SimpleJdbcTemplate {
return queryFirst(sql, params, mapResultMap); return queryFirst(sql, params, mapResultMap);
} }
public static final ResultMap<DbRecord> recordResultMap = rs -> { public static final ResultMap<DbRecord> recordResultMap = (rs, rowNumber) -> {
DbRecord result = new DbRecord(); DbRecord result = new DbRecord();
ResultSetMetaData metaData = rs.getMetaData(); ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount(); int columnCount = metaData.getColumnCount();
@ -153,26 +154,26 @@ public class SimpleJdbcTemplate {
} }
public Optional<String> queryToString(String sql, Object... params) throws SQLException { public Optional<String> 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 { public OptionalInt queryToInt(String sql, Object... params) throws SQLException {
Optional<Integer> result = queryFirst(sql, params, (ResultSet rs) -> rs.getInt(1)); Optional<Integer> result = queryFirst(sql, params, (ResultSet rs, int rowNumber) -> rs.getInt(1));
return OptionalUtil.toOptionalInt(result); return OptionalUtil.toOptionalInt(result);
} }
public OptionalLong queryToLong(String sql, Object... params) throws SQLException { public OptionalLong queryToLong(String sql, Object... params) throws SQLException {
Optional<Long> result = queryFirst(sql, params, (ResultSet rs) -> rs.getLong(1)); Optional<Long> result = queryFirst(sql, params, (ResultSet rs, int rowNumber) -> rs.getLong(1));
return OptionalUtil.toOptionalLong(result); return OptionalUtil.toOptionalLong(result);
} }
public OptionalDouble queryToDouble(String sql, Object... params) throws SQLException { public OptionalDouble queryToDouble(String sql, Object... params) throws SQLException {
Optional<Double> result = queryFirst(sql, params, (ResultSet rs) -> rs.getDouble(1)); Optional<Double> result = queryFirst(sql, params, (ResultSet rs, int rowNumber) -> rs.getDouble(1));
return OptionalUtil.toOptionalDouble(result); return OptionalUtil.toOptionalDouble(result);
} }
public Optional<BigDecimal> queryToBigDecimal(String sql, Object... params) throws SQLException { public Optional<BigDecimal> 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 { public int update(String sql, Object... params) throws SQLException {