From 2145114d312bac3b87f5f3e5c5e7cd4016460948 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Sun, 28 May 2023 16:46:56 +0800 Subject: [PATCH] optimize Row --- .../core/provider/RowSqlProvider.java | 7 ++-- .../java/com/mybatisflex/core/row/Row.java | 34 ++++++++++++++++- .../java/com/mybatisflex/core/row/RowCPI.java | 6 --- .../java/com/mybatisflex/core/row/RowKey.java | 15 +++++++- .../com/mybatisflex/core/row/RowUtil.java | 3 ++ .../com/mybatisflex/test/DbTestStarter.java | 38 ++++++++++++++----- .../com/mybatisflex/test/RowTestStarter.java | 2 +- 7 files changed, 83 insertions(+), 22 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/RowSqlProvider.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/RowSqlProvider.java index 842aa2ea..8c30cabf 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/RowSqlProvider.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/RowSqlProvider.java @@ -82,10 +82,10 @@ public class RowSqlProvider { throw FlexExceptions.wrap("rows can not be null or empty."); } - //让所有 row 的列顺序和值的数量与第条数据保持一致 - //这个必须 new 一个 LinkedHashSet,因为 keepModifyAttrs 会清除 row 所有的 modifyAttrs + // 让所有 row 的列顺序和值的数量与第条数据保持一致 + // 这个必须 new 一个 LinkedHashSet,因为 keepModifyAttrs 会清除 row 所有的 modifyAttrs Set modifyAttrs = new LinkedHashSet<>(rows.get(0).obtainModifyAttrs()); - rows.forEach(row -> RowCPI.keepModifyAttrs(row, modifyAttrs)); + rows.forEach(row -> row.prepareAttrs(modifyAttrs)); Object[] values = new Object[]{}; @@ -244,6 +244,7 @@ public class RowSqlProvider { return DialectFactory.getDialect().forUpdateEntity(tableInfo, entity, false); } + /** * 执行类似 update table set field=field+1 where ... 的场景 * diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Row.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Row.java index 514bf14d..d1d8248e 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Row.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Row.java @@ -316,8 +316,29 @@ public class Row extends LinkedHashMap implements ModifyAttrsRec return ret; } + public void prepareAttrsByKeySet(){ + this.modifyAttrs.clear(); + this.modifyAttrs.addAll(keySet()); - void keepModifyAttrs(Collection attrs) { + if (this.primaryKeys != null){ + for (RowKey primaryKey : primaryKeys) { + this.modifyAttrs.removeIf(s -> s.equalsIgnoreCase(primaryKey.getKeyColumn())); + } + } + } + + + public void prepareAttrsByKeySet(RowKey ... primaryKeys){ + this.modifyAttrs.clear(); + this.modifyAttrs.addAll(keySet()); + this.primaryKeys = primaryKeys; + + for (RowKey primaryKey : primaryKeys) { + this.modifyAttrs.removeIf(s -> s.equalsIgnoreCase(primaryKey.getKeyColumn())); + } + } + + public void prepareAttrs(Collection attrs) { if (attrs == null) { throw new NullPointerException("attrs is null."); } @@ -325,6 +346,17 @@ public class Row extends LinkedHashMap implements ModifyAttrsRec modifyAttrs.addAll(attrs); } + public RowKey[] getPrimaryKeys() { + return primaryKeys; + } + + public void setPrimaryKeys(RowKey... primaryKeys) { + this.primaryKeys = primaryKeys; + for (RowKey primaryKey : primaryKeys) { + this.modifyAttrs.removeIf(s -> s.equalsIgnoreCase(primaryKey.getKeyColumn())); + } + } + /** * 获取修改的值,值需要保持顺序,返回的内容不包含主键的值 */ diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowCPI.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowCPI.java index 5001747c..5ab652d4 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowCPI.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowCPI.java @@ -15,17 +15,11 @@ */ package com.mybatisflex.core.row; -import java.util.Collection; - /** * cross package invoker */ public class RowCPI { - public static void keepModifyAttrs(Row row, Collection attrs) { - row.keepModifyAttrs(attrs); - } - public static Object[] obtainModifyValues(Row row) { return row.obtainModifyValues(); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowKey.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowKey.java index 21b01fca..ad6b7f94 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowKey.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowKey.java @@ -16,6 +16,7 @@ package com.mybatisflex.core.row; import com.mybatisflex.annotation.KeyType; +import com.mybatisflex.core.keygen.KeyGenerators; import com.mybatisflex.core.util.SqlUtil; /** @@ -26,12 +27,22 @@ public class RowKey { /** * 自增 ID */ - public static final RowKey ID_AUTO = RowKey.of("id", KeyType.Auto, null, false); + public static final RowKey AUTO = RowKey.of("id", KeyType.Auto, null, false); /** * UUID 的 ID */ - public static final RowKey ID_UUID = RowKey.of("id", KeyType.Generator, "uuid", true); + public static final RowKey UUID = RowKey.of("id", KeyType.Generator, KeyGenerators.uuid, true); + + /** + * flexId + */ + public static final RowKey FLEX_ID = RowKey.of("id", KeyType.Generator, KeyGenerators.flexId, true); + + /** + * snowFlakeId + */ + public static final RowKey SNOW_FLAKE_ID = RowKey.of("id", KeyType.Generator, KeyGenerators.snowFlakeId, true); public static RowKey of(String keyColumn) { diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowUtil.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowUtil.java index ec9a3bfb..0733acae 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowUtil.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowUtil.java @@ -133,6 +133,9 @@ public class RowUtil { public static void printPretty(Row row) { + if (row == null) { + return; + } printPretty(Collections.singletonList(row)); } diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/DbTestStarter.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/DbTestStarter.java index b38459bf..02366b9e 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/DbTestStarter.java +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/DbTestStarter.java @@ -16,10 +16,7 @@ package com.mybatisflex.test; import com.mybatisflex.core.MybatisFlexBootstrap; -import com.mybatisflex.core.row.BatchArgsSetter; -import com.mybatisflex.core.row.Db; -import com.mybatisflex.core.row.Row; -import com.mybatisflex.core.row.RowKey; +import com.mybatisflex.core.row.*; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; @@ -33,7 +30,7 @@ public class DbTestStarter { DataSource dataSource = new EmbeddedDatabaseBuilder() .setType(EmbeddedDatabaseType.H2) .addScript("schema.sql") - .addScript("data.sql") +// .addScript("data.sql") .build(); MybatisFlexBootstrap.getInstance() @@ -41,15 +38,18 @@ public class DbTestStarter { .start(); Row row1 = Db.selectOneById("tb_account", "id", 1); - System.out.println(row1); + RowUtil.printPretty(row1); //查询全部 List rows = Db.selectAll("tb_account"); - System.out.println(rows); + RowUtil.printPretty(rows); //插入 1 条数据 - Row row = Row.ofKey(RowKey.ID_AUTO); + + Row row = Row.ofKey(RowKey.AUTO); +// Row row = new Row(); +// row.set("id", 3); row.set("user_name", "michael yang"); row.set("age", 18); row.set("birthday", new Date()); @@ -80,6 +80,26 @@ public class DbTestStarter { //再次查询全部数据 rows = Db.selectAll("tb_account"); - System.out.println(rows); + RowUtil.printPretty(rows); + +// for (Row row2 : rows) { +//// for (String s : row2.keySet()) { +//// if (!s.equalsIgnoreCase("id")) { +//// row2.set(s, row2.get(s)); +//// } +//// } +// rows.remove("id"); +// } + +// rows.forEach(row2 -> row2.setPrimaryKeys(RowKey.AUTO)); + rows.forEach(r -> { + r.prepareAttrsByKeySet(); + r.setPrimaryKeys(RowKey.AUTO); + }); + Db.insertBatch("tb_account", rows, 100); + + //再次查询全部数据 + rows = Db.selectAll("tb_account"); + RowUtil.printPretty(rows); } } diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/RowTestStarter.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/RowTestStarter.java index c7ec5f77..f748af46 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/RowTestStarter.java +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/RowTestStarter.java @@ -75,7 +75,7 @@ public class RowTestStarter { List rowList = new ArrayList<>(); for (int i = 0; i < 10; i++) { - Row row = Row.ofKey(RowKey.ID_AUTO); + Row row = Row.ofKey(RowKey.AUTO); row.set(ACCOUNT.USER_NAME,"zhang" + i); row.set(ACCOUNT.AGE,18); // row.set(ACCOUNT.BIRTHDAY,new Date());