mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
!332 docs:增加 Mybatis-Flex-Kotlin 文档
Merge pull request !332 from 卡莫sama/main
This commit is contained in:
commit
965d4ba269
55
docs/zh/intro/use-in-kotlin.md
Normal file
55
docs/zh/intro/use-in-kotlin.md
Normal file
@ -0,0 +1,55 @@
|
||||
# 在 Kotlin 中使用 Mybatis-Flex
|
||||
|
||||
> Mybatis-Flex-Kotlin 是一个 [Mybatis-Flex](https://mybatis-flex.com) 框架的扩展模块,
|
||||
> 它继承了 Mybatis-Flex 轻量的特性,同时拥有 Kotlin 特有的扩展方法、中缀表达式与DSL等语法支持,
|
||||
> 使其拥有了更高的灵活性。让我们可以更加轻松的在 Kotlin 中使用 Mybaits-Flex 所带来的开发效率和开发体验。
|
||||
|
||||
## 特征
|
||||
|
||||
- 轻量:只基于 Mybatis-Flex 核心库 ,只做扩展不做改变
|
||||
- 简明:使用 DSL 让查询语句更加简单明了
|
||||
- 快捷:结合 Kotlin 特性快速对数据库进行操作
|
||||
|
||||
## 亮点
|
||||
|
||||
- 快速构建启动:通过DSL➕重载运算符,快速配置 MybatisFlexBootstrap 实例并启动:
|
||||
```kotlin
|
||||
buildBootstrap {
|
||||
// 配置数据源 相当于 setDataSource(dataSource)
|
||||
+dataSource
|
||||
// 配置Mapper 相当于 addMapper(AccountMapper::class.java)
|
||||
+AccountMapper::class
|
||||
// 配置日志输出 相当于 setLogImpl(StdOutImpl::class.java)
|
||||
logImpl = StdOutImpl::class
|
||||
}.start()
|
||||
```
|
||||
- 快速查询数据:通过DSL➕泛型快速编写查询语句并查询: (快速查询提供三个函数:all, filter 和 query )
|
||||
- `all<Account>()` 查泛型对应的表的所有数据
|
||||
- `filter<Account>(vararg columns: QueryColumn?, condition: ()->QueryCondition)` 按条件查泛型对应的表的数据
|
||||
- `query<Account>(queryScope: QueryScope.()->Unit)` 较复杂查泛型对应的表的数据,如分组排序等
|
||||
- 简明地构建条件:通过中缀表达式➕扩展方法能更加简单明了的构建条件:
|
||||
* **【原生方式】**
|
||||
```kotlin
|
||||
val queryWrapper = QueryWrapper.create()
|
||||
.select(Account::id.column(), Account::userName.column())
|
||||
.where(Account::age.column().`in`(17, 18, 19))
|
||||
.orderBy(Account::id.column().desc())
|
||||
mapper<AccountMapper>().selectListByQuery(queryWrapper)
|
||||
```
|
||||
* **【扩展方式】**
|
||||
```kotlin
|
||||
query<Account> {
|
||||
select { listOf(Account::id, Account::userName) }
|
||||
where { Account::age `in` (17..19) } orderBy -Account::id
|
||||
}
|
||||
```
|
||||
- 摆脱APT: 使用扩展方法摆脱对 APT(注解处理器) 的使用,直接使用属性引用让代码更加灵活优雅:
|
||||
> 使用APT: `ACCOUNT.ID eq 1` ,使用属性引用: `Account::id eq 1`
|
||||
> (少依赖一个模块且不用开启注解处理器功能)
|
||||
- 属性类型约束:使用泛型➕扩展方法对操作的属性进行类型约束:
|
||||
> 如: Account 中 age 属性为 Int 类型,那么 `Account::age between (17 to 19)`而 `Account::age between ("17" to "19")`则会报错提醒
|
||||
## 总结
|
||||
引入 Mybatis-Flex-Kotlin 扩展模块在 Kotlin 中使用 Mybaits-Flex 能够基于 Kotlin 强大的语法特性可以让我们更加轻松方便地操作数据库,极大提高了开发效率和开发体验。
|
||||
> 如何引入 Mybatis-Flex-Kotlin 扩展模块在 Kotlin 中使用 :
|
||||
>* [gitee](https://gitee.com/mybatis-flex/mybatis-flex-kotlin)
|
||||
>* [github](https://github.com/KAMO030/MyBatis-Flex-Kotlin)
|
||||
Loading…
x
Reference in New Issue
Block a user