forked from plusone/plusone-commons
124 lines
4.5 KiB
Java
124 lines
4.5 KiB
Java
package xyz.zhouxy.plusone.commons.util;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
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.Optional;
|
|
import java.util.stream.Collectors;
|
|
|
|
import javax.sql.DataSource;
|
|
|
|
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;
|
|
|
|
import xyz.zhouxy.plusone.commons.jdbc.DbRecord;
|
|
import xyz.zhouxy.plusone.commons.jdbc.SimpleJdbcTemplate;
|
|
|
|
class SimpleJdbcTemplateTests {
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(SimpleJdbcTemplateTests.class);
|
|
|
|
DataSource dataSource;
|
|
|
|
String[] cStruct = {
|
|
"id",
|
|
"created_by",
|
|
"create_time",
|
|
"updated_by",
|
|
"update_time",
|
|
"status"
|
|
};
|
|
|
|
SimpleJdbcTemplateTests() {
|
|
HikariConfig config = new HikariConfig();
|
|
config.setJdbcUrl("jdbc:postgresql://localhost:5432/plusone");
|
|
config.setUsername("postgres");
|
|
config.setPassword("zhouxy108");
|
|
this.dataSource = new HikariDataSource(config);
|
|
}
|
|
|
|
@Test
|
|
void testQuery() throws SQLException {
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
List<DbRecord> rs = SimpleJdbcTemplate.connect(conn)
|
|
.queryToRecordList(
|
|
"SELECT * FROM public.base_table WHERE id IN (?, ?, ?)",
|
|
501533, 501554, 544599);
|
|
assertTrue(3 > rs.size());
|
|
for (DbRecord baseEntity : rs) {
|
|
log.info("id: {}", baseEntity.getValueAsLong("id"));
|
|
assertEquals(Optional.empty(), baseEntity.getValueAsString("updated_by"));
|
|
}
|
|
}
|
|
}
|
|
|
|
@Test
|
|
void testSaveTxt() throws IOException, SQLException {
|
|
File file = new File("C:\\Users\\zhouxy\\Desktop", "Untitled-1.txt");
|
|
assertTrue(file.exists());
|
|
List<String> recordStrList = Files.readLines(file, StandardCharsets.UTF_8);
|
|
if (MoreCollections.isEmpty(recordStrList)) {
|
|
log.info("file is empty.");
|
|
return;
|
|
}
|
|
|
|
List<DbRecord> recordList = new ArrayList<>(recordStrList.size());
|
|
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 = recordList.stream()
|
|
.map(r -> new Object[] {
|
|
r.getValueAsString("id").orElse(null),
|
|
r.getValueAsString("created_by").orElse(null),
|
|
r.getValueAsString("create_time").orElse(null),
|
|
r.getValueAsString("updated_by").orElse(null),
|
|
r.getValueAsString("update_time").orElse(null),
|
|
r.getValueAsString("status").orElse(null)
|
|
})
|
|
.collect(Collectors.toList());
|
|
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) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|