From c1481c94792d95081e6d7ef431fb132fbd786563 Mon Sep 17 00:00:00 2001 From: jaysunxiao Date: Mon, 5 Jul 2021 21:43:45 +0800 Subject: [PATCH] =?UTF-8?q?perf[orm]:=20=E7=AE=80=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- orm/README.md | 6 ++++ orm/src/test/java/com/zfoo/orm/TestUnit.java | 27 ------------------ .../com/zfoo/orm/accessor/AccessorTest.java | 28 +++++++++---------- .../com/zfoo/orm/accessor/DeleteTest.java | 5 ++-- .../com/zfoo/orm/accessor/IndexTextTest.java | 5 ++-- .../com/zfoo/orm/accessor/InsertTest.java | 5 ++-- .../zfoo/orm/accessor/TransactionTest.java | 3 +- .../com/zfoo/orm/accessor/UpdateTest.java | 7 ++--- .../java/com/zfoo/orm/query/QueryTest.java | 5 ++-- 9 files changed, 31 insertions(+), 60 deletions(-) delete mode 100644 orm/src/test/java/com/zfoo/orm/TestUnit.java diff --git a/orm/README.md b/orm/README.md index d5714139..52a7b8cd 100644 --- a/orm/README.md +++ b/orm/README.md @@ -35,6 +35,12 @@ OrmContext.getAccessor().delete(obj); OrmContext.getAccessor().update(obj); ``` +- 加载数据库中的数据 + +``` +OrmContext.getAccessor().load(id, class); +``` + #### 2. IQuery接口,为数据复杂查询接口 #### 3. 缓存使用方法 diff --git a/orm/src/test/java/com/zfoo/orm/TestUnit.java b/orm/src/test/java/com/zfoo/orm/TestUnit.java deleted file mode 100644 index 859a5aab..00000000 --- a/orm/src/test/java/com/zfoo/orm/TestUnit.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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; - -/** - * @author jaysunxiao - * @version 3.0 - */ -public class TestUnit { - - public static final String CONFIG_LOCATION = "application.xml"; - - - public static final int NUM = 10; - -} diff --git a/orm/src/test/java/com/zfoo/orm/accessor/AccessorTest.java b/orm/src/test/java/com/zfoo/orm/accessor/AccessorTest.java index aa9ba125..97344622 100644 --- a/orm/src/test/java/com/zfoo/orm/accessor/AccessorTest.java +++ b/orm/src/test/java/com/zfoo/orm/accessor/AccessorTest.java @@ -14,7 +14,6 @@ package com.zfoo.orm.accessor; import com.zfoo.orm.OrmContext; -import com.zfoo.orm.TestUnit; import com.zfoo.orm.entity.UserEntity; import org.junit.Ignore; import org.junit.Test; @@ -22,7 +21,6 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.ArrayList; import java.util.Collections; -import java.util.List; /** * @author jaysunxiao @@ -33,10 +31,10 @@ public class AccessorTest { @Test public void testBatchInsert() { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(TestUnit.CONFIG_LOCATION); - List listUser = new ArrayList<>(); - for (int i = 0; i <= TestUnit.NUM; i++) { - UserEntity userEntity = new UserEntity(i, (byte) 1, (short) i, i, true, "helloOrm" + i, "helloOrm" + i); + var context = new ClassPathXmlApplicationContext("application.xml"); + var listUser = new ArrayList(); + for (var i = 1; i <= 10; i++) { + var userEntity = new UserEntity(i, (byte) 1, (short) i, i, true, "helloOrm" + i, "helloOrm" + i); listUser.add(userEntity); } OrmContext.getAccessor().batchInsert(listUser); @@ -44,36 +42,36 @@ public class AccessorTest { @Test public void testInsert() { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(TestUnit.CONFIG_LOCATION); - UserEntity userEntity = new UserEntity(1, (byte) 2, (short) 3, 5, true, "helloORM", "helloOrm"); + var context = new ClassPathXmlApplicationContext("application.xml"); + var userEntity = new UserEntity(1, (byte) 2, (short) 3, 5, true, "helloORM", "helloOrm"); OrmContext.getAccessor().insert(userEntity); } @Test public void testUpdate() { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(TestUnit.CONFIG_LOCATION); - UserEntity userEntity = new UserEntity(1, (byte) 2, (short) 3, 5, true, "helloUpdate", "helloOrm"); + var context = new ClassPathXmlApplicationContext("application.xml"); + var userEntity = new UserEntity(1, (byte) 2, (short) 3, 5, true, "helloUpdate", "helloOrm"); OrmContext.getAccessor().update(userEntity); } @Test public void testBatchUpdate() { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(TestUnit.CONFIG_LOCATION); - UserEntity userEntity = new UserEntity(1, (byte) 2, (short) 3, 5, true, "helloBatchUpdate", "helloOrm"); + var context = new ClassPathXmlApplicationContext("application.xml"); + var userEntity = new UserEntity(1, (byte) 2, (short) 3, 5, true, "helloBatchUpdate", "helloOrm"); userEntity.setC(222); OrmContext.getAccessor().batchUpdate(Collections.singletonList(userEntity)); } @Test public void testLoad() { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(TestUnit.CONFIG_LOCATION); - UserEntity ent = (UserEntity) OrmContext.getAccessor().load(1L, UserEntity.class); + var context = new ClassPathXmlApplicationContext("application.xml"); + var ent = (UserEntity) OrmContext.getAccessor().load(1L, UserEntity.class); System.out.println(ent); } @Test public void testDelete() { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(TestUnit.CONFIG_LOCATION); + var context = new ClassPathXmlApplicationContext("application.xml"); OrmContext.getAccessor().delete(1L, UserEntity.class); } diff --git a/orm/src/test/java/com/zfoo/orm/accessor/DeleteTest.java b/orm/src/test/java/com/zfoo/orm/accessor/DeleteTest.java index 23bf2945..dac5ebf0 100644 --- a/orm/src/test/java/com/zfoo/orm/accessor/DeleteTest.java +++ b/orm/src/test/java/com/zfoo/orm/accessor/DeleteTest.java @@ -14,7 +14,6 @@ package com.zfoo.orm.accessor; import com.zfoo.orm.OrmContext; -import com.zfoo.orm.TestUnit; import com.zfoo.orm.entity.UserEntity; import com.zfoo.orm.model.cache.IEntityCaches; import com.zfoo.util.ThreadUtils; @@ -36,11 +35,11 @@ public class DeleteTest { @Test public void test() { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(TestUnit.CONFIG_LOCATION); + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml"); IEntityCaches userEntityCaches = (IEntityCaches) OrmContext.getOrmManager().getEntityCaches(UserEntity.class); - for (int i = 0; i < TestUnit.NUM; i++) { + for (int i = 0; i < 10; i++) { UserEntity userEntity = new UserEntity(i, (byte) 1, (short) i, i, true, "helloOrm" + i, "helloOrm" + i); OrmContext.getAccessor().delete(userEntity); diff --git a/orm/src/test/java/com/zfoo/orm/accessor/IndexTextTest.java b/orm/src/test/java/com/zfoo/orm/accessor/IndexTextTest.java index 6dd82595..30b9c996 100644 --- a/orm/src/test/java/com/zfoo/orm/accessor/IndexTextTest.java +++ b/orm/src/test/java/com/zfoo/orm/accessor/IndexTextTest.java @@ -15,7 +15,6 @@ package com.zfoo.orm.accessor; import com.mongodb.client.model.Filters; import com.zfoo.orm.OrmContext; -import com.zfoo.orm.TestUnit; import com.zfoo.orm.entity.UserEntity; import com.zfoo.util.ThreadUtils; import org.junit.Ignore; @@ -35,7 +34,7 @@ public class IndexTextTest { @Test public void insertTest() { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(TestUnit.CONFIG_LOCATION); + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml"); List listUser = new ArrayList<>(); var userEntity = new UserEntity(1, (byte) 1, (short) 1, 1, true, "两个黄鹂鸣翠柳,一行白鹭上青天。窗含西岭千秋雪,门泊东吴万里船。", null); listUser.add(userEntity); @@ -50,7 +49,7 @@ public class IndexTextTest { @Test public void queryTest() { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(TestUnit.CONFIG_LOCATION); + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml"); var collection = OrmContext.getOrmManager().getCollection(UserEntity.class); collection.find(Filters.text("窗含西岭千秋雪")).forEach(new Consumer() { @Override diff --git a/orm/src/test/java/com/zfoo/orm/accessor/InsertTest.java b/orm/src/test/java/com/zfoo/orm/accessor/InsertTest.java index 663f2b58..10a65dea 100644 --- a/orm/src/test/java/com/zfoo/orm/accessor/InsertTest.java +++ b/orm/src/test/java/com/zfoo/orm/accessor/InsertTest.java @@ -14,7 +14,6 @@ package com.zfoo.orm.accessor; import com.zfoo.orm.OrmContext; -import com.zfoo.orm.TestUnit; import com.zfoo.orm.entity.UserEntity; import com.zfoo.orm.model.cache.IEntityCaches; import com.zfoo.util.ThreadUtils; @@ -32,11 +31,11 @@ public class InsertTest { @SuppressWarnings("unchecked") @Test public void test() { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(TestUnit.CONFIG_LOCATION); + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml"); IEntityCaches userEntityCaches = (IEntityCaches) OrmContext.getOrmManager().getEntityCaches(UserEntity.class); - for (int i = 0; i < TestUnit.NUM; i++) { + for (int i = 0; i < 10; i++) { UserEntity userEntity = new UserEntity(i, (byte) 1, (short) i, i, true, "helloOrm" + i, "helloOrm" + i); OrmContext.getAccessor().insert(userEntity); } diff --git a/orm/src/test/java/com/zfoo/orm/accessor/TransactionTest.java b/orm/src/test/java/com/zfoo/orm/accessor/TransactionTest.java index 31d8c89b..3373be7f 100644 --- a/orm/src/test/java/com/zfoo/orm/accessor/TransactionTest.java +++ b/orm/src/test/java/com/zfoo/orm/accessor/TransactionTest.java @@ -19,7 +19,6 @@ import com.mongodb.TransactionOptions; import com.mongodb.WriteConcern; import com.mongodb.client.TransactionBody; import com.zfoo.orm.OrmContext; -import com.zfoo.orm.TestUnit; import com.zfoo.orm.entity.UserEntity; import org.junit.Ignore; import org.junit.Test; @@ -36,7 +35,7 @@ public class TransactionTest { @Test public void transactionTest() { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(TestUnit.CONFIG_LOCATION); + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml"); /* Step 1: Start a client session. */ var clientSession = OrmContext.getOrmManager().getClientSession(); diff --git a/orm/src/test/java/com/zfoo/orm/accessor/UpdateTest.java b/orm/src/test/java/com/zfoo/orm/accessor/UpdateTest.java index 53e9cc20..395d4037 100644 --- a/orm/src/test/java/com/zfoo/orm/accessor/UpdateTest.java +++ b/orm/src/test/java/com/zfoo/orm/accessor/UpdateTest.java @@ -15,7 +15,6 @@ package com.zfoo.orm.accessor; import com.mongodb.client.model.Filters; import com.zfoo.orm.OrmContext; -import com.zfoo.orm.TestUnit; import com.zfoo.orm.entity.UserEntity; import com.zfoo.orm.model.cache.IEntityCaches; import com.zfoo.util.ThreadUtils; @@ -35,11 +34,11 @@ public class UpdateTest { @Test public void test() { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(TestUnit.CONFIG_LOCATION); + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml"); IEntityCaches userEntityCaches = (IEntityCaches) OrmContext.getOrmManager().getEntityCaches(UserEntity.class); - for (int i = 0; i < TestUnit.NUM; i++) { + for (int i = 0; i < 10; i++) { var userEnt = userEntityCaches.load((long) i); userEnt.setE("update" + i); userEnt.setC(i); @@ -52,7 +51,7 @@ public class UpdateTest { @Test public void collectionTest() { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(TestUnit.CONFIG_LOCATION); + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml"); var collection = OrmContext.getOrmManager().getCollection(UserEntity.class); var result = collection.updateOne(Filters.eq("_id", 1), new Document("$inc", new Document("c", 1L))); diff --git a/orm/src/test/java/com/zfoo/orm/query/QueryTest.java b/orm/src/test/java/com/zfoo/orm/query/QueryTest.java index 3e68a175..f8a1ef64 100644 --- a/orm/src/test/java/com/zfoo/orm/query/QueryTest.java +++ b/orm/src/test/java/com/zfoo/orm/query/QueryTest.java @@ -14,7 +14,6 @@ package com.zfoo.orm.query; import com.zfoo.orm.OrmContext; -import com.zfoo.orm.TestUnit; import com.zfoo.orm.entity.UserEntity; import org.junit.Ignore; import org.junit.Test; @@ -31,14 +30,14 @@ public class QueryTest { @Test public void queryAllTest() { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(TestUnit.CONFIG_LOCATION); + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml"); List list = OrmContext.getQuery().queryAll(UserEntity.class); System.out.println(list); } @Test public void queryByFieldTest() { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(TestUnit.CONFIG_LOCATION); + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml"); List list = OrmContext.getQuery().queryFieldEqual("a", 1, UserEntity.class); System.out.println(list); }