From 9d5fd0ff56d7e8d6e6bf7f8cdff63fcce793b041 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sun, 25 Jun 2023 09:14:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20IAtom=20=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/jdbc/SimpleJdbcTemplate.java | 10 +-- .../commons/util/SimpleJdbcTemplateTests.java | 74 ++++--------------- 2 files changed, 19 insertions(+), 65 deletions(-) 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 1885972..fb0925f 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/jdbc/SimpleJdbcTemplate.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/jdbc/SimpleJdbcTemplate.java @@ -232,11 +232,11 @@ public class SimpleJdbcTemplate { } } - public void tx(final IAtom tx) throws Exception { - Assert.notNull(tx, "Tx can not be null."); + public void tx(final IAtom atom) throws SQLException, T { + Assert.notNull(atom, "Tx can not be null."); try { this.conn.setAutoCommit(false); - tx.execute(); + atom.execute(); conn.commit(); conn.setAutoCommit(true); } catch (Exception e) { @@ -247,9 +247,9 @@ public class SimpleJdbcTemplate { } @FunctionalInterface - public static interface IAtom { + public static interface IAtom { @SuppressWarnings("all") - void execute() throws Exception; + void execute() throws SQLException, T; } } } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/SimpleJdbcTemplateTests.java b/src/test/java/xyz/zhouxy/plusone/commons/util/SimpleJdbcTemplateTests.java index 8e38389..328e7d8 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/util/SimpleJdbcTemplateTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/util/SimpleJdbcTemplateTests.java @@ -2,18 +2,13 @@ package xyz.zhouxy.plusone.commons.util; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static xyz.zhouxy.plusone.commons.jdbc.JdbcSql.*; +import static xyz.zhouxy.plusone.commons.jdbc.JdbcSql.NOT_IN; -import java.io.File; -import java.io.IOException; -import java.nio.charset.StandardCharsets; import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.Optional; -import java.util.stream.Collectors; import javax.sql.DataSource; @@ -21,8 +16,6 @@ import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.base.Splitter; -import com.google.common.io.Files; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; @@ -75,58 +68,19 @@ class SimpleJdbcTemplateTests { } @Test - void testSaveTxt() throws IOException, SQLException { - File file = new File("C:\\Users\\zhouxy\\Desktop", "Untitled-1.txt"); - assertTrue(file.exists()); - List recordStrList = Files.readLines(file, StandardCharsets.UTF_8); - if (MoreCollections.isEmpty(recordStrList)) { - log.info("file is empty."); - return; - } - - List recordList = new ArrayList<>(recordStrList.size()); - for (String rStr : recordStrList) { - List props = Splitter.on("|").splitToList(rStr) - .stream() - .map(s -> { - if (s == null) { - return null; - } - s = s.trim(); - return ("".equals(s) || "null".equals(s) || "NULL".equals(s)) ? null : s; - }) - .collect(Collectors.toList()); - DbRecord r = new DbRecord(); - for (int i = 0; i < cStruct.length; i++) { - r.put(cStruct[i], props.get(i)); - } - recordList.add(r); - } - - final String sql = "INSERT INTO test_table(id, created_by, create_time, updated_by, update_time, status) VALUES(?, ?, ?, ?, ?, ?)"; - final List params = SimpleJdbcTemplate.buildBatchParams( - recordList, - r -> SimpleJdbcTemplate.buildParams( - r.getValueAsString("id"), - r.getValueAsString("created_by"), - r.getValueAsString("create_time"), - r.getValueAsString("updated_by"), - r.getValueAsString("update_time"), - r.getValueAsString("status"))); - log.info("params: {}", SimpleJdbcTemplate.paramsToString(params)); - try (Connection conn = this.dataSource.getConnection()) { - SimpleJdbcTemplate.connect(conn).tx(() -> { - SimpleJdbcTemplate.connect(conn).batchUpdate(sql, params, 20); - - File targetFile = new File("C:\\Users\\zhouxy\\Desktop\\done", file.getName()); - boolean moveSucceeded = file.renameTo(targetFile); - if (!moveSucceeded) { - throw new IOException("文件移动失败:" + file.getName()); - } - }); + void testTransaction() { + try (Connection conn = dataSource.getConnection()) { + SimpleJdbcTemplate.connect(conn) + .tx(() -> { + SimpleJdbcTemplate.connect(conn) + .update("INSERT INTO base_table (created_by, create_time, status) VALUES (?, now(), 0)", 585757); + Optional> first = SimpleJdbcTemplate.connect(conn) + .queryFirst("SELECT * FROM base_table WHERE created_by = ?", 585757); + log.info("first: {}", first); + throw new NullPointerException(); + }); } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); + log.error(e.getMessage(), e); } } }