Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit eee0cf6

Browse files
committedJul 12, 2020
优化文档
1 parent eb9d7e1 commit eee0cf6

File tree

6 files changed

+137
-10
lines changed

6 files changed

+137
-10
lines changed
 

‎README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -593,9 +593,10 @@ gradle :jkmvc-example:build -x test
593593
14. [relation](doc/orm/relation.md)
594594
15. [validation](doc/orm/validation.md)
595595
16. [using](doc/orm/using.md)
596+
17. [relation using callback](doc/orm/cbrelation.md)
596597

597598
## other
598-
17. [deploy](doc/deploy.md)
599+
18. [deploy](doc/deploy.md)
599600

600601
# 中文文档
601602

@@ -620,7 +621,8 @@ gradle :jkmvc-example:build -x test
620621
14. [关联关系](doc/orm/relation.cn.md)
621622
15. [校验](doc/orm/validation.cn.md)
622623
16. [使用](doc/orm/using.cn.md)
624+
17. [基于回调的关联关系](doc/orm/cbrelation.cn.md)
623625

624626
## 其他
625-
17. [部署](doc/deploy.cn.md)
626-
18. [changelog](doc/changelog.md)
627+
18. [部署](doc/deploy.cn.md)
628+
19. [changelog](doc/changelog.md)

‎doc/orm/cbrelation.cn.md

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# 基于回调的关联关系
2+
3+
基于db实现的关联关系, 是通过拼接主键外键条件的联查sql, 来查询关联对象的
4+
5+
而基于回调实现的关联关系, 是使用回调来查询关联对象的.
6+
7+
Jkmvc的ORM模块,支持2种回调实现的关联关系:`hasMany`, `hasOne`
8+
9+
## 1 hasMany 有多个的关系
10+
11+
`hasMany`关系,如 `Post` 模型从属于`User`模型(就是一个文章从属于一个用户)。从用户角度,`User` 模型有多个`Post`模型(就是一个用户有多个文章),我们可以这样定义`hasMany`关系:
12+
13+
```
14+
hasMany("posts" /* 关系名 */, UserModel::id /* 主模型的主键的getter */, PostModel::userId /* 从对象的外键的getter */){ // 批量获取关联对象的回调
15+
PostModel.queryBuilder().where("user_id", "IN", it).findModels<PostModel>()
16+
}
17+
```
18+
让我们来看看方法定义:
19+
20+
```
21+
fun <M:IOrm, K, R> IOrmMeta.cbHasMany(name: String, pkGetter: (M)->K, fkGetter: (R)->K, relatedSupplier:(List<K>) -> List<R>): IOrmMeta
22+
```
23+
24+
接上面的例子,`User`模型的的关系定义如下
25+
26+
```
27+
hasMany("posts", UserModel::id, PostModel::userId){
28+
// 自定义的查询, 可以是rpc
29+
PostModel.queryBuilder().where("user_id", "IN", it).findModels<PostModel>()
30+
}
31+
```
32+
33+
以后,你就可以通过`user["posts"]`来访问`Post`模型(就是某用户的文章啦)
34+
35+
## 2 hasOne 有一个的关系
36+
37+
`hasOne`关系基本等同于`hasMany`关系。只是`hasOne`关系是一对一的,而`hasMany`是一对多。如果一个用户只有一个文章,则我们的代码是这样的:
38+
39+
```
40+
hasOne("story", UserModel::id, PostModel::userId){
41+
// 自定义的查询, 可以是rpc
42+
PostModel.queryBuilder().where("user_id", "IN", it).findModels<PostModel>()
43+
}
44+
```
45+
46+
# 关联对象查询
47+
48+
使用 `OrmQueryBuilder.with()` 来联查关联对象
49+
50+
```
51+
// 联查一对一关联对象
52+
val post = PostModel.queryBuilder()
53+
.with("author")
54+
.where("id", "=", 20)
55+
.findModel<PostModel>();
56+
57+
// -------------------------------
58+
// 联查一对多关联对象
59+
val user = UserModel.queryBuilder()
60+
.with("posts")
61+
.where("id", "=", 20)
62+
.findModel<UserModel>();
63+
```

