Skip to content

Commit e60250d

Browse files
committed
Update 注解部分的结构
1 parent 338193a commit e60250d

File tree

5 files changed

+178
-13
lines changed

5 files changed

+178
-13
lines changed

.vuepress/config.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,20 @@ module.exports = {
8989
'v2.x/2.7.logicalmapper.md',
9090
]
9191
},
92-
'v2.x/3.entity.md',
92+
{
93+
title: '3. 实体类注解',
94+
sidebarDepth: 3,
95+
children: [
96+
'v2.x/3.1.entity.md',
97+
'v2.x/3.2.jpa.md',
98+
'v2.x/3.3.jakarta-jpa.md',
99+
'v2.x/3.4.custom.md',
100+
]
101+
},
93102
'v2.x/4.common.md',
94103
'v2.x/5.service.md',
95104
'v2.x/6.activerecord.md',
96105
'v2.x/7.generator.md',
97-
'v2.x/8.jpa.md'
98106
]
99107
},{
100108
title: 'v1.x 使用指南',

docs/v2.x/3.entity.md renamed to docs/v2.x/3.1.entity.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
2-
title: 3. 实体类注解
2+
title: 3.1. 默认注解
33
---
44

55
这部分属于 **mybatis-mapper/provider** 核心部分提供的基础注解,可以直接配合 mapper 使用。
66

7-
## 3.1 注解方法介绍
7+
## 3.1.1 注解方法介绍
88

99
注解提供了大量的配置属性,详细介绍看代码注释:
1010

@@ -133,7 +133,7 @@ public @interface Entity {
133133
}
134134
```
135135

136-
## 3.2 `@Entity.Table` 注解
136+
## 3.1.2 `@Entity.Table` 注解
137137

138138
这个注解除了可以设置表名外,还有下面几个属性:
139139

@@ -162,7 +162,7 @@ public class User {
162162
}
163163
```
164164

165-
## 3.3 `@Entity.Column` 注解
165+
## 3.1.3 `@Entity.Column` 注解
166166

167167
列的注解包含了大量的属性,这些属性涉及到了查询列、插入列、更新列,以及所有可能出现在 `<result>``#{}` 中的参数。
168168
这些参数的含义直接看代码注释。

docs/v2.x/8.jpa.md renamed to docs/v2.x/3.2.jpa.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
title: 8. JPA 注解支持
2+
title: 3.2. JPA 注解
33
---
44

5-
## 8.1 注解介绍
5+
## 3.2.1 注解介绍
66

77
对 JPA 注解的支持非常有限,仅支持以下注解的部分属性:
88
- `@Table`: 用于实体类,必须指定该注解,否则不会被识别为实体类
@@ -24,7 +24,7 @@ title: 8. JPA 注解支持
2424
2525
除了 JPA 这几个注解外,还支持 `@Entity.Table``@Entity.Column` 注解,这些注解可以混用,相同含义的配置中,JPA优先级更高。
2626

27-
## 8.2 JPA 示例
27+
## 3.2.2 JPA 示例
2828

2929
```java
3030
@Table(name = "user")
@@ -41,9 +41,9 @@ public class User {
4141
}
4242
```
4343

44-
## 8.3 扩展实现
44+
## 3.2.3 扩展实现
4545

46-
### 8.3.1 查找实体类类型
46+
### 3.2.3.1 查找实体类类型
4747

4848
首先通过 `JpaEntityClassFinder` 支持添加了 `javax.persistence.Table` 注解的实体类。
4949

@@ -62,7 +62,7 @@ public class JpaEntityClassFinder extends GenericEntityClassFinder {
6262
}
6363
```
6464

65-
### 8.3.2 构建 EntityTable 信息
65+
### 3.2.3.2 构建 EntityTable 信息
6666

6767
然后通过 `JpaEntityTableFactory` 读取 `javax.persistence.Table` 注解的配置信息。
6868

@@ -97,7 +97,7 @@ public class JpaEntityTableFactory implements EntityTableFactory {
9797

9898
如果返回了 `EntityTable`,就用 `javax.persistence.Table` 注解的配置覆盖 `EntityTable` 的配置,也就是 JPA 注解优先级高于默认的 `@Entity.Table` 注解。
9999

100-
### 8.3.3 构建 EntityColumn 信息
100+
### 3.2.3.3 构建 EntityColumn 信息
101101

102102
最后通过 `JpaEntityColumnFactory` 构建 `EntityColumn` 信息,这部分逻辑和上面类似,仍然支持 `@Entity.Column` 注解,而且 JPA 的优先级更高。
103103

docs/v2.x/3.3.jakarta-jpa.md

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
---
2+
title: 3.3. Jakarta JPA 注解
3+
---
4+
5+
## 3.3.1 注解介绍
6+
7+
对 JPA 注解的支持非常有限,仅支持以下注解的部分属性:
8+
- `@Table`: 用于实体类,必须指定该注解,否则不会被识别为实体类
9+
- name: 设置表名
10+
11+
- `@Column`: 设置字段的列信息
12+
- name: 列名
13+
- insertable: 是否可插入
14+
- updatable: 是否可更新
15+
- scale: 小数位数
16+
17+
- `@Id`: 主键标记
18+
19+
- `@Transient`: 排除字段,没有标记该字段的所有字段都会作为表字段处理
20+
21+
- `@OrderBy`: 设置字段排序,空值时为 `ASC`,除此之外,值只能为 `ASC``DESC`
22+
23+
>除了上面提到的有限支持外,其他的 JPA 注解都不支持,如果有好的建议或者应用场景,可以提交 issue 或者 PR 增加额外的支持。
24+
25+
除了 JPA 这几个注解外,还支持 `@Entity.Table``@Entity.Column` 注解,这些注解可以混用,相同含义的配置中,JPA优先级更高。
26+
27+
## 3.3.2 JPA 示例
28+
29+
```java
30+
@Table(name = "user")
31+
public class User {
32+
@Id
33+
@Column
34+
private Long id;
35+
@Column(name = "name")
36+
private String username;
37+
@Column
38+
@Entity.Column(selectable = false)
39+
private String sex;
40+
//忽略其他
41+
}
42+
```
43+
44+
## 3.3.3 扩展实现
45+
46+
### 3.3.3.1 查找实体类类型
47+
48+
首先通过 `JpaEntityClassFinder` 支持添加了 `javax.persistence.Table` 注解的实体类。
49+
50+
```java
51+
public class JpaEntityClassFinder extends GenericEntityClassFinder {
52+
53+
@Override
54+
public boolean isEntityClass(Class<?> clazz) {
55+
return clazz.isAnnotationPresent(Table.class);
56+
}
57+
58+
@Override
59+
public int getOrder() {
60+
return super.getOrder() + 100;
61+
}
62+
}
63+
```
64+
65+
### 3.3.3.2 构建 EntityTable 信息
66+
67+
然后通过 `JpaEntityTableFactory` 读取 `javax.persistence.Table` 注解的配置信息。
68+
69+
```java
70+
public class JpaEntityTableFactory implements EntityTableFactory {
71+
72+
@Override
73+
public EntityTable createEntityTable(Class<?> entityClass, Chain chain) {
74+
EntityTable entityTable = chain.createEntityTable(entityClass);
75+
if (entityClass.isAnnotationPresent(Table.class)) {
76+
Table table = entityClass.getAnnotation(Table.class);
77+
if(entityTable == null) {
78+
entityTable = EntityTable.of(entityClass);
79+
}
80+
if (!table.name().isEmpty()) {
81+
entityTable.table(table.name());
82+
}
83+
}
84+
return entityTable;
85+
}
86+
87+
@Override
88+
public int getOrder() {
89+
return EntityTableFactory.super.getOrder() + 100;
90+
}
91+
92+
}
93+
```
94+
在实现中先执行了 `chain.createEntityTable` 调用工厂链的后续方法创建 `EntityTable`,因此 JPA 注解实现支持和 `@Entity.Table` 注解混用。
95+
96+
如果 `chain` 方法没有返回 `EntityTable`,就根据 `javax.persistence.Table` 注解创建。
97+
98+
如果返回了 `EntityTable`,就用 `javax.persistence.Table` 注解的配置覆盖 `EntityTable` 的配置,也就是 JPA 注解优先级高于默认的 `@Entity.Table` 注解。
99+
100+
### 3.3.3.3 构建 EntityColumn 信息
101+
102+
最后通过 `JpaEntityColumnFactory` 构建 `EntityColumn` 信息,这部分逻辑和上面类似,仍然支持 `@Entity.Column` 注解,而且 JPA 的优先级更高。
103+
104+
```java
105+
public class JpaEntityColumnFactory implements EntityColumnFactory {
106+
107+
@Override
108+
public Optional<List<EntityColumn>> createEntityColumn(EntityTable entityTable, EntityField field, Chain chain) {
109+
//代码太长,省略
110+
}
111+
112+
@Override
113+
public int getOrder() {
114+
return EntityColumnFactory.super.getOrder() + 100;
115+
}
116+
117+
}
118+
```
119+
120+
在上面具体代码的实现中,会先判断 `@Transient` 注解,只要标记了该注解就会忽略。
121+
122+
然后是读取 JPA 中的 `@Column` 注解,支持下面的属性:
123+
- name: 列名
124+
- insertable: 是否可插入
125+
- updatable: 是否可更新
126+
- scale: 小数位数
127+
128+
还支持 JPA 的 `@Id` 设置主键字段。
129+
130+
支持 `@OrderBy` 设置字段的排序,这里有一定的限制,不能指定多个字段,只能为空(默认 `ASC`)或者为 `ASC``DESC`
131+
132+
除了上面提到的有限支持外,其他的 JPA 注解都不支持,如果有好的建议或者应用场景,可以提交 issue 或者 PR 增加额外的支持。

docs/v2.x/3.4.custom.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: 3.4. 自定义注解扩展
3+
---
4+
5+
## 3.4 扩展介绍
6+
7+
对 JPA 注解的支持非常有限,仅支持以下注解的部分属性:
8+
- `@Table`: 用于实体类,必须指定该注解,否则不会被识别为实体类
9+
- name: 设置表名
10+
11+
- `@Column`: 设置字段的列信息
12+
- name: 列名
13+
- insertable: 是否可插入
14+
- updatable: 是否可更新
15+
- scale: 小数位数
16+
17+
- `@Id`: 主键标记
18+
19+
- `@Transient`: 排除字段,没有标记该字段的所有字段都会作为表字段处理
20+
21+
- `@OrderBy`: 设置字段排序,空值时为 `ASC`,除此之外,值只能为 `ASC``DESC`
22+
23+
>除了上面提到的有限支持外,其他的 JPA 注解都不支持,如果有好的建议或者应用场景,可以提交 issue 或者 PR 增加额外的支持。
24+
25+
除了 JPA 这几个注解外,还支持 `@Entity.Table``@Entity.Column` 注解,这些注解可以混用,相同含义的配置中,JPA优先级更高。

0 commit comments

Comments
 (0)