mirror of
https://github.com/CarmJos/EasySQL.git
synced 2026-06-04 15:28:20 +08:00
feat(keys): 现在可以自定义返回的自增主键类型。
现在可以通过 returnGeneratedKey(Class<T> numberClass) 方法要求返回指定类型的自增主键。 BREAKING CHANGE: 移除了对于“是否返回主键”的选择,一旦定义了主键类型,就代表action将返回该类型的主键。
This commit is contained in:
@@ -134,10 +134,10 @@ public class EasySQLDemo {
|
||||
|
||||
public void sqlInsert(SQLManager sqlManager) {
|
||||
// 同步SQL插入 (不使用try-catch的情况下,返回的数值可能为空。)
|
||||
Long id = sqlManager.createInsert("users")
|
||||
int id = sqlManager.createInsert("users")
|
||||
.setColumnNames("username", "phone", "email", "registerTime")
|
||||
.setParams("CarmJos", "18888888888", "carm@carm.cc", TimeDateUtils.getCurrentTime())
|
||||
.setReturnGeneratedKey(true)// 设定在后续返回自增主键
|
||||
.returnGeneratedKey() // 设定在后续返回自增主键
|
||||
.execute((exception, action) -> {
|
||||
// 处理异常
|
||||
System.out.println("#" + action.getShortID() + " -> " + action.getSQLContent());
|
||||
@@ -145,7 +145,7 @@ public class EasySQLDemo {
|
||||
});
|
||||
|
||||
try {
|
||||
Long userID = sqlManager.createInsert("users")
|
||||
int userID = sqlManager.createInsert("users")
|
||||
.setColumnNames("username", "phone", "email", "registerTime")
|
||||
.setParams("CarmJos", "18888888888", "carm@carm.cc", TimeDateUtils.getCurrentTime())
|
||||
.returnGeneratedKey().execute();
|
||||
|
||||
@@ -8,6 +8,7 @@ import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -16,7 +17,6 @@ public class EasySQLTest {
|
||||
|
||||
@Test
|
||||
public void onTest() {
|
||||
|
||||
HikariConfig config = new HikariConfig();
|
||||
config.setDriverClassName("org.h2.Driver");
|
||||
config.setJdbcUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;MODE=MYSQL;");
|
||||
@@ -30,9 +30,10 @@ public class EasySQLTest {
|
||||
// tests.add(new TableAlterTest());
|
||||
// tests.add(new TableRenameTest());
|
||||
// tests.add(new QueryNotCloseTest());
|
||||
tests.add(new QueryCloseTest());
|
||||
|
||||
tests.add(new SQLUpdateBatchTests());
|
||||
tests.add(new SQLUpdateReturnKeysTest());
|
||||
tests.add(new QueryCloseTest());
|
||||
tests.add(new QueryFunctionTest());
|
||||
// tests.add(new DeleteTest());
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import cc.carm.lib.easysql.TestHandler;
|
||||
import cc.carm.lib.easysql.api.SQLManager;
|
||||
import cc.carm.lib.easysql.api.SQLQuery;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
@@ -24,17 +25,11 @@ public class QueryCloseTest extends TestHandler {
|
||||
|
||||
System.out.printf(
|
||||
"id: %d username: %s%n",
|
||||
resultSet.getInt("id"),
|
||||
resultSet.getObject("id", BigInteger.class),
|
||||
resultSet.getString("username")
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(500L);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ public class SQLUpdateBatchTests extends TestHandler {
|
||||
List<Long> updates = sqlManager.createInsertBatch("test_user_table")
|
||||
.setColumnNames("uuid", "username", "age")
|
||||
.setAllParams(generateParams())
|
||||
.returnGeneratedKeys(Long.class)
|
||||
.execute();
|
||||
|
||||
System.out.println("changes " + Arrays.toString(updates.toArray()));
|
||||
|
||||
@@ -11,10 +11,10 @@ public class SQLUpdateReturnKeysTest extends SQLUpdateBatchTests {
|
||||
|
||||
@Override
|
||||
public void onTest(SQLManager sqlManager) throws SQLException {
|
||||
List<Long> generatedKeys = sqlManager.createInsertBatch("test_user_table")
|
||||
List<Integer> generatedKeys = sqlManager.createInsertBatch("test_user_table")
|
||||
.setColumnNames("uuid", "username", "age")
|
||||
.setAllParams(generateParams())
|
||||
.returnGeneratedKeys()
|
||||
.returnGeneratedKeys(Integer.class)
|
||||
.execute();
|
||||
|
||||
System.out.println("generated " + Arrays.toString(generatedKeys.toArray()));
|
||||
|
||||
Reference in New Issue
Block a user