‎doc/orm/cbrelation.md

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Relations using callback
2+
3+
Relations using db query related objects with sql
4+
5+
Relations using callback query related objects using callback
6+
7+
Jkmvc ORM supports 2 types of callback relations: `hasMany`, `hasOne`.
8+
9+
## 1 hasMany
10+
11+
The standard `hasMany` relation, eg: a post belongs to a user. From the user's perspective, a user has many posts. A hasMany relation is defined below:
12+
13+
```
14+
hasMany("posts" /* relation name */, UserModel::id /* this model's primary key getter */, PostModel::userId /* related model's foreign key getter */){ // callback to query related objects
15+
PostModel.queryBuilder().where("user_id", "IN", it).findModels<PostModel>()
16+
}
17+
```
18+
Let's have a look at the method definition
19+
20+
```
21+
fun <M:IOrm, K, R> IOrmMeta.cbHasMany(name: String, pkGetter: (M)->K, fkGetter: (R)->K, relatedSupplier:(List<K>) -> List<R>): IOrmMeta
22+
```
23+
Again, for our user and post example, this would look like the following in the user model:
24+
25+
```
26+
hasMany("posts", UserModel::id, PostModel::userId){
27+
// you can also call rpc
28+
PostModel.queryBuilder().where("user_id", "IN", it).findModels<PostModel>()
29+
}
30+
```
31+
32+
Using the above, the posts could be access using `user["posts"]`.
33+
34+
## 2 hasOne
35+
36+
A `hasOne` relation is almost identical to a `hasMany` relation. In a `hasOne` relation, there can be 1 and only 1 relation (rather than 1 or more in a hasMany). If a user can only have one post or story, rather than many then the code would look like this:
37+
38+
```
39+
hasOne("story", UserModel::id, PostModel::userId){
40+
// you can also call rpc
41+
PostModel.queryBuilder().where("user_id", "IN", it).findModels<PostModel>()
42+
}
43+
```
44+
45+
# Query related object
46+
47+
use `OrmQueryBuilder.with()` to query related object
48+
49+
```
50+
// Query 1:1 related object, it will merge into 1 sql
51+
val post = PostModel.queryBuilder()
52+
.with("author")
53+
.where("id", "=", 20)
54+
.findModel<PostModel>();
55+
56+
// -------------------------------
57+
// Query 1:N related objects, it will split into 2 sql
58+
val user = UserModel.queryBuilder()
59+
.with("posts")
60+
.where("id", "=", 20)
61+
.findModel<UserModel>();
62+
```

‎doc/orm/relation.cn.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ post.removeRelation('categories', category);
156156

157157
# 关联对象查询
158158

159-
使用 `OrmQueryBuilder.with()` 来联查关联对象
159+
使用 `OrmQueryBuilder.with()` 来联查关联对象, 框架会自动帮你解决联查放大问题
160160

161161
```
162162
// 联查一对一关联对象, 合并为1条sql查询

‎doc/orm/validation.cn.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ Orm模型与[Validation](../common/validation/validation.cn.md)库紧密集成
88

99
每个规则是`net.jkcode.jkmvc.orm.RuleValidator`对象,它有2个属性:
1010
1. `label`:字段中文名
11-
2. `rule`:验证表达式
11+
2. `rule`:验证表达式, 可包含多个, 用空格分割
1212

1313
有两种方法来定义规则
1414
1.重写`OrmMeta :: rules`属性
1515

1616
```
1717
public override val rules: MutableMap<String, IValidator> = hashMapOf(
1818
"userId" to RuleValidator("用户", "notEmpty"),
19-
"age" to RuleValidator( "年龄", "between(1,120)")
19+
"age" to RuleValidator( "年龄", "digit() between(1,120)")
2020
)
2121
```
2222

@@ -25,7 +25,7 @@ public override val rules: MutableMap<String, IValidator> = hashMapOf(
2525
```
2626
// 添加标签 + 规则
2727
addRule("name", "姓名", "notEmpty");
28-
addRule("age", "年龄", "between(1,120)");
28+
addRule("age", "年龄", "digit() between(1,120)");
2929
```
3030

3131
## 2执行验证

‎doc/orm/validation.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ Validation rules are defined in the `OrmMeta::rules` property. This property is
88

99
Each rule is `net.jkcode.jkmvc.orm.RuleValidator` object, which has 2 properties:
1010
1. `label`: A label is a human-readable version of the field name.
11-
2. `rule`: A validation expression
11+
2. `rule`: A validation expressions, split by space
1212

1313
There are 2 way to define rules
1414
1. override `OrmMeta::rules` property
1515

1616
```
1717
public override val rules: MutableMap<String, IValidator> = hashMapOf(
1818
"userId" to RuleValidator("Id label", "notEmpty"),
19-
"age" to RuleValidator( "Age label", "between(1,120)")
19+
"age" to RuleValidator( "Age label", "digit() between(1,120)")
2020
)
2121
```
2222

@@ -25,7 +25,7 @@ public override val rules: MutableMap<String, IValidator> = hashMapOf(
2525
```
2626
// add label and rule for field
2727
addRule("name", "Name label", "notEmpty");
28-
addRule("age", "Age label", "between(1,120)");
28+
addRule("age", "Age label", "digit() between(1,120)");
2929
```
3030

3131
## 2 Execute validation

0 commit comments

Comments
 (0)
Please sign in to comment.