doc[zfoo]: update document

This commit is contained in:
godotg committed 2022-12-02 20:39:13 +08:00
1 parent 2f75896907
commit 44004d5f93
14 files changed
+965 -265

No files matched your search

+55 -43
View File
@@ -1,51 +1,55 @@
### Ⅰ. 简介
English | [简体中文](./README_CN.md)
- [orm](https://github.com/zfoo-project/zfoo/blob/main/orm/README.md) 基于MongoDB的orm框架,提供POJO对象和MongoDB数据库之间的映射
- mongodb是分布式数据库,可以单机使用,也可以分布式使用
- 基于 [caffeine](https://github.com/ben-manes/caffeine) 的高性能数据库实体对象Entity缓存
### Ⅰ. Introduction
### Ⅱ. 使用
- [orm](https://github.com/zfoo-project/zfoo/blob/main/orm/README.md) Based on Mongo DB's orm framework, it provides a
mapping between POJO objects and Mongo DB databases
- MongoDB is a distributed database that can be used on a single machine or distributed in a single machine
- High-performance database entity object caching based on [caffeine](https://github.com/ben-manes/caffeine)
#### 1. 直接使用(low level api),通过MongoDB官方提供的底层Api操作数据库
### Ⅱ. Use
- 首先通过OrmManager获取Collection
#### 1. Direct use (low level API), through the low-level API provided by MongoDB to operate the database
- Start by getting the Collection through Orm Manager
```
var collection = OrmContext.getOrmManager().getCollection(UserEntity.class)
```
- 通过collection操作集合
- 增,collection.insertOne(obj)
- 删,collection.deleteOne(obj)
- 改,collection.updateOne(obj)
- 查,collection.find(Filters.eq("_id", userId))
- Manipulate collections through collections
- insert,collection.insertOne(obj)
- delete,collection.deleteOne(obj)
- update,collection.updateOne(obj)
- query,collection.find(Filters.eq("_id", userId))
#### 2. 间接使用(middle level api),通过collection的简易包装类IAccessor和IQuery接口访问数据
#### 2. Indirect use (middle level API) to access data through the collection's simple wrapper IAccessor and IQuery interfaces
- IAccessor接口,为数据访问接口
- 插入数据到数据库,会以对象的id()方法的返回值作为主键
- IAccessor is a data access interface
- Inserting data into the database uses the return value of the object's id() method as the primary key
```
OrmContext.getAccessor().insert(obj)
```
- 删除数据库中的数据,会以对象的id()方法的返回值作为查找关键字,删除以这个id()为主键的数据
- To delete data in the database, the return value of the id() method of the object will be used as the search
keyword, and the data with this id() as the main key will be deleted
```
OrmContext.getAccessor().delete(obj);
```
- 修改数据库中的数据
- Modify the data in the database
```
OrmContext.getAccessor().update(obj);
```
- 加载数据库中的数据
- Load data from the database
```
OrmContext.getAccessor().load(id, class);
```
- IQuery接口,为数据复杂查询接口
- 目前提供的方法有:queryFieldLike,queryAll,queryFieldEqual,queryFieldIn,pageQuery
- IQuery is data complex query interface
- The methods currently offered are: queryFieldLike,queryAll,queryFieldEqual,queryFieldIn,pageQuery
#### 3. 缓存使用(high level api),缓存Entity定时刷新缓存到数据库
#### 3. Cache usage (high level API), cache entity regular refresh cache to the database
- 通过下列配置指定数据库和缓存的策略列表
- Specify a list of policies for the database and cache through the following configuration
```
<orm:config id="config" entity-package="com.zfoo.orm.**.entity">
@@ -54,14 +58,14 @@ var collection = OrmContext.getOrmManager().getCollection(UserEntity.class)
<orm:address name="${mongodb.address.name}" url="${mongodb.address.url}"/>
</orm:host>
<!-- 缓存策略 -->
<!-- Caching policy -->
<orm:caches>
<orm:cache strategy="default" size="6000" expire-millisecond="600000"/>
<orm:cache strategy="thousand" size="1000" expire-millisecond="600000"/>
<orm:cache strategy="threeThousand" size="3000" expire-millisecond="600000"/>
</orm:caches>
<!-- 持久化策略 -->
<!-- Persistence policy -->
<orm:persisters>
<orm:persister strategy="default" type="time" config="600000"/>
<orm:persister strategy="cron30s" type="cron" config="0/30 * * * * ?"/>
@@ -69,23 +73,26 @@ var collection = OrmContext.getOrmManager().getCollection(UserEntity.class)
</orm:persisters>
</orm:config>
database表示操作哪个数据库
address表示数据库的地址,支持分片的配置
database indicates which database to operate
address indicates the address of the database and supports sharding configuration
```
- 通过注解引入映射
- mappings through annotations
```
@EntityCaches(cacheStrategy = @Cache("tenThousand"), persister = @Persister("time30s"))
public class UserEntity implements IEntity<Long> {
}
cacheStrategy表示一个缓存的策略,即将数据库中的数据先读入Orm中的EntityCaches缓存,如thousand这个策略表示,缓存数据库中1000条数据,10分钟过期
persisters表示一个持久化的策略,如time30s这个策略表示,将EntityCaches中的缓存数据每30s写入到数据库中一次,即使中途宕机,也只损失30s秒的数据
cache strategy represents a caching strategy, the data in the database is first read into the Entity Caches cache in Orm,
such as thousand, which means that 1000 pieces of data in the cache database expire in 10 minutes
persisters represents a persistence strategy, such as time 30 s, which means that the cached data in the Entity Caches is
written to the database every 30 seconds, and even if it goes down midway, only 30 s seconds of data is lost
```
- 缓存的使用方法
1. 通过注解
- How to use the cache
1. By annotation
```
@Component
public class UserManager {
@@ -95,27 +102,32 @@ persisters表示一个持久化的策略,如time30s这个策略表示,将Ent
}
```
2. 通过类动态获取
2. Dynamically obtained through classes
```
IEntityCaches<Long, UserEntity> userEntityCaches = OrmContext.getOrmManager().getEntityCaches(UserEntity.class);
```
- 更新对象
- Update the object
```
userEntityCaches.update(entity);
```
### Ⅲ. 注意事项
### Ⅲ. Notes
- 如果不想映射某属性,直接加上transient关键字
- 支持基本数据属性(byte,short,int,long,float,double,boolean),字符串String,自定义对象,不支持泛型
- 数组支持一维数组,集合支持List,Set
- Map类型MongoDB官方限定了key只能为String
- 数据库主键能用整数尽量用整数,因为MongoDB默认的主键是一个字符串,比较占空间
- 数据库使用自研的orm框架,比如一个实体类UserEntity,映射到数据库中的集合为user,首字母小写,去掉Entity
- 智能语法分析,不支持泛型和循环引用的对象,错误的entity对象定义将无法启动程序并给出错误警告
- If you don't want to map an attribute, just add the transient keyword
- Supports basic data properties (byte, short, int, long, float, double, boolean), string String, custom objects, does
not support generics
- Arrays support one-dimensional arrays, and collections support List, Set
- Map type: Mongo DB officially restricts key to String
- Database primary keys can use integers as much as possible, because Mongo DB's default primary key is a string, which
takes up space
- The database uses a self-developed ORM framework, such as an entity class User Entity, which maps to the collection in
the database as user, with the first letter lowercase, and the Entity removed
- Intelligent parsing, does not support generic and circular referenced objects, and the wrong entity object definition
will fail to start the program with an error warning
### Ⅳ. 教程
### Ⅳ. Tutorial
- [test](https://github.com/zfoo-project/zfoo/tree/main/orm/src/test/java/com/zfoo/orm) 下中包含了所有增删改查的教程,运行之前请先安装MongoDB
- [test](https://github.com/zfoo-project/zfoo/tree/main/orm/src/test/java/com/zfoo/orm) contains all tutorials for
adding, deleting, and modifying, please install Mongo DB before running