mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 01:28:24 +08:00
optimize Row
This commit is contained in:
parent
45258ff765
commit
2145114d31
@ -82,10 +82,10 @@ public class RowSqlProvider {
|
|||||||
throw FlexExceptions.wrap("rows can not be null or empty.");
|
throw FlexExceptions.wrap("rows can not be null or empty.");
|
||||||
}
|
}
|
||||||
|
|
||||||
//让所有 row 的列顺序和值的数量与第条数据保持一致
|
// 让所有 row 的列顺序和值的数量与第条数据保持一致
|
||||||
//这个必须 new 一个 LinkedHashSet,因为 keepModifyAttrs 会清除 row 所有的 modifyAttrs
|
// 这个必须 new 一个 LinkedHashSet,因为 keepModifyAttrs 会清除 row 所有的 modifyAttrs
|
||||||
Set<String> modifyAttrs = new LinkedHashSet<>(rows.get(0).obtainModifyAttrs());
|
Set<String> modifyAttrs = new LinkedHashSet<>(rows.get(0).obtainModifyAttrs());
|
||||||
rows.forEach(row -> RowCPI.keepModifyAttrs(row, modifyAttrs));
|
rows.forEach(row -> row.prepareAttrs(modifyAttrs));
|
||||||
|
|
||||||
|
|
||||||
Object[] values = new Object[]{};
|
Object[] values = new Object[]{};
|
||||||
@ -244,6 +244,7 @@ public class RowSqlProvider {
|
|||||||
|
|
||||||
return DialectFactory.getDialect().forUpdateEntity(tableInfo, entity, false);
|
return DialectFactory.getDialect().forUpdateEntity(tableInfo, entity, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 执行类似 update table set field=field+1 where ... 的场景
|
* 执行类似 update table set field=field+1 where ... 的场景
|
||||||
*
|
*
|
||||||
|
|||||||
@ -316,8 +316,29 @@ public class Row extends LinkedHashMap<String, Object> implements ModifyAttrsRec
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void prepareAttrsByKeySet(){
|
||||||
|
this.modifyAttrs.clear();
|
||||||
|
this.modifyAttrs.addAll(keySet());
|
||||||
|
|
||||||
void keepModifyAttrs(Collection<String> 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<String> attrs) {
|
||||||
if (attrs == null) {
|
if (attrs == null) {
|
||||||
throw new NullPointerException("attrs is null.");
|
throw new NullPointerException("attrs is null.");
|
||||||
}
|
}
|
||||||
@ -325,6 +346,17 @@ public class Row extends LinkedHashMap<String, Object> implements ModifyAttrsRec
|
|||||||
modifyAttrs.addAll(attrs);
|
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()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取修改的值,值需要保持顺序,返回的内容不包含主键的值
|
* 获取修改的值,值需要保持顺序,返回的内容不包含主键的值
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -15,17 +15,11 @@
|
|||||||
*/
|
*/
|
||||||
package com.mybatisflex.core.row;
|
package com.mybatisflex.core.row;
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* cross package invoker
|
* cross package invoker
|
||||||
*/
|
*/
|
||||||
public class RowCPI {
|
public class RowCPI {
|
||||||
|
|
||||||
public static void keepModifyAttrs(Row row, Collection<String> attrs) {
|
|
||||||
row.keepModifyAttrs(attrs);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Object[] obtainModifyValues(Row row) {
|
public static Object[] obtainModifyValues(Row row) {
|
||||||
return row.obtainModifyValues();
|
return row.obtainModifyValues();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,6 +16,7 @@
|
|||||||
package com.mybatisflex.core.row;
|
package com.mybatisflex.core.row;
|
||||||
|
|
||||||
import com.mybatisflex.annotation.KeyType;
|
import com.mybatisflex.annotation.KeyType;
|
||||||
|
import com.mybatisflex.core.keygen.KeyGenerators;
|
||||||
import com.mybatisflex.core.util.SqlUtil;
|
import com.mybatisflex.core.util.SqlUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -26,12 +27,22 @@ public class RowKey {
|
|||||||
/**
|
/**
|
||||||
* 自增 ID
|
* 自增 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
|
* 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) {
|
public static RowKey of(String keyColumn) {
|
||||||
|
|||||||
@ -133,6 +133,9 @@ public class RowUtil {
|
|||||||
|
|
||||||
|
|
||||||
public static void printPretty(Row row) {
|
public static void printPretty(Row row) {
|
||||||
|
if (row == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
printPretty(Collections.singletonList(row));
|
printPretty(Collections.singletonList(row));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -16,10 +16,7 @@
|
|||||||
package com.mybatisflex.test;
|
package com.mybatisflex.test;
|
||||||
|
|
||||||
import com.mybatisflex.core.MybatisFlexBootstrap;
|
import com.mybatisflex.core.MybatisFlexBootstrap;
|
||||||
import com.mybatisflex.core.row.BatchArgsSetter;
|
import com.mybatisflex.core.row.*;
|
||||||
import com.mybatisflex.core.row.Db;
|
|
||||||
import com.mybatisflex.core.row.Row;
|
|
||||||
import com.mybatisflex.core.row.RowKey;
|
|
||||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||||
|
|
||||||
@ -33,7 +30,7 @@ public class DbTestStarter {
|
|||||||
DataSource dataSource = new EmbeddedDatabaseBuilder()
|
DataSource dataSource = new EmbeddedDatabaseBuilder()
|
||||||
.setType(EmbeddedDatabaseType.H2)
|
.setType(EmbeddedDatabaseType.H2)
|
||||||
.addScript("schema.sql")
|
.addScript("schema.sql")
|
||||||
.addScript("data.sql")
|
// .addScript("data.sql")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
MybatisFlexBootstrap.getInstance()
|
MybatisFlexBootstrap.getInstance()
|
||||||
@ -41,15 +38,18 @@ public class DbTestStarter {
|
|||||||
.start();
|
.start();
|
||||||
|
|
||||||
Row row1 = Db.selectOneById("tb_account", "id", 1);
|
Row row1 = Db.selectOneById("tb_account", "id", 1);
|
||||||
System.out.println(row1);
|
RowUtil.printPretty(row1);
|
||||||
|
|
||||||
//查询全部
|
//查询全部
|
||||||
List<Row> rows = Db.selectAll("tb_account");
|
List<Row> rows = Db.selectAll("tb_account");
|
||||||
System.out.println(rows);
|
RowUtil.printPretty(rows);
|
||||||
|
|
||||||
|
|
||||||
//插入 1 条数据
|
//插入 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("user_name", "michael yang");
|
||||||
row.set("age", 18);
|
row.set("age", 18);
|
||||||
row.set("birthday", new Date());
|
row.set("birthday", new Date());
|
||||||
@ -80,6 +80,26 @@ public class DbTestStarter {
|
|||||||
|
|
||||||
//再次查询全部数据
|
//再次查询全部数据
|
||||||
rows = Db.selectAll("tb_account");
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -75,7 +75,7 @@ public class RowTestStarter {
|
|||||||
|
|
||||||
List<Row> rowList = new ArrayList<>();
|
List<Row> rowList = new ArrayList<>();
|
||||||
for (int i = 0; i < 10; i++) {
|
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.USER_NAME,"zhang" + i);
|
||||||
row.set(ACCOUNT.AGE,18);
|
row.set(ACCOUNT.AGE,18);
|
||||||
// row.set(ACCOUNT.BIRTHDAY,new Date());
|
// row.set(ACCOUNT.BIRTHDAY,new Date());
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user