From af8ddfc3628198829aff0567c2c503a93a09e3e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Sun, 5 Mar 2023 13:00:32 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E6=B7=BB=E5=8A=A0=E4=B8=BB=E9=94=AE?= =?UTF-8?q?=E7=9A=84=E9=85=8D=E7=BD=AE=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/zh/id.md | 169 ++++++++++++++++++ .../java/com/mybatisflex/annotation/Id.java | 2 +- readme_zh.md | 8 +- 3 files changed, 175 insertions(+), 4 deletions(-) create mode 100644 docs/zh/id.md diff --git a/docs/zh/id.md b/docs/zh/id.md new file mode 100644 index 00000000..9cd2227f --- /dev/null +++ b/docs/zh/id.md @@ -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; + +} +``` diff --git a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/Id.java b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/Id.java index 99234d4a..e2903231 100644 --- a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/Id.java +++ b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/Id.java @@ -25,7 +25,7 @@ import java.lang.annotation.*; public @interface Id { /** - * ID 生成策略,默认为 auto + * ID 生成策略,默认为 none * * @return 生成策略 */ diff --git a/readme_zh.md b/readme_zh.md index ead048e6..ee20b692 100644 --- a/readme_zh.md +++ b/readme_zh.md @@ -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() ![](./docs/assets/images/build_idea.png) -> 更多关于 Mybatis-Flex APT 的配置点击 [这里](./docs/zh/apt.md)。 +> 更多关于 Mybatis-Flex APT 的配置,请点击 [这里](./docs/zh/apt.md)。 ## Db + Row 工具类 @@ -322,7 +322,7 @@ Page 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) + ## 更多示例