From e7bb7fdd98704d5b08a0f8ef473ec726737f18dc Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 21 Feb 2022 09:59:01 +0800 Subject: [PATCH] =?UTF-8?q?perf[module]:=20=E4=BC=98=E5=8C=96orm=E6=94=AF?= =?UTF-8?q?=E6=8C=81map=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/zfoo/orm/manager/OrmManager.java | 20 +++--- .../java/com/zfoo/orm/accessor/MapTest.java | 47 ++++++++++++++ .../java/com/zfoo/orm/entity/bag/BagItem.java | 62 ++++++++++++++++++ .../java/com/zfoo/orm/entity/bag/Item.java | 48 ++++++++++++++ .../com/zfoo/orm/entity/bag/MapEntity.java | 63 +++++++++++++++++++ 5 files changed, 231 insertions(+), 9 deletions(-) create mode 100644 orm/src/test/java/com/zfoo/orm/accessor/MapTest.java create mode 100644 orm/src/test/java/com/zfoo/orm/entity/bag/BagItem.java create mode 100644 orm/src/test/java/com/zfoo/orm/entity/bag/Item.java create mode 100644 orm/src/test/java/com/zfoo/orm/entity/bag/MapEntity.java diff --git a/orm/src/main/java/com/zfoo/orm/manager/OrmManager.java b/orm/src/main/java/com/zfoo/orm/manager/OrmManager.java index ebaaae03..30d0d9fa 100644 --- a/orm/src/main/java/com/zfoo/orm/manager/OrmManager.java +++ b/orm/src/main/java/com/zfoo/orm/manager/OrmManager.java @@ -445,12 +445,12 @@ public class OrmManager implements IOrmManager { } entitySubClassMap.put(clazz, new HashSet<>()); - // 是否为一个简单的javabean - ReflectionUtils.assertIsPojoClass(clazz); - // 不能是泛型类 - AssertionUtils.isTrue(ArrayUtils.isEmpty(clazz.getTypeParameters()), "[class:{}]不能是泛型类", clazz.getCanonicalName()); - // 必须要有一个空的构造器 - ReflectionUtils.publicEmptyConstructor(clazz); +// // 是否为一个简单的javabean +// ReflectionUtils.assertIsPojoClass(clazz); +// // 不能是泛型类 +// AssertionUtils.isTrue(ArrayUtils.isEmpty(clazz.getTypeParameters()), "[class:{}]不能是泛型类", clazz.getCanonicalName()); +// // 必须要有一个空的构造器 +// ReflectionUtils.publicEmptyConstructor(clazz); // 不能使用Storage的Index注解 var storageIndexes = ReflectionUtils.getFieldsByAnnoNameInPOJOClass(clazz, "com.zfoo.storage.model.anno.Index"); @@ -497,9 +497,11 @@ public class OrmManager implements IOrmManager { AssertionUtils.isTrue(types.length == 1, "ORM[class:{}]中List类型声明不正确,[field:{}]必须声明泛型类", clazz.getCanonicalName(), field.getName()); checkSubEntity(clazz, types[0], entitySubClassMap); - } else if (Map.class.isAssignableFrom(fieldType)) { - throw new RunException("ORM[class:{}]类型声明不正确,不支持Map类型", clazz.getCanonicalName()); - } else { + } +// else if (Map.class.isAssignableFrom(fieldType)) { +// throw new RunException("ORM[class:{}]类型声明不正确,不支持Map类型", clazz.getCanonicalName()); +// } + else { entitySubClassMap.get(clazz).add(fieldType); checkEntity(fieldType, entitySubClassMap); } diff --git a/orm/src/test/java/com/zfoo/orm/accessor/MapTest.java b/orm/src/test/java/com/zfoo/orm/accessor/MapTest.java new file mode 100644 index 00000000..b6b1508a --- /dev/null +++ b/orm/src/test/java/com/zfoo/orm/accessor/MapTest.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.orm.accessor; + +import com.zfoo.orm.OrmContext; +import com.zfoo.orm.entity.bag.BagItem; +import com.zfoo.orm.entity.bag.Item; +import com.zfoo.orm.entity.bag.MapEntity; +import org.junit.Ignore; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +@Ignore +public class MapTest { + private static final Logger log = LoggerFactory.getLogger(MapTest.class); + + @Test + public void insertMapData() { + + var context = new ClassPathXmlApplicationContext("application.xml"); + MapEntity entity = OrmContext.getAccessor().load(1, MapEntity.class); + if (entity == null) { + entity = new MapEntity(); + entity.setId(1); + entity.getRoleBag().computeIfAbsent("1", k -> new BagItem()) + .getMapItem().computeIfAbsent("2", k -> new Item()); + OrmContext.getAccessor().insert(entity); + log.info("数据插入成功 {}", entity); + } else { + log.info("entity已存在 {}", entity); + } + + } +} \ No newline at end of file diff --git a/orm/src/test/java/com/zfoo/orm/entity/bag/BagItem.java b/orm/src/test/java/com/zfoo/orm/entity/bag/BagItem.java new file mode 100644 index 00000000..5ec26552 --- /dev/null +++ b/orm/src/test/java/com/zfoo/orm/entity/bag/BagItem.java @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.orm.entity.bag; + +import java.util.HashMap; +import java.util.Map; + +public class BagItem { + private int id; + private String desc; + + private Map mapItem = new HashMap<>(); + + public BagItem() { + this.id = 1; + this.desc = "desc"; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getDesc() { + return desc; + } + + public void setDesc(String desc) { + this.desc = desc; + } + + public Map getMapItem() { + return mapItem; + } + + public void setMapItem(Map mapItem) { + this.mapItem = mapItem; + } + + @Override + public String toString() { + return "BagItem{" + + "id=" + id + + ", desc='" + desc + '\'' + + ", mapItem=" + mapItem + + '}'; + } +} \ No newline at end of file diff --git a/orm/src/test/java/com/zfoo/orm/entity/bag/Item.java b/orm/src/test/java/com/zfoo/orm/entity/bag/Item.java new file mode 100644 index 00000000..d71dba8a --- /dev/null +++ b/orm/src/test/java/com/zfoo/orm/entity/bag/Item.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.orm.entity.bag; + +public class Item { + private int a; + private String b; + + public Item() { + this.a = 1; + this.b = "bbbb"; + } + + public int getA() { + return a; + } + + public void setA(int a) { + this.a = a; + } + + public String getB() { + return b; + } + + public void setB(String b) { + this.b = b; + } + + @Override + public String toString() { + return "Item{" + + "a=" + a + + ", b='" + b + '\'' + + '}'; + } +} \ No newline at end of file diff --git a/orm/src/test/java/com/zfoo/orm/entity/bag/MapEntity.java b/orm/src/test/java/com/zfoo/orm/entity/bag/MapEntity.java new file mode 100644 index 00000000..f3a793f3 --- /dev/null +++ b/orm/src/test/java/com/zfoo/orm/entity/bag/MapEntity.java @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.orm.entity.bag; + +import com.zfoo.orm.model.anno.EntityCache; +import com.zfoo.orm.model.anno.Id; +import com.zfoo.orm.model.anno.Persister; +import com.zfoo.orm.model.entity.IEntity; + +import java.util.HashMap; +import java.util.Map; + +@EntityCache(cacheStrategy = "thousand", persister = @Persister("time30s")) +public class MapEntity implements IEntity { + @Id + private long id; + + private Map roleBag = new HashMap<>(); + + public MapEntity() { + + } + + @Override + public Long id() { + return id; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public Map getRoleBag() { + return roleBag; + } + + public void setRoleBag(Map roleBag) { + this.roleBag = roleBag; + } + + @Override + public String toString() { + return "MapEntity{" + + "id=" + id + + ", roleBag=" + roleBag + + '}'; + } +} \ No newline at end of file