From 0fea97f2a102a0cf4af8f726e01bb0181ce79bc8 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Thu, 1 Aug 2024 10:10:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=B9=E6=B3=95=EF=BC=8C?= =?UTF-8?q?=E5=85=81=E8=AE=B8=E6=89=A7=E8=A1=8C=20SQL=20=E5=90=8E=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=9B=B4=E6=96=B0=E5=90=8E=E7=9A=84=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/xyz/zhouxy/jdbc/ResultMap.java | 26 +++++++ .../xyz/zhouxy/jdbc/SimpleJdbcTemplate.java | 71 +++++++++++-------- .../zhouxy/jdbc/SimpleJdbcTemplateTests.java | 21 ++++++ 3 files changed, 87 insertions(+), 31 deletions(-) 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