perf[module]: 优化orm支持map类型

This commit is contained in:
unknown
2022-02-21 09:59:01 +08:00
parent 2086c542d7
commit e7bb7fdd98
5 changed files with 231 additions and 9 deletions
@@ -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);
}
@@ -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);
}
}
}
@@ -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<String, Item> 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<String, Item> getMapItem() {
return mapItem;
}
public void setMapItem(Map<String, Item> mapItem) {
this.mapItem = mapItem;
}
@Override
public String toString() {
return "BagItem{" +
"id=" + id +
", desc='" + desc + '\'' +
", mapItem=" + mapItem +
'}';
}
}
@@ -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 + '\'' +
'}';
}
}
@@ -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<Long> {
@Id
private long id;
private Map<String, BagItem> 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<String, BagItem> getRoleBag() {
return roleBag;
}
public void setRoleBag(Map<String, BagItem> roleBag) {
this.roleBag = roleBag;
}
@Override
public String toString() {
return "MapEntity{" +
"id=" + id +
", roleBag=" + roleBag +
'}';
}
}