mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
add: 添加主键的配置文档
This commit is contained in:
parent
306e5a579e
commit
af8ddfc362
169
docs/zh/id.md
Normal file
169
docs/zh/id.md
Normal file
@ -0,0 +1,169 @@
|
||||
# Entity 的主键配置
|
||||
|
||||
在 Entity 类中,Mybatis-Flex 是使用 `@Id` 注解来标识主键的,如下代码所示:
|
||||
|
||||
```java
|
||||
@Table("tb_account")
|
||||
public class Account {
|
||||
|
||||
// id 为自增主键
|
||||
@Id(keyType = KeyType.Auto)
|
||||
private Long id;
|
||||
|
||||
//getter setter
|
||||
}
|
||||
```
|
||||
|
||||
`@Id` 注解的内容如下:
|
||||
|
||||
```java
|
||||
public @interface Id {
|
||||
|
||||
/**
|
||||
* ID 生成策略,默认为 none
|
||||
*
|
||||
* @return 生成策略
|
||||
*/
|
||||
KeyType keyType() default KeyType.None;
|
||||
|
||||
/**
|
||||
* 若 keyType 类型是 sequence, value 则代表的是
|
||||
* sequence 序列的 sql 内容
|
||||
* 例如:select SEQ_USER_ID.nextval as id from dual
|
||||
*
|
||||
* 若 keyType 是 Generator,value 则代表的是使用的那个 keyGenerator 的名称
|
||||
*
|
||||
*/
|
||||
String value() default "";
|
||||
|
||||
|
||||
/**
|
||||
* sequence 序列执行顺序
|
||||
* 是在 entity 数据插入之前执行,还是之后执行,之后执行的一般是数据主动生成的 id
|
||||
*
|
||||
* @return 执行之前还是之后
|
||||
*/
|
||||
boolean before() default true;
|
||||
}
|
||||
```
|
||||
|
||||
keyType 为主键的生成方式,KeyType 有 4 种类型:
|
||||
|
||||
```java
|
||||
public enum KeyType {
|
||||
|
||||
/**
|
||||
* 自增的方式
|
||||
*/
|
||||
Auto,
|
||||
|
||||
/**
|
||||
* 通过执行数据库 sql 生成
|
||||
* 例如:select SEQ_USER_ID.nextval as id from dual
|
||||
*/
|
||||
Sequence,
|
||||
|
||||
/**
|
||||
* 通过 IKeyGenerator 生成器生成
|
||||
*/
|
||||
Generator,
|
||||
|
||||
/**
|
||||
* 其他方式,比如说在代码层用户手动设置
|
||||
*/
|
||||
None,
|
||||
}
|
||||
```
|
||||
|
||||
## 多主键、复合主键
|
||||
|
||||
Mybatis-Flex 多主键就是在 Entity 类里有多个 `@Id` 注解标识而已,比如:
|
||||
|
||||
```java
|
||||
@Table("tb_account")
|
||||
public class Account {
|
||||
|
||||
@Id(keyType=KeyType.Auto)
|
||||
private Long id;
|
||||
|
||||
@Id(keyType=KeyType.Generator, value="uuid")
|
||||
private String otherId;
|
||||
|
||||
//getter setter
|
||||
}
|
||||
```
|
||||
当我们保存数据的时候,Account 的 id 主键为自增,而 otherId 主键则通过 uuid 生成。
|
||||
|
||||
## 主键生成器
|
||||
|
||||
第 1 步:编写一个类,实现 `IKeyGenerator` 接口,例如:
|
||||
|
||||
```java
|
||||
public class UUIDKeyGenerator implements IKeyGenerator {
|
||||
|
||||
@Override
|
||||
public Object generate(Object entity, String keyColumn) {
|
||||
return UUID.randomUUID().toString().replace("-", "");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
第 2 步:注册 UUIDKeyGenerator
|
||||
|
||||
```java
|
||||
KeyGeneratorFactory.register("myUUID", new UUIDKeyGenerator());
|
||||
```
|
||||
|
||||
第 3 步:在 Entity 里使用 "myUUID" 生成器:
|
||||
|
||||
```java
|
||||
@Table("tb_account")
|
||||
public class Account {
|
||||
|
||||
@Id(keyType=KeyType.Generator, value="myUUID")
|
||||
private String otherId;
|
||||
|
||||
//getter setter
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## 使用序列 Sequence 生成
|
||||
|
||||
```java
|
||||
@Table("tb_account")
|
||||
public class Account {
|
||||
|
||||
@Id(keyType=KeyType.Sequence, value="select SEQ_USER_ID.nextval as id from dual")
|
||||
private Long id;
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
## 全局配置
|
||||
|
||||
一般的项目中,通常是许多的 Entity 使用同一个数据库,同时使用一种主键生成方式,比如说都使用 自增,
|
||||
或者都使用通过序列(Sequence)生成,此时,我们是没有必要为每个 Entity 单独配置一样内容的。
|
||||
|
||||
Mybatis-Flex 提供了一种全局配置的方式,代码如下:
|
||||
|
||||
```java
|
||||
FlexGlobalConfig.KeyConfig keyConfig = new FlexGlobalConfig.KeyConfig();
|
||||
keyConfig.setKeyType(KeyType.Sequence);
|
||||
keyConfig.setValue("select SEQ_USER_ID.nextval as id from dual")
|
||||
keyConfig.setBefore(true);
|
||||
|
||||
FlexGlobalConfig.getDefaultConfig().setKeyConfig(keyConfig);
|
||||
```
|
||||
|
||||
此时,Entity 类 Account.java 只需要如下配置即可。
|
||||
|
||||
```java
|
||||
@Table("tb_account")
|
||||
public class Account {
|
||||
|
||||
@Id()
|
||||
private Long id;
|
||||
|
||||
}
|
||||
```
|
||||
@ -25,7 +25,7 @@ import java.lang.annotation.*;
|
||||
public @interface Id {
|
||||
|
||||
/**
|
||||
* ID 生成策略,默认为 auto
|
||||
* ID 生成策略,默认为 none
|
||||
*
|
||||
* @return 生成策略
|
||||
*/
|
||||
|
||||
@ -34,7 +34,7 @@
|
||||
@Table("tb_account")
|
||||
public class Account {
|
||||
|
||||
@Id()
|
||||
@Id(keyTYpe = KeyType.Auto)
|
||||
private Long id;
|
||||
private String userName;
|
||||
private Date birthday;
|
||||
@ -282,7 +282,7 @@ QueryWrapper queryWrapper = QueryWrapper.create()
|
||||
|
||||

|
||||
|
||||
> 更多关于 Mybatis-Flex APT 的配置点击 [这里](./docs/zh/apt.md)。
|
||||
> 更多关于 Mybatis-Flex APT 的配置,请点击 [这里](./docs/zh/apt.md)。
|
||||
|
||||
## Db + Row 工具类
|
||||
|
||||
@ -322,7 +322,7 @@ Page<Row> rowPage = Db.paginate("tb_account",3,10,query);
|
||||
>
|
||||
> 具体参考: [Db.java](./mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Db.java) 。
|
||||
>
|
||||
> 更多关于 Row 插入时的**主键生成机制**、以及Db 的**事务管理**等,请移步 [这里](./docs/zh/row_and_db.md) 。
|
||||
> 更多关于 Row 插入时的**主键生成机制**、以及Db 的**事务管理**等,请点击 [这里](./docs/zh/row_and_db.md) 。
|
||||
|
||||
## Entity 部分字段更新
|
||||
|
||||
@ -418,6 +418,8 @@ public class Account {
|
||||
}
|
||||
```
|
||||
|
||||
> 更多关于主键的配置,请点击 [这里](./docs/zh/id.md)
|
||||
|
||||
|
||||
## 更多示例
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user