mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-06 16:48:24 +08:00
commit
6eadba4e77
@ -239,8 +239,8 @@ ArticleVo articleVo = articleService.queryChain()
|
||||
```java
|
||||
// 新增 Row 构建
|
||||
DbChain.table("tb_account")
|
||||
.set(RowKey.AUTO)
|
||||
.set("user_name", "王帅")
|
||||
.setId(RowKey.AUTO)
|
||||
.set("user_name", "zhang san")
|
||||
.set("age", 18)
|
||||
.set("birthday", new Date())
|
||||
.save();
|
||||
|
||||
@ -199,9 +199,27 @@ public class MyConfiguration {
|
||||
|
||||
@Bean
|
||||
public LogicDeleteProcessor logicDeleteProcessor(){
|
||||
LogicDeleteProcessor processor = new ....;
|
||||
return processor;
|
||||
return new DateTimeLogicDeleteProcessor();
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
## 全局配置逻辑删除字段
|
||||
|
||||
在 `MyBatis-Flex` 中,可以使用 `FlexGlobalConfig` 在 `MyBatis-Flex` 启动之前,指定项目中的逻辑删除列的列名。
|
||||
|
||||
```java
|
||||
FlexGlobalConfig.getDefaultConfig().setLogicDeleteColumn("del_flag");
|
||||
```
|
||||
|
||||
这样就可以省略实体类属性上的 `@Column(isLogicDelete = true)` 注解了。
|
||||
|
||||
```java
|
||||
public class Account {
|
||||
|
||||
// @Column(isLogicDelete = true)
|
||||
private Boolean delFlag;
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
@ -75,7 +75,7 @@ public class MyConfiguration {
|
||||
TenantFactory tenantFactory = new ....;
|
||||
return tenantFactory;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
@ -162,7 +162,7 @@ System.out.println(tenantAccounts);
|
||||
```java
|
||||
try {
|
||||
TenantManager.ignoreTenantCondition();
|
||||
|
||||
|
||||
//此处操作的数据不会带有 tenant_id 的条件
|
||||
accountMapper.selectListByQuery(...);
|
||||
} finally {
|
||||
@ -171,3 +171,22 @@ try {
|
||||
```
|
||||
|
||||
当然,除此之外,`TenantFactory` 返回空数据,也会忽略 tenant 条件。
|
||||
|
||||
## 全局配置多租户字段
|
||||
|
||||
在 `MyBatis-Flex` 中,可以使用 `FlexGlobalConfig` 在 `MyBatis-Flex` 启动之前,指定项目中的多租户列的列名。
|
||||
|
||||
```java
|
||||
FlexGlobalConfig.getDefaultConfig().setTenantColumn("tenant_id");
|
||||
```
|
||||
|
||||
这样就可以省略实体类属性上的 `@Column(tenantId = true)` 注解了。
|
||||
|
||||
```java
|
||||
public class Account {
|
||||
|
||||
// @Column(tenantId = true)
|
||||
private Integer tenantId;
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
@ -12,7 +12,7 @@
|
||||
更新数据时,执行的 SQL 如下:
|
||||
|
||||
```sql
|
||||
UPDATE account SET nickname = ?, version = version + 1
|
||||
UPDATE account SET nickname = ?, version = version + 1
|
||||
WHERE id = ? AND version = ?
|
||||
```
|
||||
|
||||
@ -28,11 +28,30 @@ public class Account {
|
||||
|
||||
@Column(version = true)
|
||||
private Long version;
|
||||
|
||||
|
||||
//Getter Setter...
|
||||
}
|
||||
```
|
||||
需要注意的是:
|
||||
|
||||
- 1、在同一张表中,只能有一个被 `@Column(version = true)` 修饰的字段。
|
||||
- 2、Account 在插入数据时,若 version 未设置值,那么会自动被 MyBatis-Flex 设置为 0。
|
||||
- 2、Account 在插入数据时,若 version 未设置值,那么会自动被 MyBatis-Flex 设置为 0。
|
||||
|
||||
## 全局配置乐观锁字段
|
||||
|
||||
在 `MyBatis-Flex` 中,可以使用 `FlexGlobalConfig` 在 `MyBatis-Flex` 启动之前,指定项目中的乐观锁列的列名。
|
||||
|
||||
```java
|
||||
FlexGlobalConfig.getDefaultConfig().setVersionColumn("version");
|
||||
```
|
||||
|
||||
这样就可以省略实体类属性上的 `@Column(version = true)` 注解了。
|
||||
|
||||
```java
|
||||
public class Account {
|
||||
|
||||
// @Column(version = true)
|
||||
private Integer version;
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
@ -1,14 +1,13 @@
|
||||
# Gradle 依赖
|
||||
|
||||
> 以下的 xml gradle 依赖示例中,可能并非最新的 MyBatis-Flex 版本,请自行查看最新版本,并修改版本号。
|
||||
>
|
||||
> 建议配置 annotationProcessor,那么可以省略mybatis-flex-processor的依赖
|
||||
>
|
||||
> 以下的 gradle 依赖示例中,可能并非最新的 MyBatis-Flex 版本,请自行查看最新版本,并修改版本号。
|
||||
|
||||
> 建议配置 annotationProcessor,那么可以省略 `mybatis-flex-processor` 的依赖。
|
||||
|
||||
1、只用到了 MyBatis,没用到 Spring 的场景:
|
||||
|
||||
**【Kotlin】**
|
||||
|
||||
```kotlin
|
||||
dependencies {
|
||||
implementation("com.mybatis-flex:mybatis-flex-core:1.5.6")
|
||||
@ -16,6 +15,7 @@ dependencies {
|
||||
```
|
||||
|
||||
**【Groovy】**
|
||||
|
||||
```groovy
|
||||
dependencies {
|
||||
implementation 'com.mybatis-flex:mybatis-flex-core:1.5.6'
|
||||
@ -25,6 +25,7 @@ dependencies {
|
||||
2、用到了 Spring 的场景
|
||||
|
||||
**【Kotlin】**
|
||||
|
||||
```kotlin
|
||||
dependencies {
|
||||
implementation("com.mybatis-flex:mybatis-flex-spring:1.5.6")
|
||||
@ -32,14 +33,17 @@ dependencies {
|
||||
```
|
||||
|
||||
**【Groovy】**
|
||||
|
||||
```groovy
|
||||
dependencies {
|
||||
implementation 'com.mybatis-flex:mybatis-flex-spring:1.5.6'
|
||||
}
|
||||
```
|
||||
|
||||
3、用到了 Spring Boot 的场景
|
||||
|
||||
**【Kotlin】**
|
||||
|
||||
```kotlin
|
||||
dependencies {
|
||||
implementation("com.mybatis-flex:mybatis-flex-spring-boot-starter:1.5.6")
|
||||
@ -47,21 +51,23 @@ dependencies {
|
||||
```
|
||||
|
||||
**【Groovy】**
|
||||
|
||||
```groovy
|
||||
dependencies {
|
||||
implementation 'com.mybatis-flex:mybatis-flex-spring-boot-starter:1.5.6'
|
||||
}
|
||||
```
|
||||
|
||||
4. 配置 annotationProcessor
|
||||
4、配置 annotationProcessor
|
||||
|
||||
`mybatis-flex-processor`提供APT服务。
|
||||
由 `mybatis-flex-processor` 提供APT服务。
|
||||
|
||||
参考:[APT 设置-和 Lombok、Mapstruct 整合](../others/apt.md)
|
||||
参考:[APT 设置-和 Lombok、Mapstruct 整合](../others/apt.md)
|
||||
|
||||
> 在Kotlin中使用时,请参考[在Kotlin中使用注解处理器](../others/kapt.md)
|
||||
|
||||
**【Kotlin】**
|
||||
|
||||
```kotlin
|
||||
dependencies {
|
||||
annotationProcessor("com.mybatis-flex:mybatis-flex-processor:1.5.6")
|
||||
@ -69,6 +75,7 @@ dependencies {
|
||||
```
|
||||
|
||||
**【Groovy】**
|
||||
|
||||
```groovy
|
||||
dependencies {
|
||||
annotationProcessor 'com.mybatis-flex:mybatis-flex-processor:1.5.6'
|
||||
|
||||
@ -1,121 +0,0 @@
|
||||
# 基于 Kotlin 扩展 Mybatis-Flex
|
||||
|
||||
## 快速开始
|
||||
|
||||
在开始之前,我们假定您已经:
|
||||
|
||||
- 熟悉 Kotlin 环境配置及其开发
|
||||
- 熟悉 关系型 数据库,比如 MySQL
|
||||
- 熟悉 Kotlin 构建工具,比如 Gradle、Maven
|
||||
|
||||
> 当前章节涉及到的 [演示源码](https://gitee.com/mybatis-flex/mybatis-flex/tree/main/mybatis-flex-test/mybatis-flex-spring-kotlin-test) 已经全部上传
|
||||
>
|
||||
> 在开始之前,您也可以先下载到本地,导入到 idea 开发工具后,在继续看文档。
|
||||
|
||||
|
||||
## 特点
|
||||
|
||||
- 本模块基于 Mybatis-Flex 核心库 ,只做扩展不做改变
|
||||
- 结合 Kotlin 特性、DSL让数据库操作更简单
|
||||
|
||||
## Hello World 文档
|
||||
|
||||
**第 1 步:创建 Kotlin 项目,并添加 Kotlin 的扩展依赖**
|
||||
|
||||
>如何创建 Kotlin 项目可参考 [官方文档](https://www.kotlincn.net/docs/tutorials/jvm-get-started.html)
|
||||
|
||||
需要添加的主要依赖:
|
||||
|
||||
**【Kotlin】**
|
||||
|
||||
```kotlin
|
||||
dependencies {
|
||||
implementation("com.mybatis-flex:mybatis-flex-kotlin:1.5.7")
|
||||
compileOnly("com.mybatis-flex:mybatis-flex-processor:1.5.7")
|
||||
}
|
||||
```
|
||||
|
||||
**【Maven】**
|
||||
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>com.mybatis-flex</groupId>
|
||||
<artifactId>mybatis-flex-kotlin</artifactId>
|
||||
<version>${mybatis-flex.version}</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
**第 2 步:创建数据库表与配置数据源**
|
||||
|
||||
> 请参考 [快速开始](../intro/getting-started.md) 创建数据库表与配置数据源,
|
||||
> 或者使用演示源码中的内嵌数据库快速体验
|
||||
|
||||
**第 3 步:编写实体类**
|
||||
|
||||
```kotlin
|
||||
@Table("tb_account")
|
||||
class Account {
|
||||
|
||||
@Id
|
||||
var id: Long
|
||||
var userName: String
|
||||
var age: Integer
|
||||
var birthday: Date
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
- 使用 `@Table("tb_account")` 设置实体类与表名的映射关系
|
||||
- 使用 `@Id` 标识主键
|
||||
|
||||
**第 4 步:开始使用**
|
||||
|
||||
添加测试类,进行功能测试:
|
||||
|
||||
```kotlin
|
||||
fun main() {
|
||||
//加载数据源(为了方便演示这里使用了演示源码中的内嵌数据源)
|
||||
val dataSource: DataSource = EmbeddedDatabaseBuilder()
|
||||
.setType(EmbeddedDatabaseType.H2)
|
||||
.addScript("schema.sql")
|
||||
.addScript("data-kt.sql")
|
||||
.build()
|
||||
//启动并配入数据源
|
||||
buildBootstrap { +dataSource }.start()
|
||||
//条件过滤查询并打印
|
||||
filter<Account> {
|
||||
ACCOUNT.ID `=` 1 and
|
||||
(ACCOUNT.AGE `in` listOf(18,19) or (ACCOUNT.BIRTHDAY between ("2020-01-10" to "2020-01-12")) )
|
||||
}.forEach(::println)
|
||||
//查询全部数据并打印
|
||||
//ACCOUNT.all<Account>().forEach(::println)
|
||||
}
|
||||
```
|
||||
执行的SQL:
|
||||
```sql
|
||||
SELECT * FROM `tb_account` WHERE`id` = 1 AND (`age` IN (18, 19) OR `birthday`BETWEEN '2020-01-10' AND '2020-01-12' )
|
||||
```
|
||||
控制台输出:
|
||||
|
||||
```txt
|
||||
Account(id=1, userName=张三, age=18, birthday=Sat Jan 11 00:00:00 CST 2020)
|
||||
```
|
||||
|
||||
> 以上的示例中, `ACCOUNT` 为 MyBatis-Flex 通过 APT
|
||||
> 自动生成,只需通过静态导入即可,无需手动编码。更多查看 [在Kotlin中使用注解处理器](../others/kapt.md)
|
||||
>
|
||||
> 若觉得 APT 使用不习惯,也可以使用代码生成器来生成。点击 [代码生成器文档](../others/codegen.md) 了解。
|
||||
|
||||
[comment]: <> (## 更多使用)
|
||||
|
||||
[comment]: <> (- 功能 1:[Bootstrap简化配置]())
|
||||
|
||||
[comment]: <> (- 功能 2:[简单查询]())
|
||||
|
||||
[comment]: <> (- 功能 3:[表实体扩展]())
|
||||
|
||||
[comment]: <> (- 功能 4:[SQL扩展/中缀]())
|
||||
|
||||
[comment]: <> (- 功能 5:[Mapper扩展]())
|
||||
|
||||
[comment]: <> (###### TODO ...)
|
||||
@ -1,16 +1,15 @@
|
||||
# 在Kotlin中使用注解处理器
|
||||
# 在 Kotlin 中使用注解处理器
|
||||
|
||||
> 在Kotlin中想要使`@Table`等注解生效十分简单。只需要使用kapt即可。
|
||||
>
|
||||
> 在 Kotlin 中想要使 `@Table` 等注解生效十分简单。只需要使用 KAPT 即可。
|
||||
|
||||
## 在Gradle中使用
|
||||
## 在 Gradle 中使用
|
||||
|
||||
1. 应用Gradle插件:kotlin-kapt
|
||||
1、应用 Gradle 插件:`kotlin-kapt`
|
||||
|
||||
**【Kotlin】**
|
||||
|
||||
```kotlin
|
||||
plugins {
|
||||
plugins {
|
||||
kotlin("kapt") version "1.9.0"
|
||||
}
|
||||
```
|
||||
@ -23,8 +22,9 @@ plugins {
|
||||
}
|
||||
```
|
||||
|
||||
2. 在 dependencies 块中使用 kapt 配置添加相应的依赖项
|
||||
**【Kotlin】**
|
||||
2、在 dependencies 块中使用 kapt 配置添加相应的依赖项
|
||||
|
||||
**【Kotlin】**
|
||||
|
||||
```kotlin
|
||||
dependencies {
|
||||
@ -40,12 +40,11 @@ dependencies {
|
||||
}
|
||||
```
|
||||
|
||||
## 在Maven中使用
|
||||
## 在 Maven 中使用
|
||||
|
||||
1. 将以下kapt配置插入指定位置。
|
||||
1、 将以下kapt配置插入指定位置。
|
||||
|
||||
```xml
|
||||
|
||||
<execution>
|
||||
<id>kapt</id>
|
||||
<goals>
|
||||
@ -63,8 +62,7 @@ dependencies {
|
||||
</execution>
|
||||
```
|
||||
|
||||
你需要使kapt在compile前工作。将其插入到`kotlin-maven-plugin`中的compile前,
|
||||
然后将compile的时机改为`process-sources`
|
||||
你需要使 KAPT 在 compile 前工作,将其插入到 `kotlin-maven-plugin` 中的 compile 前,然后将 compile 的时机改为 `process-sources`
|
||||
|
||||
```xml
|
||||
<plugin>
|
||||
@ -73,7 +71,6 @@ dependencies {
|
||||
<version>${kotlin.version}</version>
|
||||
<executions>
|
||||
<!--上述代码需插入到此处,compile前-->
|
||||
|
||||
<execution>
|
||||
<id>compile</id>
|
||||
<!--将此处的phase改为process-sources-->
|
||||
@ -92,16 +89,17 @@ dependencies {
|
||||
</executions>
|
||||
</plugin>
|
||||
```
|
||||
2. 令kapt在构建前运行
|
||||
|
||||
以idea举例
|
||||
1. 点击maven图标
|
||||
2、 令 KAPT 在构建前运行
|
||||
|
||||
以 IDEA 举例:
|
||||
|
||||
1. 点击 Maven 图标
|
||||
2. 找到对应项目
|
||||
3. 点击插件
|
||||
4. 点击kotlin
|
||||
5. 右击kotlin:kapt,在选项中点击"**构建前执行**",以让kapt能够正确的生成代码。
|
||||
4. 点击 kotlin
|
||||
5. 右击 kotlin:kapt,在选项中点击“**构建前执行**”,以让 KAPT 能够正确的生成代码。
|
||||
|
||||

|
||||
|
||||
|
||||
> 关于Kapt更详细的说明,请看[Kotlin官网说明](https://book.kotlincn.net/text/kapt.html)
|
||||
> ,或[Kotlin语言中文站](https://www.kotlincn.net/docs/reference/kapt.html)。
|
||||
> 关于 KAPT 更详细的说明,请看[Kotlin官网说明](https://book.kotlincn.net/text/kapt.html),或[Kotlin语言中文站](https://www.kotlincn.net/docs/reference/kapt.html)。
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user