mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-27 16:24:51 +00:00
perf[orm]: 简化代码
This commit is contained in:
@@ -35,6 +35,12 @@ OrmContext.getAccessor().delete(obj);
|
||||
OrmContext.getAccessor().update(obj);
|
||||
```
|
||||
|
||||
- 加载数据库中的数据
|
||||
|
||||
```
|
||||
OrmContext.getAccessor().load(id, class);
|
||||
```
|
||||
|
||||
#### 2. IQuery接口,为数据复杂查询接口
|
||||
|
||||
#### 3. 缓存使用方法
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
@@ -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<UserEntity> 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<UserEntity>();
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Long, UserEntity> userEntityCaches = (IEntityCaches<Long, UserEntity>) 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);
|
||||
|
||||
@@ -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<UserEntity> 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<UserEntity>() {
|
||||
@Override
|
||||
|
||||
@@ -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<Long, UserEntity> userEntityCaches = (IEntityCaches<Long, UserEntity>) 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);
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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<Long, UserEntity> userEntityCaches = (IEntityCaches<Long, UserEntity>) 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)));
|
||||
|
||||
@@ -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<UserEntity> 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<UserEntity> list = OrmContext.getQuery().queryFieldEqual("a", 1, UserEntity.class);
|
||||
System.out.println(list);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user