添加方法,允许执行 SQL 后获取更新后的数据。

dev
ZhouXY108 2024-08-01 10:10:59 +08:00
parent a135292909
commit 0fea97f2a1
3 changed files with 87 additions and 31 deletions

View File

@ -19,10 +19,36 @@ package xyz.zhouxy.jdbc;
import com.google.common.annotations.Beta;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
@Beta
@FunctionalInterface
public interface ResultMap<T> {
T map(ResultSet rs, int rowNumber) throws SQLException;
public static final ResultMap<Map<String, Object>> mapResultMap = (rs, rowNumber) -> {
Map<String, Object> result = new HashMap<>();
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
String colName = metaData.getColumnName(i);
result.put(colName, rs.getObject(colName));
}
return result;
};
public static final ResultMap<DbRecord> recordResultMap = (rs, rowNumber) -> {
DbRecord result = new DbRecord();
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
String colName = metaData.getColumnName(i);
result.put(colName, rs.getObject(colName));
}
return result;
};
}

View File

@ -20,13 +20,12 @@ import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@ -36,7 +35,7 @@ import java.util.OptionalLong;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.commons.lang3.ArrayUtils;
import javax.annotation.Nonnull;
import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions;
@ -110,42 +109,20 @@ public class SimpleJdbcTemplate {
return query(sql, params, resultMap).stream().findFirst();
}
public static final ResultMap<Map<String, Object>> mapResultMap = (rs, rowNumber) -> {
Map<String, Object> result = new HashMap<>();
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
String colName = metaData.getColumnName(i);
result.put(colName, rs.getObject(colName));
}
return result;
};
public List<Map<String, Object>> query(String sql, Object... params) throws SQLException {
return query(sql, params, mapResultMap);
return query(sql, params, ResultMap.mapResultMap);
}
public Optional<Map<String, Object>> queryFirst(String sql, Object... params) throws SQLException {
return queryFirst(sql, params, mapResultMap);
return queryFirst(sql, params, ResultMap.mapResultMap);
}
public static final ResultMap<DbRecord> recordResultMap = (rs, rowNumber) -> {
DbRecord result = new DbRecord();
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
String colName = metaData.getColumnName(i);
result.put(colName, rs.getObject(colName));
}
return result;
};
public List<DbRecord> queryToRecordList(String sql, Object... params) throws SQLException {
return query(sql, params, recordResultMap);
return query(sql, params, ResultMap.recordResultMap);
}
public Optional<DbRecord> queryFirstRecord(String sql, Object... params) throws SQLException {
return queryFirst(sql, params, recordResultMap);
return queryFirst(sql, params, ResultMap.recordResultMap);
}
public Optional<String> queryToString(String sql, Object... params) throws SQLException {
@ -178,6 +155,36 @@ public class SimpleJdbcTemplate {
}
}
/**
* SQL
*
* @param sql SQL
* @param params
* @param resultMap
*
* @return
* @throws SQLException SQL
*/
public <T> List<T> update(@Nonnull String sql, @Nonnull Object[] params, ResultMap<T> resultMap)
throws SQLException {
Preconditions.checkNotNull(sql, "The sql could not be null.");
Preconditions.checkNotNull(params, "The params could not be null.");
Preconditions.checkNotNull(resultMap, "The resultMap could not be null.");
final List<T> result = new ArrayList<>();
try (PreparedStatement stmt = this.conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
fillStatement(stmt, params);
stmt.executeUpdate();
try (ResultSet generatedKeys = stmt.getGeneratedKeys();) {
int rowNumber = 0;
while (generatedKeys.next()) {
T e = resultMap.map(generatedKeys, rowNumber++);
result.add(e);
}
}
return result;
}
}
public int[] batchUpdate(String sql, Collection<Object[]> params, int batchSize) throws SQLException {
int executeCount = params.size() / batchSize;
executeCount = (params.size() % batchSize == 0) ? executeCount : (executeCount + 1);
@ -247,9 +254,11 @@ public class SimpleJdbcTemplate {
public static class ParamBuilder {
public static final Object[] EMPTY_OBJECT_ARRAY = {};
public static Object[] buildParams(final Object... params) {
if (ArrayUtils.isEmpty(params)) {
return ArrayUtils.EMPTY_OBJECT_ARRAY;
if (ArrayTools.isNullOrEmpty(params)) {
return EMPTY_OBJECT_ARRAY;
}
return Arrays.stream(params)
.map(param -> {

View File

@ -9,6 +9,7 @@ import static xyz.zhouxy.plusone.commons.sql.JdbcSql.IN;
import java.sql.Connection;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@ -73,6 +74,26 @@ class SimpleJdbcTemplateTests {
}
}
@Test
void testInsert() throws SQLException {
try (Connection conn = dataSource.getConnection()) {
List<Map<String, Object>> keys = new ArrayList<>();
SimpleJdbcTemplate.connect(conn).update("INSERT INTO base_table(status, created_by) VALUES (?, ?)",
buildParams(1, 886), keys);
log.info("keys: {}", keys);
}
}
@Test
void testUpdate() throws SQLException {
try (Connection conn = dataSource.getConnection()) {
List<Map<String, Object>> keys = new ArrayList<>();
SimpleJdbcTemplate.connect(conn).update("UPDATE base_table SET status = ?, version = version + 1, update_time = now(), updated_by = ? WHERE id = ?",
buildParams(2, 886, 9), keys);
log.info("keys: {}", keys);
}
}
final IdWorker idGenerator = IdGenerator.getSnowflakeIdGenerator(0);
@Test