diff --git a/src/main/java/xyz/zhouxy/jdbc/ResultMap.java b/src/main/java/xyz/zhouxy/jdbc/ResultMap.java index 532a3e3..005efbd 100644 --- a/src/main/java/xyz/zhouxy/jdbc/ResultMap.java +++ b/src/main/java/xyz/zhouxy/jdbc/ResultMap.java @@ -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 map(ResultSet rs, int rowNumber) throws SQLException; + + public static final ResultMap> mapResultMap = (rs, rowNumber) -> { + Map 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 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; + }; } diff --git a/src/main/java/xyz/zhouxy/jdbc/SimpleJdbcTemplate.java b/src/main/java/xyz/zhouxy/jdbc/SimpleJdbcTemplate.java index 4840f67..cb3c00c 100644 --- a/src/main/java/xyz/zhouxy/jdbc/SimpleJdbcTemplate.java +++ b/src/main/java/xyz/zhouxy/jdbc/SimpleJdbcTemplate.java @@ -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> mapResultMap = (rs, rowNumber) -> { - Map 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> query(String sql, Object... params) throws SQLException { - return query(sql, params, mapResultMap); + return query(sql, params, ResultMap.mapResultMap); } public Optional> queryFirst(String sql, Object... params) throws SQLException { - return queryFirst(sql, params, mapResultMap); + return queryFirst(sql, params, ResultMap.mapResultMap); } - public static final ResultMap 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 queryToRecordList(String sql, Object... params) throws SQLException { - return query(sql, params, recordResultMap); + return query(sql, params, ResultMap.recordResultMap); } public Optional queryFirstRecord(String sql, Object... params) throws SQLException { - return queryFirst(sql, params, recordResultMap); + return queryFirst(sql, params, ResultMap.recordResultMap); } public Optional 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 List update(@Nonnull String sql, @Nonnull Object[] params, ResultMap 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 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 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 -> { diff --git a/src/test/java/xyz/zhouxy/jdbc/SimpleJdbcTemplateTests.java b/src/test/java/xyz/zhouxy/jdbc/SimpleJdbcTemplateTests.java index 7b010e4..2df1a2d 100644 --- a/src/test/java/xyz/zhouxy/jdbc/SimpleJdbcTemplateTests.java +++ b/src/test/java/xyz/zhouxy/jdbc/SimpleJdbcTemplateTests.java @@ -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> 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> 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