优化 IAtom 接口。

feature/net-util
ZhouXY108 2023-06-25 09:14:27 +08:00
parent e61a393b93
commit 9d5fd0ff56
2 changed files with 19 additions and 65 deletions

View File

@ -232,11 +232,11 @@ public class SimpleJdbcTemplate {
} }
} }
public void tx(final IAtom tx) throws Exception { public <T extends Exception> void tx(final IAtom<T> atom) throws SQLException, T {
Assert.notNull(tx, "Tx can not be null."); Assert.notNull(atom, "Tx can not be null.");
try { try {
this.conn.setAutoCommit(false); this.conn.setAutoCommit(false);
tx.execute(); atom.execute();
conn.commit(); conn.commit();
conn.setAutoCommit(true); conn.setAutoCommit(true);
} catch (Exception e) { } catch (Exception e) {
@ -247,9 +247,9 @@ public class SimpleJdbcTemplate {
} }
@FunctionalInterface @FunctionalInterface
public static interface IAtom { public static interface IAtom<T extends Exception> {
@SuppressWarnings("all") @SuppressWarnings("all")
void execute() throws Exception; void execute() throws SQLException, T;
} }
} }
} }

View File

@ -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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static xyz.zhouxy.plusone.commons.jdbc.JdbcSql.NOT_IN;
import static xyz.zhouxy.plusone.commons.jdbc.JdbcSql.*;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -21,8 +16,6 @@ import org.junit.jupiter.api.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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.HikariConfig;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
@ -75,58 +68,19 @@ class SimpleJdbcTemplateTests {
} }
@Test @Test
void testSaveTxt() throws IOException, SQLException { void testTransaction() {
File file = new File("C:\\Users\\zhouxy\\Desktop", "Untitled-1.txt"); try (Connection conn = dataSource.getConnection()) {
assertTrue(file.exists()); SimpleJdbcTemplate.connect(conn)
List<String> recordStrList = Files.readLines(file, StandardCharsets.UTF_8); .tx(() -> {
if (MoreCollections.isEmpty(recordStrList)) { SimpleJdbcTemplate.connect(conn)
log.info("file is empty."); .update("INSERT INTO base_table (created_by, create_time, status) VALUES (?, now(), 0)", 585757);
return; Optional<Map<String,Object>> first = SimpleJdbcTemplate.connect(conn)
} .queryFirst("SELECT * FROM base_table WHERE created_by = ?", 585757);
log.info("first: {}", first);
List<DbRecord> recordList = new ArrayList<>(recordStrList.size()); throw new NullPointerException();
for (String rStr : recordStrList) { });
List<String> 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<Object[]> 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());
}
});
} catch (Exception e) { } catch (Exception e) {
// TODO Auto-generated catch block log.error(e.getMessage(), e);
e.printStackTrace();
} }
} }
} }