From 1ee632fb9217709e82cfb9808cfd6cfc4ad9a2d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Fri, 8 Sep 2023 10:49:51 +0800 Subject: [PATCH 01/47] doc: update docs; close #I7ZKSB close #I7Z48X --- docs/zh/faq.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/docs/zh/faq.md b/docs/zh/faq.md index a33eb47f..01a98bb1 100644 --- a/docs/zh/faq.md +++ b/docs/zh/faq.md @@ -307,3 +307,26 @@ System.out.println("插入成功的主键: " + row.get("my_id")); ## 如何替换 Ruoyi 项目中的 MyBatis 为 MyBatis-Flex ? 参考 issue:https://gitee.com/mybatis-flex/mybatis-flex/issues/I7UX96 + + +## MyBatis-Flex 如何 activiti6 以及 Flowable 等工作流引擎集成? + +当 MyBatis-Flex 与 activiti6 (或者 Flowable)集成时,需要覆盖其自动配置;添加 mybatis-flex 的事务管理器(FlexTransactionManager)和 DataSource(FlexDataSource) +注入到 ProcessEngineConfiguration,配置代码如下: + +```java +@Bean +public ProcessEngineConfiguration processEngineConfiguration( + SqlSessionFactory sqlSessionFactory, + PlatformTransactionManager annotationDrivenTransactionManager) { + + SpringProcessEngineConfiguration processEngineConfiguration = new SpringProcessEngineConfiguration(); + + // 指定 MyBatis-Flex 数据源 + processEngineConfiguration.setDataSource(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource()); + + // 配置 MyBatis-Flex 的事务管理器 + processEngineConfiguration.setTransactionManager(annotationDrivenTransactionManager); + ... +} +``` From 65e2de865fc0e7b3fbbecd213349ce7d5e6803bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Fri, 8 Sep 2023 10:57:58 +0800 Subject: [PATCH 02/47] doc: update docs --- docs/.vitepress/config.ts | 1 + docs/zh/intro/use-in-kotlin.md | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index 5f61e59b..557164ad 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -49,6 +49,7 @@ export default defineConfig({ {text: '快速开始', link: '/zh/intro/getting-started'}, {text: 'Maven 依赖', link: '/zh/intro/maven'}, {text: 'Gradle 依赖', link: '/zh/intro/gradle'}, + {text: 'Kotlin 使用', link: '/zh/intro/use-in-kotlin'}, {text: '和同类框架「功能」对比', link: '/zh/intro/comparison'}, {text: '和同类框架「性能」对比', link: '/zh/intro/benchmark'}, {text: '使用 Mybatis 原生功能', link: '/zh/intro/use-mybatis-native'}, diff --git a/docs/zh/intro/use-in-kotlin.md b/docs/zh/intro/use-in-kotlin.md index 9a4b1530..e1bb9ada 100644 --- a/docs/zh/intro/use-in-kotlin.md +++ b/docs/zh/intro/use-in-kotlin.md @@ -1,8 +1,15 @@ # 在 Kotlin 中使用 Mybatis-Flex -> Mybatis-Flex-Kotlin 是一个 [Mybatis-Flex](https://mybatis-flex.com) 框架的扩展模块, -> 它继承了 Mybatis-Flex 轻量的特性,同时拥有 Kotlin 特有的扩展方法、中缀表达式与DSL等语法支持, -> 使其拥有了更高的灵活性。让我们可以更加轻松的在 Kotlin 中使用 Mybaits-Flex 所带来的开发效率和开发体验。 +MyBatis-Flex-Kotlin 基于 Mybatis-Flex 的 Kotlin 扩展模块,方便 Kotlin 开发者使用 MyBatis-Flex 进行开发。 + + +它继承了 Mybatis-Flex 轻量的特性,同时拥有 Kotlin 特有的扩展方法、中缀表达式与DSL等语法支持, +使其拥有了更高的灵活性。让我们可以更加轻松的在 Kotlin 中使用 Mybaits-Flex 所带来的开发效率和开发体验。 + + + +Git 地址:https://gitee.com/mybatis-flex/mybatis-flex-kotlin + ## 特征 @@ -10,6 +17,11 @@ - 简明:使用 DSL 让查询语句更加简单明了 - 快捷:结合 Kotlin 特性快速对数据库进行操作 +## 快速开始 + +点击链接进入详情: +https://gitee.com/mybatis-flex/mybatis-flex-kotlin#%E5%BF%AB%E9%80%9F%E5%BC%80%E5%A7%8B + ## 亮点 - 快速构建启动:通过DSL➕重载运算符,快速配置 MybatisFlexBootstrap 实例并启动: @@ -48,6 +60,9 @@ > (少依赖一个模块且不用开启注解处理器功能) - 属性类型约束:使用泛型➕扩展方法对操作的属性进行类型约束: > 如: 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 中使用 : From 03ee738e734d9dd35a897acfd1ab19d388e22288 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Fri, 8 Sep 2023 11:00:46 +0800 Subject: [PATCH 03/47] doc: update docs --- docs/zh/intro/use-in-kotlin.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/zh/intro/use-in-kotlin.md b/docs/zh/intro/use-in-kotlin.md index e1bb9ada..5710b342 100644 --- a/docs/zh/intro/use-in-kotlin.md +++ b/docs/zh/intro/use-in-kotlin.md @@ -8,7 +8,7 @@ MyBatis-Flex-Kotlin 基于 Mybatis-Flex 的 Kotlin 扩展模块,方便 Kotlin -Git 地址:https://gitee.com/mybatis-flex/mybatis-flex-kotlin +Git 地址:https://github.com/KAMO030/MyBatis-Flex-Kotlin ## 特征 @@ -20,7 +20,7 @@ Git 地址:https://gitee.com/mybatis-flex/mybatis-flex-kotlin ## 快速开始 点击链接进入详情: -https://gitee.com/mybatis-flex/mybatis-flex-kotlin#%E5%BF%AB%E9%80%9F%E5%BC%80%E5%A7%8B +https://github.com/KAMO030/MyBatis-Flex-Kotlin#%E5%BF%AB%E9%80%9F%E5%BC%80%E5%A7%8B ## 亮点 From 6142aab76b97cbeabc8aa4b053894292af9837cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A1=E8=8E=ABsama?= <837080904@qq.com> Date: Fri, 8 Sep 2023 03:41:34 +0000 Subject: [PATCH 04/47] =?UTF-8?q?update=20docs/zh/intro/use-in-kotlin.md.?= =?UTF-8?q?=20=E6=9B=B4=E6=AD=A3=E8=AF=AD=E5=8F=A5=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E4=B8=8E=E6=8D=A2=E8=A1=8C=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 卡莫sama <837080904@qq.com> --- docs/zh/intro/use-in-kotlin.md | 49 +++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/docs/zh/intro/use-in-kotlin.md b/docs/zh/intro/use-in-kotlin.md index 5710b342..df2ce2bd 100644 --- a/docs/zh/intro/use-in-kotlin.md +++ b/docs/zh/intro/use-in-kotlin.md @@ -35,36 +35,43 @@ https://github.com/KAMO030/MyBatis-Flex-Kotlin#%E5%BF%AB%E9%80%9F%E5%BC%80%E5%A7 logImpl = StdOutImpl::class }.start() ``` + - 快速查询数据:通过DSL➕泛型快速编写查询语句并查询: (快速查询提供三个函数:all, filter 和 query ) - - `all()` 查泛型对应的表的所有数据 - - `filter(vararg columns: QueryColumn?, condition: ()->QueryCondition)` 按条件查泛型对应的表的数据 - - `query(queryScope: QueryScope.()->Unit)` 较复杂查泛型对应的表的数据,如分组排序等 + >- `all()` 查泛型对应的表的所有数据 + >- `filter(vararg columns: QueryColumn?, condition: ()->QueryCondition)` 按条件查泛型对应的表的数据 + >- `query(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().selectListByQuery(queryWrapper) - ``` - * **【扩展方式】** - ```kotlin - query { - select { listOf(Account::id, Account::userName) } - where { Account::age `in` (17..19) } orderBy -Account::id - } - ``` + * **【原生方式】** + ```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().selectListByQuery(queryWrapper) + ``` + * **【扩展方式】** + ```kotlin + query { + 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")`则会报错提醒 - - + > 如: Account 中 age 属性为 Int 类型 + > + > 那么使用between时后续参数也必须是Int: `Account::age between (17 to 19)` + > + > 而如果写成String:`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) From 551cbca6367f378c6071436fa6309d3743022fbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8F=9C=E9=B8=9F3853?= Date: Fri, 8 Sep 2023 14:01:55 +0800 Subject: [PATCH 05/47] doc: update docs --- docs/zh/core/logic-delete.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/zh/core/logic-delete.md b/docs/zh/core/logic-delete.md index 3bac4427..108bff4b 100644 --- a/docs/zh/core/logic-delete.md +++ b/docs/zh/core/logic-delete.md @@ -197,7 +197,7 @@ public interface LogicDeleteProcessor { 针对这种场景,目前有两个方案: -- **方案1:重新 IService 的 removeById 方法** +- **方案1:重写 IService 的 removeById 方法** 先更新 `deleteTime` 和 `deleteUserId`,然后再进行逻辑删除。同时需要保证这两个方法在同一个事务里进行, 如下代码: From 6712f5bd88bb6ea0ee2964506c17d6721f681d79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A1=E8=8E=ABsama?= <837080904@qq.com> Date: Fri, 8 Sep 2023 06:36:42 +0000 Subject: [PATCH 06/47] =?UTF-8?q?update=20docs/zh/intro/use-in-kotlin.md.?= =?UTF-8?q?=20=E5=A2=9E=E5=8A=A0=E6=BC=94=E7=A4=BA=E7=9A=84sql=EF=BC=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96select=E8=B0=83=E7=94=A8=E6=95=88=E6=9E=9C?= =?UTF-8?q?=EF=BC=8C=E6=95=B4=E4=BD=93=E5=B8=83=E5=B1=80=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 卡莫sama <837080904@qq.com> --- docs/zh/intro/use-in-kotlin.md | 44 ++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/docs/zh/intro/use-in-kotlin.md b/docs/zh/intro/use-in-kotlin.md index df2ce2bd..b2e8aa90 100644 --- a/docs/zh/intro/use-in-kotlin.md +++ b/docs/zh/intro/use-in-kotlin.md @@ -1,10 +1,10 @@ # 在 Kotlin 中使用 Mybatis-Flex -MyBatis-Flex-Kotlin 基于 Mybatis-Flex 的 Kotlin 扩展模块,方便 Kotlin 开发者使用 MyBatis-Flex 进行开发。 +**MyBatis-Flex-Kotlin 基于 Mybatis-Flex 的 Kotlin 扩展模块,方便 Kotlin 开发者使用 MyBatis-Flex 进行开发。** -它继承了 Mybatis-Flex 轻量的特性,同时拥有 Kotlin 特有的扩展方法、中缀表达式与DSL等语法支持, -使其拥有了更高的灵活性。让我们可以更加轻松的在 Kotlin 中使用 Mybaits-Flex 所带来的开发效率和开发体验。 +>它继承了 Mybatis-Flex 轻量的特性,同时拥有 Kotlin 特有的扩展方法、中缀表达式与DSL等语法支持, +>使其拥有了更高的灵活性。让我们可以更加轻松的在 Kotlin 中使用 Mybaits-Flex 所带来的开发效率和开发体验。 @@ -35,29 +35,32 @@ https://github.com/KAMO030/MyBatis-Flex-Kotlin#%E5%BF%AB%E9%80%9F%E5%BC%80%E5%A7 logImpl = StdOutImpl::class }.start() ``` - - 快速查询数据:通过DSL➕泛型快速编写查询语句并查询: (快速查询提供三个函数:all, filter 和 query ) >- `all()` 查泛型对应的表的所有数据 >- `filter(vararg columns: QueryColumn?, condition: ()->QueryCondition)` 按条件查泛型对应的表的数据 >- `query(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().selectListByQuery(queryWrapper) - ``` - * **【扩展方式】** - ```kotlin - query { - select { listOf(Account::id, Account::userName) } - where { Account::age `in` (17..19) } orderBy -Account::id - } - ``` + * **【原生方式】** + ```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().selectListByQuery(queryWrapper) + ``` + + * **【扩展方式】** + ```kotlin + query { + select(Account::id, Account::userName) + where { Account::age `in` (17..19) } orderBy -Account::id + } + ``` + > 执行的SQL: + ```sql + SELECT `id`, `user_name` FROM `tb_account` WHERE `age` IN (17, 18, 19) ORDER BY `id` DESC + ``` - 摆脱APT: 使用扩展方法摆脱对 APT(注解处理器) 的使用,直接使用属性引用让代码更加灵活优雅: > 使用APT: `ACCOUNT.ID eq 1` ,使用属性引用: `Account::id eq 1` > @@ -71,7 +74,6 @@ https://github.com/KAMO030/MyBatis-Flex-Kotlin#%E5%BF%AB%E9%80%9F%E5%BC%80%E5%A7 ## 总结 引入 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) From 32d3eefc7dd4ae3dbb64a5c877779ac8407d757a Mon Sep 17 00:00:00 2001 From: ZhuHJay Date: Sat, 9 Sep 2023 18:39:26 +0800 Subject: [PATCH 07/47] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0controller-Solo?= =?UTF-8?q?n.tpl=E6=A8=A1=E6=9D=BF=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../templates/enjoy/controller-Solon.tpl | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 mybatis-flex-codegen/src/main/resources/templates/enjoy/controller-Solon.tpl diff --git a/mybatis-flex-codegen/src/main/resources/templates/enjoy/controller-Solon.tpl b/mybatis-flex-codegen/src/main/resources/templates/enjoy/controller-Solon.tpl new file mode 100644 index 00000000..eda6dbe8 --- /dev/null +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/controller-Solon.tpl @@ -0,0 +1,152 @@ +#set(tableComment = table.getComment()) +#set(entityClassName = table.buildEntityClassName()) +#set(entityVarName = firstCharToLowerCase(entityClassName)) +#set(serviceVarName = firstCharToLowerCase(table.buildServiceClassName())) +package #(packageConfig.controllerPackage); + +import com.mybatisflex.core.paginate.Page; +import org.noear.solon.annotation.*; +import #(packageConfig.entityPackage).#(entityClassName); +import #(packageConfig.servicePackage).#(table.buildServiceClassName()); +#if(controllerConfig.superClass != null) +import #(controllerConfig.buildSuperClassImport()); +#end +#if(withSwagger && swaggerVersion.getName() == "FOX") +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +#end +#if(withSwagger && swaggerVersion.getName() == "DOC") +import io.swagger.v3.oas.annotations.tags.Tag; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +#end +import java.util.List; + +/** + * #(tableComment) 控制层。 + * + * @author #(javadocConfig.getAuthor()) + * @since #(javadocConfig.getSince()) + */ +@Controller +#if(withSwagger && swaggerVersion.getName() == "FOX") +@Api("#(tableComment)接口") +#end +#if(withSwagger && swaggerVersion.getName() == "DOC") +@Tag(name = "#(tableComment)接口") +#end +@Mapping("/#(firstCharToLowerCase(entityClassName))") +public class #(table.buildControllerClassName()) #if(controllerConfig.superClass)extends #(controllerConfig.buildSuperClassName()) #end { + + @Inject + private #(table.buildServiceClassName()) #(serviceVarName); + + /** + * 添加#(tableComment)。 + * + * @param #(entityVarName) #(tableComment) + * @return {@code true} 添加成功,{@code false} 添加失败 + */ + @Post + @Mapping("save") + #if(withSwagger && swaggerVersion.getName() == "FOX") + @ApiOperation("保存#(tableComment)") + #end + #if(withSwagger && swaggerVersion.getName() == "DOC") + @Operation(description="保存#(tableComment)") + #end + public boolean save(@Body #if(withSwagger && swaggerVersion.getName() == "FOX")@ApiParam("#(tableComment)") #end #if(withSwagger && swaggerVersion.getName() == "DOC")@Parameter(description="#(tableComment)")#end #(entityClassName) #(entityVarName)) { + return #(serviceVarName).save(#(entityVarName)); + } + + /** + * 根据主键删除#(tableComment)。 + * + * @param id 主键 + * @return {@code true} 删除成功,{@code false} 删除失败 + */ + @Delete + @Mapping("remove/{id}") + #if(withSwagger && swaggerVersion.getName() == "FOX") + @ApiOperation("根据主键#(tableComment)") + #end + #if(withSwagger && swaggerVersion.getName() == "DOC") + @Operation(description="根据主键#(tableComment)") + #end + public boolean remove(@Path #if(withSwagger && swaggerVersion.getName() == "FOX")@ApiParam("#(tableComment)主键") #end #if(withSwagger && swaggerVersion.getName() == "DOC")@Parameter(description="#(tableComment)主键")#end Long id) { + return #(serviceVarName).removeById(id); + } + + /** + * 根据主键更新#(tableComment)。 + * + * @param #(entityVarName) #(tableComment) + * @return {@code true} 更新成功,{@code false} 更新失败 + */ + @Put + @Mapping("update") + #if(withSwagger && swaggerVersion.getName() == "FOX") + @ApiOperation("根据主键更新#(tableComment)") + #end + #if(withSwagger && swaggerVersion.getName() == "DOC") + @Operation(description="根据主键更新#(tableComment)") + #end + public boolean update(@Body #if(withSwagger && swaggerVersion.getName() == "FOX")@ApiParam("#(tableComment)主键") #end #if(withSwagger && swaggerVersion.getName() == "DOC")@Parameter(description="#(tableComment)主键")#end#(entityClassName) #(entityVarName)) { + return #(serviceVarName).updateById(#(entityVarName)); + } + + /** + * 查询所有#(tableComment)。 + * + * @return 所有数据 + */ + @Get + @Mapping("list") + #if(withSwagger && swaggerVersion.getName() == "FOX") + @ApiOperation("查询所有#(tableComment)") + #end + #if(withSwagger && swaggerVersion.getName() == "DOC") + @Operation(description="查询所有#(tableComment)") + #end + public List<#(entityClassName)> list() { + return #(serviceVarName).list(); + } + + /** + * 根据#(tableComment)主键获取详细信息。 + * + * @param id #(tableComment)主键 + * @return #(tableComment)详情 + */ + @Get + @Mapping("getInfo/{id}") + #if(withSwagger && swaggerVersion.getName() == "FOX") + @ApiOperation("根据主键获取#(tableComment)") + #end + #if(withSwagger && swaggerVersion.getName() == "DOC") + @Operation(description="根据主键获取#(tableComment)") + #end + public #(entityClassName) getInfo(@Path #if(withSwagger && swaggerVersion.getName() == "FOX")@ApiParam("#(tableComment)主键") #if(withSwagger && swaggerVersion.getName() == "DOC")@Parameter(description="#(tableComment)主键")#end#end Long id) { + return #(serviceVarName).getById(id); + } + + /** + * 分页查询#(tableComment)。 + * + * @param page 分页对象 + * @return 分页对象 + */ + @Get + @Mapping("page") + #if(withSwagger && swaggerVersion.getName() == "FOX") + @ApiOperation("分页查询#(tableComment)") + #end + #if(withSwagger && swaggerVersion.getName() == "DOC") + @Operation(description="分页查询#(tableComment)") + #end + public Page<#(entityClassName)> page(#if(withSwagger && swaggerVersion.getName() == "FOX")@ApiParam("分页信息") #end #if(withSwagger && swaggerVersion.getName() == "DOC")@Parameter(description="分页信息")#end Page<#(entityClassName)> page) { + return #(serviceVarName).page(page); + } + +} From 381ceb91f527f06b071b0a8a96986d756e8369e6 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sat, 9 Sep 2023 21:57:11 +0800 Subject: [PATCH 08/47] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20Pull=20Reque?= =?UTF-8?q?st=20=E6=A8=A1=E6=9D=BF=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitee/PULL_REQUEST_TEMPLATE.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 .gitee/PULL_REQUEST_TEMPLATE.md diff --git a/.gitee/PULL_REQUEST_TEMPLATE.md b/.gitee/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 00000000..54c52b85 --- /dev/null +++ b/.gitee/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,23 @@ +## 该 PR 关联的 Issue + +关联 Issue 的解决说明 + +## 修改描述 + +- 第一部分 +- 第二部分 +- 第三部分 + +## 测试用例 + +1. 第一步 +2. 第二步 +3. 第三步 + +## 修复效果 + +附上图片或测试输出,测试输出请用格式: + +> ``` +> 测试输出 +> ``` \ No newline at end of file From 957778240adcd22754a26e437a42efe2fceff47c Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sun, 10 Sep 2023 12:39:08 +0800 Subject: [PATCH 09/47] =?UTF-8?q?feat:=20=E5=8E=9F=E7=94=9F=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E5=88=97=E6=94=AF=E6=8C=81=E5=8D=A0=E4=BD=8D=E7=AC=A6?= =?UTF-8?q?=E4=BC=A0=E5=8F=82=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mybatisflex/core/query/RawQueryColumn.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/RawQueryColumn.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/RawQueryColumn.java index d62a2367..05efb84c 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/RawQueryColumn.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/RawQueryColumn.java @@ -23,13 +23,15 @@ import java.util.List; /** * 自定义字符串列,用于扩展 */ -public class RawQueryColumn extends QueryColumn { +public class RawQueryColumn extends QueryColumn implements HasParamsColumn { protected String content; + protected Object[] params; - public RawQueryColumn(Object content) { + public RawQueryColumn(Object content, Object... params) { this.content = String.valueOf(content); + this.params = params; } @Override @@ -54,4 +56,9 @@ public class RawQueryColumn extends QueryColumn { return (RawQueryColumn) super.clone(); } + @Override + public Object[] getParamValues() { + return params; + } + } From 2d34b1be3d1588fdd72835aa8ef6857f329813a8 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sun, 10 Sep 2023 12:41:51 +0800 Subject: [PATCH 10/47] =?UTF-8?q?feat:=20=E6=9E=84=E5=BB=BA=E5=8E=9F?= =?UTF-8?q?=E7=94=9F=E6=9F=A5=E8=AF=A2=E5=88=97=E6=94=AF=E6=8C=81=E5=8D=A0?= =?UTF-8?q?=E4=BD=8D=E7=AC=A6=E4=BC=A0=E5=8F=82=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/mybatisflex/core/query/QueryMethods.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryMethods.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryMethods.java index 025ef318..f5c409b1 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryMethods.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryMethods.java @@ -2406,8 +2406,8 @@ public class QueryMethods { /** * 构建自定义列。 */ - public static QueryColumn column(String column) { - return new RawQueryColumn(column); + public static QueryColumn column(String column, Object... params) { + return new RawQueryColumn(column, params); } /** From bb9d832014e539eb82e542201db8d5b5169cb877 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sun, 10 Sep 2023 12:42:12 +0800 Subject: [PATCH 11/47] =?UTF-8?q?test:=20=E6=B5=8B=E8=AF=95=E5=8E=9F?= =?UTF-8?q?=E7=94=9F=E6=9F=A5=E8=AF=A2=E5=88=97=E4=BC=A0=E5=8F=82=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mybatisflex/coretest/FunctionSqlTest.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/FunctionSqlTest.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/FunctionSqlTest.java index 350848ca..d5638da9 100644 --- a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/FunctionSqlTest.java +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/FunctionSqlTest.java @@ -86,4 +86,15 @@ public class FunctionSqlTest { System.out.println(sql); } + @Test + public void test06() { + String sql = QueryWrapper.create() + .select(column("(select role_name from tb_role where id = ?)", 1)) + .select(ACCOUNT.USER_NAME) + .from(ACCOUNT) + .toSQL(); + + System.out.println(sql); + } + } From d080c6b2846d14cf0b11d7f9bd2838c9a8f344bb Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sun, 10 Sep 2023 12:42:30 +0800 Subject: [PATCH 12/47] =?UTF-8?q?fix:=20=E4=BF=AE=E6=AD=A3=20toString=20?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/mybatisflex/core/query/RawQueryColumn.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/RawQueryColumn.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/RawQueryColumn.java index 05efb84c..0558b46a 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/RawQueryColumn.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/RawQueryColumn.java @@ -18,6 +18,7 @@ package com.mybatisflex.core.query; import com.mybatisflex.core.dialect.IDialect; +import java.util.Arrays; import java.util.List; /** @@ -48,6 +49,7 @@ public class RawQueryColumn extends QueryColumn implements HasParamsColumn { public String toString() { return "RawQueryColumn{" + "content='" + content + '\'' + + ", params='" + Arrays.toString(params) + '\'' + '}'; } From d34b17119641b5db8462e9d497ede57811dc66f9 Mon Sep 17 00:00:00 2001 From: aqnghu <405705277@qq.com> Date: Mon, 11 Sep 2023 17:26:33 +0000 Subject: [PATCH 13/47] =?UTF-8?q?UpdateEntity=20=E5=8E=BB=E9=99=A4?= =?UTF-8?q?=E6=9C=89=E5=BF=BD=E7=95=A5=E6=B3=A8=E8=A7=A3=E7=9A=84=E5=AD=97?= =?UTF-8?q?=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: aqnghu <405705277@qq.com> --- .../core/update/ModifyAttrsRecordHandler.java | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/ModifyAttrsRecordHandler.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/ModifyAttrsRecordHandler.java index b72f7b2c..604c1d59 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/ModifyAttrsRecordHandler.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/ModifyAttrsRecordHandler.java @@ -15,10 +15,12 @@ */ package com.mybatisflex.core.update; - +import com.mybatisflex.annotation.Column; +import com.mybatisflex.core.util.FieldWrapper; import com.mybatisflex.core.util.StringUtil; import org.apache.ibatis.javassist.util.proxy.MethodHandler; +import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.LinkedHashMap; import java.util.Map; @@ -44,12 +46,29 @@ class ModifyAttrsRecordHandler implements MethodHandler { && originalMethod.getParameterCount() == 1) { String property = StringUtil.firstCharToLowerCase(originalMethod.getName().substring(3)); updates.put(property, args[0]); + + //去除有忽略注解的字段 + Field field = getField(originalMethod.getDeclaringClass(), property); + if (field != null) { + Column column = field.getAnnotation(Column.class); + if (column != null && column.ignore()) { + updates.remove(property); + } + } } return proxyMethod.invoke(self, args); } + + private Field getField(Class clazz, String fieldName) { + try { + FieldWrapper fw = FieldWrapper.of(clazz, fieldName); + if (fw != null) { + return fw.getField(); + } + } catch (Exception ignored) { + } + return null; + } - -} - - +} \ No newline at end of file From 4529e290c5359d4f10f3afc1fe99797a8f15edae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Tue, 12 Sep 2023 11:55:15 +0800 Subject: [PATCH 14/47] perf: optimize ModifyAttrsRecordHandler --- .../core/update/ModifyAttrsRecordHandler.java | 33 ++++++------------- .../mybatisflex/core/util/FieldWrapper.java | 10 ++++++ 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/ModifyAttrsRecordHandler.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/ModifyAttrsRecordHandler.java index 604c1d59..efdb47e4 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/ModifyAttrsRecordHandler.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/ModifyAttrsRecordHandler.java @@ -15,12 +15,10 @@ */ package com.mybatisflex.core.update; -import com.mybatisflex.annotation.Column; import com.mybatisflex.core.util.FieldWrapper; import com.mybatisflex.core.util.StringUtil; import org.apache.ibatis.javassist.util.proxy.MethodHandler; -import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.LinkedHashMap; import java.util.Map; @@ -44,31 +42,20 @@ class ModifyAttrsRecordHandler implements MethodHandler { && methodName.length() > 3 && Character.isUpperCase(methodName.charAt(3)) && originalMethod.getParameterCount() == 1) { - String property = StringUtil.firstCharToLowerCase(originalMethod.getName().substring(3)); - updates.put(property, args[0]); - //去除有忽略注解的字段 - Field field = getField(originalMethod.getDeclaringClass(), property); - if (field != null) { - Column column = field.getAnnotation(Column.class); - if (column != null && column.ignore()) { - updates.remove(property); - } + String property = StringUtil.firstCharToLowerCase(originalMethod.getName().substring(3)); + + //标识 @Column(ignore=true) 的字段,不去更新 + FieldWrapper fw = FieldWrapper.of(originalMethod.getDeclaringClass(), property); + if (fw != null && fw.isIgnore()) { + return proxyMethod.invoke(self, args); } + + updates.put(property, args[0]); } return proxyMethod.invoke(self, args); } - - private Field getField(Class clazz, String fieldName) { - try { - FieldWrapper fw = FieldWrapper.of(clazz, fieldName); - if (fw != null) { - return fw.getField(); - } - } catch (Exception ignored) { - } - return null; - } -} \ No newline at end of file + +} diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/FieldWrapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/FieldWrapper.java index f645e6c2..ad7d3b6b 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/FieldWrapper.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/FieldWrapper.java @@ -15,6 +15,7 @@ */ package com.mybatisflex.core.util; +import com.mybatisflex.annotation.Column; import org.apache.ibatis.reflection.Reflector; import java.lang.reflect.*; @@ -27,6 +28,7 @@ public class FieldWrapper { public static Map, Map> cache = new ConcurrentHashMap<>(); private Field field; + private boolean isIgnore = false; private Class fieldType; private Class mappingType; private Class keyType; @@ -69,6 +71,10 @@ public class FieldWrapper { fieldWrapper.fieldType = findField.getType(); initMappingTypeAndKeyType(clazz, findField, fieldWrapper); + Column column = findField.getAnnotation(Column.class); + if (column != null && column.ignore()) { + fieldWrapper.isIgnore = true; + } fieldWrapper.setterMethod = setter; @@ -138,4 +144,8 @@ public class FieldWrapper { public Field getField() { return field; } + + public boolean isIgnore() { + return isIgnore; + } } From fdf30284b7a1bc6b5432d65ee5125450428f9730 Mon Sep 17 00:00:00 2001 From: nosusume Date: Tue, 12 Sep 2023 22:14:32 +0800 Subject: [PATCH 15/47] =?UTF-8?q?feat:codegen=E9=85=8D=E7=BD=AE=E7=B1=BB?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0Serializable=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mybatisflex/codegen/config/ControllerConfig.java | 7 +++++-- .../java/com/mybatisflex/codegen/config/EntityConfig.java | 3 ++- .../java/com/mybatisflex/codegen/config/GlobalConfig.java | 4 +++- .../java/com/mybatisflex/codegen/config/JavadocConfig.java | 4 +++- .../java/com/mybatisflex/codegen/config/MapperConfig.java | 7 +++++-- .../com/mybatisflex/codegen/config/MapperXmlConfig.java | 7 +++++-- .../java/com/mybatisflex/codegen/config/PackageConfig.java | 5 ++++- .../java/com/mybatisflex/codegen/config/ServiceConfig.java | 7 +++++-- .../com/mybatisflex/codegen/config/ServiceImplConfig.java | 5 ++++- .../com/mybatisflex/codegen/config/StrategyConfig.java | 4 +++- .../java/com/mybatisflex/codegen/config/TableConfig.java | 4 +++- .../com/mybatisflex/codegen/config/TableDefConfig.java | 5 ++++- .../com/mybatisflex/codegen/config/TemplateConfig.java | 5 ++++- 13 files changed, 50 insertions(+), 17 deletions(-) diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ControllerConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ControllerConfig.java index 618fff9a..020c8fb7 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ControllerConfig.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ControllerConfig.java @@ -15,6 +15,8 @@ */ package com.mybatisflex.codegen.config; +import java.io.Serializable; + /** * 生成 Controller 的配置。 * @@ -22,8 +24,9 @@ package com.mybatisflex.codegen.config; * @since 2023-05-15 */ @SuppressWarnings("unused") -public class ControllerConfig { +public class ControllerConfig implements Serializable { + private static final long serialVersionUID = 8391630904705910611L; /** * Controller 类的前缀。 */ @@ -132,4 +135,4 @@ public class ControllerConfig { return this; } -} \ No newline at end of file +} diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/EntityConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/EntityConfig.java index 6636419f..7b517859 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/EntityConfig.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/EntityConfig.java @@ -24,8 +24,9 @@ import java.io.Serializable; * @since 2023-05-15 */ @SuppressWarnings("unused") -public class EntityConfig { +public class EntityConfig implements Serializable { + private static final long serialVersionUID = -6790274333595436008L; /** * Entity 类的前缀。 */ diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java index d7252e6c..4a79d41e 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java @@ -17,6 +17,7 @@ package com.mybatisflex.codegen.config; import com.mybatisflex.codegen.template.ITemplate; +import java.io.Serializable; import java.util.HashMap; import java.util.Map; import java.util.Set; @@ -32,7 +33,8 @@ import java.util.function.UnaryOperator; * @since 2023-05-15 */ @SuppressWarnings("unused") -public class GlobalConfig { +public class GlobalConfig implements Serializable { + private static final long serialVersionUID = 5033600623041298000L; // === 必须配置 === diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/JavadocConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/JavadocConfig.java index a66edaa2..b97e8e1c 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/JavadocConfig.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/JavadocConfig.java @@ -18,6 +18,7 @@ package com.mybatisflex.codegen.config; import com.mybatisflex.core.util.StringUtil; +import java.io.Serializable; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.function.Function; @@ -31,8 +32,9 @@ import java.util.function.UnaryOperator; * @since 2023-05-17 */ @SuppressWarnings("unused") -public class JavadocConfig { +public class JavadocConfig implements Serializable { + private static final long serialVersionUID = -4280345489968397327L; /** * 作者。 */ diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/MapperConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/MapperConfig.java index 83e0bf9e..064310e8 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/MapperConfig.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/MapperConfig.java @@ -17,6 +17,8 @@ package com.mybatisflex.codegen.config; import com.mybatisflex.core.BaseMapper; +import java.io.Serializable; + /** * 生成 Mapper 的配置。 * @@ -24,8 +26,9 @@ import com.mybatisflex.core.BaseMapper; * @since 2023-05-15 */ @SuppressWarnings("unused") -public class MapperConfig { +public class MapperConfig implements Serializable { + private static final long serialVersionUID = 1937442008907641534L; /** * Mapper 类的前缀。 */ @@ -134,4 +137,4 @@ public class MapperConfig { return this; } -} \ No newline at end of file +} diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/MapperXmlConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/MapperXmlConfig.java index dfa5fa47..31216836 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/MapperXmlConfig.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/MapperXmlConfig.java @@ -16,6 +16,8 @@ package com.mybatisflex.codegen.config; +import java.io.Serializable; + /** * 生成 MapperXml 的配置。 * @@ -23,8 +25,9 @@ package com.mybatisflex.codegen.config; * @since 2023-05-17 */ @SuppressWarnings("unused") -public class MapperXmlConfig { +public class MapperXmlConfig implements Serializable { + private static final long serialVersionUID = 7836897652282634412L; /** * MapperXml 文件的前缀。 */ @@ -85,4 +88,4 @@ public class MapperXmlConfig { return this; } -} \ No newline at end of file +} diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/PackageConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/PackageConfig.java index 70aa0b41..69b8770b 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/PackageConfig.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/PackageConfig.java @@ -18,6 +18,8 @@ package com.mybatisflex.codegen.config; import com.mybatisflex.core.util.StringUtil; +import java.io.Serializable; + /** * 生成软件包的配置。 * @@ -25,8 +27,9 @@ import com.mybatisflex.core.util.StringUtil; * @since 2023-05-15 */ @SuppressWarnings("unused") -public class PackageConfig { +public class PackageConfig implements Serializable { + private static final long serialVersionUID = -8257632247633439537L; /** * 代码生成目录。 */ diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ServiceConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ServiceConfig.java index 912ac1c5..d42a64cd 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ServiceConfig.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ServiceConfig.java @@ -18,6 +18,8 @@ package com.mybatisflex.codegen.config; import com.mybatisflex.core.service.IService; +import java.io.Serializable; + /** * 生成 Service 的配置。 * @@ -25,8 +27,9 @@ import com.mybatisflex.core.service.IService; * @since 2023-05-15 */ @SuppressWarnings("unused") -public class ServiceConfig { +public class ServiceConfig implements Serializable { + private static final long serialVersionUID = -2152473328300910220L; /** * Service 类的前缀。 */ @@ -115,4 +118,4 @@ public class ServiceConfig { return this; } -} \ No newline at end of file +} diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ServiceImplConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ServiceImplConfig.java index 3eafaf45..5d705fee 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ServiceImplConfig.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ServiceImplConfig.java @@ -15,6 +15,8 @@ */ package com.mybatisflex.codegen.config; +import java.io.Serializable; + /** * 生成 ServiceImpl 的配置。 * @@ -22,8 +24,9 @@ package com.mybatisflex.codegen.config; * @since 2023-05-15 */ @SuppressWarnings("unused") -public class ServiceImplConfig { +public class ServiceImplConfig implements Serializable { + private static final long serialVersionUID = 17115432462168151L; /** * ServiceImpl 类的前缀。 */ diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/StrategyConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/StrategyConfig.java index 46a8b828..797cf62c 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/StrategyConfig.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/StrategyConfig.java @@ -18,6 +18,7 @@ package com.mybatisflex.codegen.config; import com.mybatisflex.core.util.StringUtil; +import java.io.Serializable; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -30,8 +31,9 @@ import java.util.Set; * @since 2023-05-14 */ @SuppressWarnings("unused") -public class StrategyConfig { +public class StrategyConfig implements Serializable { + private static final long serialVersionUID = 504853587703061034L; /** * 数据库表前缀,多个前缀用英文逗号(,) 隔开。 */ diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TableConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TableConfig.java index 2354122a..27b41045 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TableConfig.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TableConfig.java @@ -19,6 +19,7 @@ import com.mybatisflex.annotation.InsertListener; import com.mybatisflex.annotation.SetListener; import com.mybatisflex.annotation.UpdateListener; +import java.io.Serializable; import java.util.HashMap; import java.util.Map; @@ -26,9 +27,10 @@ import java.util.Map; * 表的单独设置。 */ @SuppressWarnings({"unused", "UnusedReturnValue"}) -public class TableConfig { +public class TableConfig implements Serializable { public static final String ALL_TABLES = "*"; + private static final long serialVersionUID = -2568968178699265858L; /** * 数据库的 schema(模式)。 diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TableDefConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TableDefConfig.java index 207feda6..c2e9ed41 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TableDefConfig.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TableDefConfig.java @@ -17,6 +17,8 @@ package com.mybatisflex.codegen.config; import com.mybatisflex.core.util.StringUtil; +import java.io.Serializable; + /** * 生成 TableDef 的配置。 * @@ -24,8 +26,9 @@ import com.mybatisflex.core.util.StringUtil; * @since 2023-05-15 */ @SuppressWarnings("unused") -public class TableDefConfig { +public class TableDefConfig implements Serializable { + private static final long serialVersionUID = 8137903163796008036L; /** * TableDef 类的前缀。 */ diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TemplateConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TemplateConfig.java index ba135c39..205aca5a 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TemplateConfig.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TemplateConfig.java @@ -20,6 +20,8 @@ import com.mybatisflex.codegen.generator.GeneratorFactory; import com.mybatisflex.codegen.template.ITemplate; import com.mybatisflex.codegen.template.impl.EnjoyTemplate; +import java.io.Serializable; + /** * 模板配置。 * @@ -27,8 +29,9 @@ import com.mybatisflex.codegen.template.impl.EnjoyTemplate; * @since 2023-05-17 */ @SuppressWarnings("unused") -public class TemplateConfig { +public class TemplateConfig implements Serializable { + private static final long serialVersionUID = 6700855804948021101L; /** * 生成代码的模板引擎。 */ From e72f62df50be57e4396ad35b16b249eb8e0afb70 Mon Sep 17 00:00:00 2001 From: life <13122192336@163.com> Date: Wed, 13 Sep 2023 17:48:22 +0800 Subject: [PATCH 16/47] =?UTF-8?q?=E5=85=BC=E5=AE=B9=E6=9E=9A=E4=B8=BE?= =?UTF-8?q?=E4=BD=BF=E7=94=A8flex=E9=BB=98=E8=AE=A4=E7=9A=84=E6=9E=9A?= =?UTF-8?q?=E4=B8=BE=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/mybatis/FlexConfiguration.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java index 396e8040..e1b85578 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java @@ -15,6 +15,7 @@ */ package com.mybatisflex.core.mybatis; +import com.mybatisflex.annotation.Table; import com.mybatisflex.core.FlexConsts; import com.mybatisflex.core.handler.CompositeEnumTypeHandler; import com.mybatisflex.core.keygen.MultiEntityKeyGenerator; @@ -28,6 +29,7 @@ import com.mybatisflex.core.row.RowMapper; import com.mybatisflex.core.table.TableInfo; import com.mybatisflex.core.table.TableInfoFactory; import com.mybatisflex.core.util.StringUtil; +import java.util.List; import org.apache.ibatis.executor.CachingExecutor; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.executor.keygen.KeyGenerator; @@ -53,6 +55,7 @@ import java.util.concurrent.ConcurrentHashMap; /** * @author michael + * @author life */ public class FlexConfiguration extends Configuration { @@ -177,8 +180,17 @@ public class FlexConfiguration extends Configuration { else if (StringUtil.endsWithAny(ms.getId(), "selectOneById", "selectListByIds" , "selectListByQuery", "selectCursorByQuery")) { ms = replaceResultMap(ms, getTableInfo(ms)); + }else{ + List resultMaps1 = ms.getResultMaps(); + //根据resultMap里面的class进行 + for (ResultMap resultMap:resultMaps1) { + //获取结果的类型 + Class clazz =resultMap.getType(); + if (clazz.getDeclaredAnnotation(Table.class) !=null){//判断是否为表实体类 + ms = replaceResultMap(ms, getTableInfo(ms)); + } + } } - super.addMappedStatement(ms); } From 08894355c98f0162705f5ed1384da93fdcc78005 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Wed, 13 Sep 2023 19:37:24 +0800 Subject: [PATCH 17/47] style: format style --- .../core/mybatis/FlexConfiguration.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java index e1b85578..13e6f1a3 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java @@ -29,7 +29,6 @@ import com.mybatisflex.core.row.RowMapper; import com.mybatisflex.core.table.TableInfo; import com.mybatisflex.core.table.TableInfoFactory; import com.mybatisflex.core.util.StringUtil; -import java.util.List; import org.apache.ibatis.executor.CachingExecutor; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.executor.keygen.KeyGenerator; @@ -50,6 +49,7 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Proxy; import java.lang.reflect.Type; import java.util.Collections; +import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -180,13 +180,14 @@ public class FlexConfiguration extends Configuration { else if (StringUtil.endsWithAny(ms.getId(), "selectOneById", "selectListByIds" , "selectListByQuery", "selectCursorByQuery")) { ms = replaceResultMap(ms, getTableInfo(ms)); - }else{ - List resultMaps1 = ms.getResultMaps(); - //根据resultMap里面的class进行 - for (ResultMap resultMap:resultMaps1) { + } else { + List resultMaps = ms.getResultMaps(); + //根据 resultMap 里面的 class 进行判断 + for (ResultMap resultMap : resultMaps) { //获取结果的类型 - Class clazz =resultMap.getType(); - if (clazz.getDeclaredAnnotation(Table.class) !=null){//判断是否为表实体类 + Class clazz = resultMap.getType(); + //判断是否为表实体类 + if (clazz.getDeclaredAnnotation(Table.class) != null) { ms = replaceResultMap(ms, getTableInfo(ms)); } } From d0db506078f49d730688652291a3cb6c7bdcdaf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8B=93=E5=AE=87=E5=9C=A8=E6=80=9D=E8=80=83?= Date: Thu, 14 Sep 2023 01:13:48 +0000 Subject: [PATCH 18/47] =?UTF-8?q?update=20docs/zh/base/auto-mapping.md.=20?= =?UTF-8?q?fix=20=E6=95=B2=E9=94=99=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 拓宇在思考 --- docs/zh/base/auto-mapping.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/zh/base/auto-mapping.md b/docs/zh/base/auto-mapping.md index 360c7186..1ccec292 100644 --- a/docs/zh/base/auto-mapping.md +++ b/docs/zh/base/auto-mapping.md @@ -71,7 +71,7 @@ public class Account { `AccountMapper` 方法直接查询,例如: ```java -QueryWrapper qw = new QeuryWrapper(); +QueryWrapper qw = new QueryWrapper(); qw.select(ACCOUNT.ALL_COLUMNS) .where(ACCOUNT.ID.ge(100)); From ff40f421e7ca813bf1ef12705e8fd33ef4062748 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Thu, 14 Sep 2023 09:46:58 +0800 Subject: [PATCH 19/47] build: v1.6.5 release (^.^)YYa!! --- mybatis-flex-annotation/pom.xml | 2 +- mybatis-flex-codegen/pom.xml | 2 +- mybatis-flex-core/pom.xml | 2 +- .../src/main/java/com/mybatisflex/core/FlexConsts.java | 2 +- mybatis-flex-dependencies/pom.xml | 2 +- mybatis-flex-processor/pom.xml | 2 +- mybatis-flex-solon-plugin/pom.xml | 2 +- mybatis-flex-spring-boot-starter/pom.xml | 2 +- mybatis-flex-spring/pom.xml | 2 +- mybatis-flex-test/mybatis-flex-native-test/pom.xml | 2 +- mybatis-flex-test/mybatis-flex-seata-test/pom.xml | 2 +- mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml | 2 +- mybatis-flex-test/mybatis-flex-spring-cloud-test/pom.xml | 2 +- mybatis-flex-test/mybatis-flex-spring-test/pom.xml | 2 +- mybatis-flex-test/pom.xml | 2 +- 15 files changed, 15 insertions(+), 15 deletions(-) diff --git a/mybatis-flex-annotation/pom.xml b/mybatis-flex-annotation/pom.xml index 3579675e..716ad913 100644 --- a/mybatis-flex-annotation/pom.xml +++ b/mybatis-flex-annotation/pom.xml @@ -5,7 +5,7 @@ parent com.mybatis-flex - 1.6.4 + 1.6.5 4.0.0 diff --git a/mybatis-flex-codegen/pom.xml b/mybatis-flex-codegen/pom.xml index e3d8bfd8..7a576415 100644 --- a/mybatis-flex-codegen/pom.xml +++ b/mybatis-flex-codegen/pom.xml @@ -5,7 +5,7 @@ parent com.mybatis-flex - 1.6.4 + 1.6.5 4.0.0 diff --git a/mybatis-flex-core/pom.xml b/mybatis-flex-core/pom.xml index f2cfd7e8..9003cbcb 100644 --- a/mybatis-flex-core/pom.xml +++ b/mybatis-flex-core/pom.xml @@ -5,7 +5,7 @@ parent com.mybatis-flex - 1.6.4 + 1.6.5 4.0.0 diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexConsts.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexConsts.java index 37fe3411..74ddd48e 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexConsts.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexConsts.java @@ -27,7 +27,7 @@ public class FlexConsts { } public static final String NAME = "MyBatis-Flex"; - public static final String VERSION = "1.6.4"; + public static final String VERSION = "1.6.5"; public static final String SQL = "$$sql"; diff --git a/mybatis-flex-dependencies/pom.xml b/mybatis-flex-dependencies/pom.xml index 38d10729..69524f00 100644 --- a/mybatis-flex-dependencies/pom.xml +++ b/mybatis-flex-dependencies/pom.xml @@ -6,7 +6,7 @@ com.mybatis-flex parent - 1.6.4 + 1.6.5 mybatis-flex-dependencies diff --git a/mybatis-flex-processor/pom.xml b/mybatis-flex-processor/pom.xml index fc02cfb3..1c0b06d7 100644 --- a/mybatis-flex-processor/pom.xml +++ b/mybatis-flex-processor/pom.xml @@ -5,7 +5,7 @@ parent com.mybatis-flex - 1.6.4 + 1.6.5 4.0.0 diff --git a/mybatis-flex-solon-plugin/pom.xml b/mybatis-flex-solon-plugin/pom.xml index 88a1d3f8..e36bafa1 100644 --- a/mybatis-flex-solon-plugin/pom.xml +++ b/mybatis-flex-solon-plugin/pom.xml @@ -5,7 +5,7 @@ parent com.mybatis-flex - 1.6.4 + 1.6.5 4.0.0 diff --git a/mybatis-flex-spring-boot-starter/pom.xml b/mybatis-flex-spring-boot-starter/pom.xml index 4fd9879d..04211803 100644 --- a/mybatis-flex-spring-boot-starter/pom.xml +++ b/mybatis-flex-spring-boot-starter/pom.xml @@ -5,7 +5,7 @@ parent com.mybatis-flex - 1.6.4 + 1.6.5 4.0.0 diff --git a/mybatis-flex-spring/pom.xml b/mybatis-flex-spring/pom.xml index 385adad7..51663020 100644 --- a/mybatis-flex-spring/pom.xml +++ b/mybatis-flex-spring/pom.xml @@ -5,7 +5,7 @@ parent com.mybatis-flex - 1.6.4 + 1.6.5 4.0.0 diff --git a/mybatis-flex-test/mybatis-flex-native-test/pom.xml b/mybatis-flex-test/mybatis-flex-native-test/pom.xml index b158875b..7a228f4f 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/pom.xml +++ b/mybatis-flex-test/mybatis-flex-native-test/pom.xml @@ -5,7 +5,7 @@ mybatis-flex-test com.mybatis-flex - 1.6.4 + 1.6.5 4.0.0 diff --git a/mybatis-flex-test/mybatis-flex-seata-test/pom.xml b/mybatis-flex-test/mybatis-flex-seata-test/pom.xml index be8bc9c1..5258a777 100644 --- a/mybatis-flex-test/mybatis-flex-seata-test/pom.xml +++ b/mybatis-flex-test/mybatis-flex-seata-test/pom.xml @@ -4,7 +4,7 @@ mybatis-flex-test com.mybatis-flex - 1.6.4 + 1.6.5 4.0.0 diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml b/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml index f3f5fb40..07b77ed5 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml @@ -5,7 +5,7 @@ mybatis-flex-test com.mybatis-flex - 1.6.4 + 1.6.5 4.0.0 diff --git a/mybatis-flex-test/mybatis-flex-spring-cloud-test/pom.xml b/mybatis-flex-test/mybatis-flex-spring-cloud-test/pom.xml index 1a5e337e..030feed2 100644 --- a/mybatis-flex-test/mybatis-flex-spring-cloud-test/pom.xml +++ b/mybatis-flex-test/mybatis-flex-spring-cloud-test/pom.xml @@ -4,7 +4,7 @@ mybatis-flex-test com.mybatis-flex - 1.6.4 + 1.6.5 4.0.0 diff --git a/mybatis-flex-test/mybatis-flex-spring-test/pom.xml b/mybatis-flex-test/mybatis-flex-spring-test/pom.xml index 0e3ac469..733cc45c 100644 --- a/mybatis-flex-test/mybatis-flex-spring-test/pom.xml +++ b/mybatis-flex-test/mybatis-flex-spring-test/pom.xml @@ -5,7 +5,7 @@ mybatis-flex-test com.mybatis-flex - 1.6.4 + 1.6.5 4.0.0 diff --git a/mybatis-flex-test/pom.xml b/mybatis-flex-test/pom.xml index b595cbcd..39f7d108 100644 --- a/mybatis-flex-test/pom.xml +++ b/mybatis-flex-test/pom.xml @@ -5,7 +5,7 @@ parent com.mybatis-flex - 1.6.4 + 1.6.5 4.0.0 From 6f3f91460a986fcdda3f307d8af75258e3cf24ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Thu, 14 Sep 2023 09:47:06 +0800 Subject: [PATCH 20/47] build: v1.6.5 release (^.^)YYa!! --- changes.md | 21 +++++++++++++++++++++ docs/zh/changes.md | 21 +++++++++++++++++++++ docs/zh/intro/getting-started.md | 2 +- docs/zh/intro/gradle.md | 16 ++++++++-------- docs/zh/intro/maven.md | 14 +++++++------- docs/zh/others/apt.md | 2 +- docs/zh/others/codegen.md | 2 +- pom.xml | 4 ++-- 8 files changed, 62 insertions(+), 20 deletions(-) diff --git a/changes.md b/changes.md index afc80ae3..b0d00571 100644 --- a/changes.md +++ b/changes.md @@ -3,6 +3,27 @@ 查看 [全部代码贡献者](/zh/intro/what-is-mybatisflex.html#贡献者)。 +## v1.6.5 20230914: +- 新增:代码生成器为 Oracle 的 JdbcTypeMapping 类型 OracleBlob 添加映射处理 +- 新增:LogicDeleteManager 和 TenantManager 添加 Runnable 无返回值重载,感谢 @Suomm +- 新增:RawQueryColumn 添加参数占位符的支持功能,感谢 @Suomm +- 新增:代码生成器添加关于 solon Controller 生成的代码模板,感谢 @ZhuHJay +- 新增:UpdateEntity 添加自动去除有忽略注解的字段的功能,感谢 @aqnghu +- 优化:代码生成器配置类添加 Serializable 接口实现的支持,方便自定义缓存保存,感谢 @zoufang162 +- 优化:使用 lambda 优化部分写法,感谢 @handy-git +- 优化:使用 try-with-resources 释放 Connection,感谢 @handy-git +- 优化:DataSourceBuilder 出错时,吞掉原始的 exception 的 message 信息的问题 #I7YYRF +- 优化:`@Table` 注解增加 `@Inherited` 修饰,感谢 @jerryzhengsz1 +- 修复:当工作流引擎 activti6 整合 MyBatis-Flex 可能出现 NPE 的问题 +- 修复:通过 XML 自定义的 SQL 查询不兼容自定义枚举使用的问题,感谢 @lifejwang11 +- 文档:更新关于 MyBatis-Flex-Admin 的相关文档 +- 文档:添加关于 MyBatis-Flex-Kotlin 的相关文档 感谢 @kamo-sama +- 文档:修正自定义脱敏处理器的示例代码错误,感谢 @wang_yong_ji +- 文档:添加关于 MyBatis-Flex 与 activiti6 以及 Flowable 等工作流引擎集成的相关文档,感谢 @simple_wind +- 文档:修复逻辑删除文档的个别错别字,感谢 @cainiao3853 +- 文档:修正自定义映射的相关代码示例错误,感谢 @tycms + + ## v1.6.4 20230903: - 新增:QueryWrapper 添加动态排序功能的支持,感谢 @Suomm diff --git a/docs/zh/changes.md b/docs/zh/changes.md index afc80ae3..b0d00571 100644 --- a/docs/zh/changes.md +++ b/docs/zh/changes.md @@ -3,6 +3,27 @@ 查看 [全部代码贡献者](/zh/intro/what-is-mybatisflex.html#贡献者)。 +## v1.6.5 20230914: +- 新增:代码生成器为 Oracle 的 JdbcTypeMapping 类型 OracleBlob 添加映射处理 +- 新增:LogicDeleteManager 和 TenantManager 添加 Runnable 无返回值重载,感谢 @Suomm +- 新增:RawQueryColumn 添加参数占位符的支持功能,感谢 @Suomm +- 新增:代码生成器添加关于 solon Controller 生成的代码模板,感谢 @ZhuHJay +- 新增:UpdateEntity 添加自动去除有忽略注解的字段的功能,感谢 @aqnghu +- 优化:代码生成器配置类添加 Serializable 接口实现的支持,方便自定义缓存保存,感谢 @zoufang162 +- 优化:使用 lambda 优化部分写法,感谢 @handy-git +- 优化:使用 try-with-resources 释放 Connection,感谢 @handy-git +- 优化:DataSourceBuilder 出错时,吞掉原始的 exception 的 message 信息的问题 #I7YYRF +- 优化:`@Table` 注解增加 `@Inherited` 修饰,感谢 @jerryzhengsz1 +- 修复:当工作流引擎 activti6 整合 MyBatis-Flex 可能出现 NPE 的问题 +- 修复:通过 XML 自定义的 SQL 查询不兼容自定义枚举使用的问题,感谢 @lifejwang11 +- 文档:更新关于 MyBatis-Flex-Admin 的相关文档 +- 文档:添加关于 MyBatis-Flex-Kotlin 的相关文档 感谢 @kamo-sama +- 文档:修正自定义脱敏处理器的示例代码错误,感谢 @wang_yong_ji +- 文档:添加关于 MyBatis-Flex 与 activiti6 以及 Flowable 等工作流引擎集成的相关文档,感谢 @simple_wind +- 文档:修复逻辑删除文档的个别错别字,感谢 @cainiao3853 +- 文档:修正自定义映射的相关代码示例错误,感谢 @tycms + + ## v1.6.4 20230903: - 新增:QueryWrapper 添加动态排序功能的支持,感谢 @Suomm diff --git a/docs/zh/intro/getting-started.md b/docs/zh/intro/getting-started.md index 4c3d8e62..816fe812 100644 --- a/docs/zh/intro/getting-started.md +++ b/docs/zh/intro/getting-started.md @@ -62,7 +62,7 @@ VALUES (1, '张三', 18, '2020-01-11'), com.mybatis-flex mybatis-flex-spring-boot-starter - 1.6.4 + 1.6.5 com.mysql diff --git a/docs/zh/intro/gradle.md b/docs/zh/intro/gradle.md index 5e34b50e..014113c4 100644 --- a/docs/zh/intro/gradle.md +++ b/docs/zh/intro/gradle.md @@ -10,7 +10,7 @@ ```kotlin dependencies { - implementation("com.mybatis-flex:mybatis-flex-core:1.6.4") + implementation("com.mybatis-flex:mybatis-flex-core:1.6.5") } ``` @@ -18,7 +18,7 @@ dependencies { ```groovy dependencies { - implementation 'com.mybatis-flex:mybatis-flex-core:1.6.4' + implementation 'com.mybatis-flex:mybatis-flex-core:1.6.5' } ``` @@ -28,7 +28,7 @@ dependencies { ```kotlin dependencies { - implementation("com.mybatis-flex:mybatis-flex-spring:1.6.4") + implementation("com.mybatis-flex:mybatis-flex-spring:1.6.5") } ``` @@ -36,7 +36,7 @@ dependencies { ```groovy dependencies { - implementation 'com.mybatis-flex:mybatis-flex-spring:1.6.4' + implementation 'com.mybatis-flex:mybatis-flex-spring:1.6.5' } ``` @@ -46,7 +46,7 @@ dependencies { ```kotlin dependencies { - implementation("com.mybatis-flex:mybatis-flex-spring-boot-starter:1.6.4") + implementation("com.mybatis-flex:mybatis-flex-spring-boot-starter:1.6.5") } ``` @@ -54,7 +54,7 @@ dependencies { ```groovy dependencies { - implementation 'com.mybatis-flex:mybatis-flex-spring-boot-starter:1.6.4' + implementation 'com.mybatis-flex:mybatis-flex-spring-boot-starter:1.6.5' } ``` @@ -70,7 +70,7 @@ dependencies { ```kotlin dependencies { - annotationProcessor("com.mybatis-flex:mybatis-flex-processor:1.6.4") + annotationProcessor("com.mybatis-flex:mybatis-flex-processor:1.6.5") } ``` @@ -78,6 +78,6 @@ dependencies { ```groovy dependencies { - annotationProcessor 'com.mybatis-flex:mybatis-flex-processor:1.6.4' + annotationProcessor 'com.mybatis-flex:mybatis-flex-processor:1.6.5' } ``` diff --git a/docs/zh/intro/maven.md b/docs/zh/intro/maven.md index f00b4318..4f8c0078 100644 --- a/docs/zh/intro/maven.md +++ b/docs/zh/intro/maven.md @@ -12,12 +12,12 @@ com.mybatis-flex mybatis-flex-core - 1.6.4 + 1.6.5 com.mybatis-flex mybatis-flex-processor - 1.6.4 + 1.6.5 provided ``` @@ -28,12 +28,12 @@ com.mybatis-flex mybatis-flex-spring - 1.6.4 + 1.6.5 com.mybatis-flex mybatis-flex-processor - 1.6.4 + 1.6.5 provided `````` @@ -44,12 +44,12 @@ com.mybatis-flex mybatis-flex-spring-boot-starter - 1.6.4 + 1.6.5 com.mybatis-flex mybatis-flex-processor - 1.6.4 + 1.6.5 provided ``` @@ -72,7 +72,7 @@ com.mybatis-flex mybatis-flex-processor - 1.6.4 + 1.6.5 diff --git a/docs/zh/others/apt.md b/docs/zh/others/apt.md index 2e693a06..f592ae69 100644 --- a/docs/zh/others/apt.md +++ b/docs/zh/others/apt.md @@ -220,7 +220,7 @@ pom.xml 添加 `annotationProcessorPaths` 配置, ``` dependencies { ... - annotationProcessor 'com.mybatis-flex:mybatis-flex-processor:1.6.4' + annotationProcessor 'com.mybatis-flex:mybatis-flex-processor:1.6.5' } ``` diff --git a/docs/zh/others/codegen.md b/docs/zh/others/codegen.md index 6f842a1d..ecb6ef03 100644 --- a/docs/zh/others/codegen.md +++ b/docs/zh/others/codegen.md @@ -10,7 +10,7 @@ com.mybatis-flex mybatis-flex-codegen - 1.6.4 + 1.6.5 ``` diff --git a/pom.xml b/pom.xml index 139ce64f..882b8bcc 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ com.mybatis-flex parent pom - 1.6.4 + 1.6.5 mybatis-flex https://mybatis-flex.com @@ -55,7 +55,7 @@ 8 8 - 1.6.4 + 1.6.5 3.5.13 2.1.0 From 39d8375e447c140d3e25301a71549e9f0e1f3aec Mon Sep 17 00:00:00 2001 From: Ice Date: Sat, 16 Sep 2023 18:21:24 +0800 Subject: [PATCH 21/47] =?UTF-8?q?feat=EF=BC=9ARelation=E6=B3=A8=E8=A7=A3?= =?UTF-8?q?=E6=96=B0=E5=A2=9EtargetFieldBind=E5=B1=9E=E6=80=A7=EF=BC=8C?= =?UTF-8?q?=E5=BD=93=E4=B8=8D=E4=B8=BA=E7=A9=BA=E4=B8=B2=E6=97=B6=E5=80=BC?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E6=9F=90=E4=B8=AA=E5=AD=97=E6=AE=B5=E8=B5=8B?= =?UTF-8?q?=E5=80=BC=20test=EF=BC=9A=E5=A2=9E=E5=8A=A0Relation=E6=B3=A8?= =?UTF-8?q?=E8=A7=A3=E5=8D=95=E5=AD=97=E6=AE=B5=E8=B5=8B=E5=80=BCSpringboo?= =?UTF-8?q?t=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../annotation/RelationManyToMany.java | 8 ++ .../annotation/RelationManyToOne.java | 8 ++ .../annotation/RelationOneToMany.java | 8 ++ .../annotation/RelationOneToOne.java | 8 ++ .../core/relation/AbstractRelation.java | 48 +++++++--- .../mybatisflex/core/relation/ManyToMany.java | 1 + .../mybatisflex/core/relation/ManyToOne.java | 1 + .../mybatisflex/core/relation/OneToMany.java | 1 + .../mybatisflex/core/relation/OneToOne.java | 1 + .../core/relation/RelationManager.java | 3 +- .../core/relation/ToManyRelation.java | 11 ++- .../core/relation/ToOneRelation.java | 12 ++- .../com/mybatisflex/test/model/UserVO5.java | 96 +++++++++++++++++++ .../test/mapper/UserMapperTest.java | 6 ++ 14 files changed, 194 insertions(+), 18 deletions(-) create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO5.java diff --git a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToMany.java b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToMany.java index 41990ae9..68e523e3 100644 --- a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToMany.java +++ b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToMany.java @@ -67,6 +67,14 @@ public @interface RelationManyToMany { */ String targetField() default ""; + /** + * 目标实体类的字段绑定 + *

+ * 当字段不为空串时,只进行某个字段赋值(使用对应字段类型接收) + * @return 属性名称 + */ + String targetFieldBind() default ""; + /** * 当映射是一个 map 时,使用哪个内容来当做 map 的 Key * @return 指定的列 diff --git a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToOne.java b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToOne.java index cfbb8d0d..287a177b 100644 --- a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToOne.java +++ b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToOne.java @@ -67,6 +67,14 @@ public @interface RelationManyToOne { */ String targetField() default ""; + /** + * 目标实体类的字段绑定 + *

+ * 当字段不为空串时,只进行某个字段赋值(使用对应字段类型接收) + * @return 属性名称 + */ + String targetFieldBind() default ""; + /** * 中间表名称,一对一的关系是通过通过中间表维护时,需要添加此项配置。 * diff --git a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToMany.java b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToMany.java index eeafeb08..91aafab0 100644 --- a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToMany.java +++ b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToMany.java @@ -67,6 +67,14 @@ public @interface RelationOneToMany { */ String targetField(); + /** + * 目标实体类的字段绑定 + *

+ * 当字段不为空串时,只进行某个字段赋值(使用对应字段类型接收) + * @return 属性名称 + */ + String targetFieldBind() default ""; + /** * 当映射是一个 map 时,使用哪个内容来当做 map 的 Key * @return 指定的列 diff --git a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToOne.java b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToOne.java index 528e910c..b12d6759 100644 --- a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToOne.java +++ b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToOne.java @@ -67,6 +67,14 @@ public @interface RelationOneToOne { */ String targetField(); + /** + * 目标实体类的字段绑定 + *

+ * 当字段不为空串时,只进行某个字段赋值(使用对应字段类型接收) + * @return 属性名称 + */ + String targetFieldBind() default ""; + /** * 中间表名称,一对一的关系是通过通过中间表维护时,需要添加此项配置。 * diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/AbstractRelation.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/AbstractRelation.java index 472b5e73..653614da 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/AbstractRelation.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/AbstractRelation.java @@ -46,6 +46,8 @@ abstract class AbstractRelation { protected String targetSchema; protected String targetTable; protected Field targetField; + protected String targetFieldBind; + protected boolean onlyTargetFieldBind; protected Class targetEntityClass; protected TableInfo targetTableInfo; protected FieldWrapper targetFieldWrapper; @@ -62,7 +64,7 @@ abstract class AbstractRelation { protected QueryColumn conditionColumn; protected String[] selectColumns; - public AbstractRelation(String selfField, String targetSchema, String targetTable, String targetField, + public AbstractRelation(String selfField, String targetSchema, String targetTable, String targetField, String targetFieldBind, String joinTable, String joinSelfColumn, String joinTargetColumn, String dataSource, Class entityClass, Field relationField, String extraCondition, String[] selectColumns @@ -82,25 +84,33 @@ abstract class AbstractRelation { this.selfField = ClassUtil.getFirstField(entityClass, field -> field.getName().equals(selfField)); this.selfFieldWrapper = FieldWrapper.of(entityClass, selfField); - - this.targetEntityClass = relationFieldWrapper.getMappingType(); + //以使用者注解配置为主 + this.targetTableInfo = StringUtil.isBlank(targetTable) ? TableInfoFactory.ofEntityClass(relationFieldWrapper.getMappingType()) : TableInfoFactory.ofTableName(targetTable); + this.targetEntityClass = targetTableInfo != null ? targetTableInfo.getEntityClass() : relationFieldWrapper.getMappingType(); this.targetSchema = targetSchema; - this.targetTable = targetTable; + this.targetTable = targetTableInfo != null ? targetTableInfo.getTableName() : targetTable; this.targetField = ClassUtil.getFirstField(targetEntityClass, field -> field.getName().equals(targetField)); this.targetFieldWrapper = FieldWrapper.of(targetEntityClass, targetField); - this.targetTableInfo = TableInfoFactory.ofEntityClass(targetEntityClass); + this.targetFieldBind = targetFieldBind; + this.onlyTargetFieldBind = StringUtil.isNotBlank(targetFieldBind); this.conditionColumn = column(targetTable, targetTableInfo.getColumnByProperty(this.targetField.getName())); - if (ArrayUtil.isNotEmpty(selectColumns)) { - if (ArrayUtil.contains(selectColumns, conditionColumn.getName())) { - this.selectColumns = selectColumns; - } else { - //需要追加 conditionColumn,因为进行内存 join 的时候,需要用到这个内容进行对比 - this.selectColumns = ArrayUtil.concat(selectColumns, new String[]{conditionColumn.getName()}); + if (onlyTargetFieldBind) { + //仅绑定字段时只需要查询关联列和该字段列即可 + this.selectColumns = new String[]{conditionColumn.getName(), targetTableInfo != null ? targetTableInfo.getColumnByProperty(this.targetFieldBind) : StringUtil.camelToUnderline(this.targetFieldBind)}; + } else { + if (ArrayUtil.isNotEmpty(selectColumns)) { + if (ArrayUtil.contains(selectColumns, conditionColumn.getName())) { + this.selectColumns = selectColumns; + } else { + //需要追加 conditionColumn,因为进行内存 join 的时候,需要用到这个内容进行对比 + this.selectColumns = ArrayUtil.concat(selectColumns, new String[]{conditionColumn.getName()}); + } } + } initExtraCondition(extraCondition); @@ -249,6 +259,22 @@ abstract class AbstractRelation { this.targetTable = targetTable; } + public String getTargetFieldBind() { + return targetFieldBind; + } + + public void setTargetFieldBind(String targetFieldBind) { + this.targetFieldBind = targetFieldBind; + } + + public boolean isOnlyTargetFieldBind() { + return onlyTargetFieldBind; + } + + public void setOnlyTargetFieldBind(boolean onlyTargetFieldBind) { + this.onlyTargetFieldBind = onlyTargetFieldBind; + } + public String getJoinTable() { return joinTable; } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ManyToMany.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ManyToMany.java index 7fcddb21..69fbd813 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ManyToMany.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ManyToMany.java @@ -26,6 +26,7 @@ class ManyToMany extends ToManyRelation { , annotation.targetSchema() , annotation.targetTable() , getDefaultPrimaryProperty(annotation.targetField(), getTargetEntityClass(entityClass, relationField), "@RelationManyToMany.targetField can not be empty in field: \"" + entityClass.getName() + "." + relationField.getName() + "\"") + , annotation.targetFieldBind() , annotation.joinTable() , annotation.joinSelfColumn() , annotation.joinTargetColumn() diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ManyToOne.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ManyToOne.java index 45d71ac8..a49071c5 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ManyToOne.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ManyToOne.java @@ -27,6 +27,7 @@ class ManyToOne extends ToOneRelation { , annotation.targetTable() , getDefaultPrimaryProperty(annotation.targetField(), getTargetEntityClass(entityClass, relationField) , "@RelationManyToOne.selfField can not be empty in field: \"" + entityClass.getName() + "." + relationField.getName() + "\"") + , annotation.targetFieldBind() , annotation.joinTable() , annotation.joinSelfColumn() , annotation.joinTargetColumn() diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/OneToMany.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/OneToMany.java index fc5a642c..a50cc48e 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/OneToMany.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/OneToMany.java @@ -27,6 +27,7 @@ class OneToMany extends ToManyRelation { , annotation.targetSchema() , annotation.targetTable() , annotation.targetField() + , annotation.targetFieldBind() , annotation.joinTable() , annotation.joinSelfColumn() , annotation.joinTargetColumn() diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/OneToOne.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/OneToOne.java index 9e410ec0..bf90c3a1 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/OneToOne.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/OneToOne.java @@ -27,6 +27,7 @@ class OneToOne extends ToOneRelation { , annotation.targetSchema() , annotation.targetTable() , annotation.targetField() + , annotation.targetFieldBind() , annotation.joinTable() , annotation.joinSelfColumn() , annotation.joinTargetColumn() diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/RelationManager.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/RelationManager.java index 8bb15149..26cb6ce1 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/RelationManager.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/RelationManager.java @@ -359,8 +359,9 @@ public class RelationManager { DataSourceKey.use(configDsKey); } + //仅绑定字段:As目标实体类 不进行字段绑定:As映射类型 QueryWrapper queryWrapper = relation.buildQueryWrapper(targetValues); - List targetObjectList = mapper.selectListByQueryAs(queryWrapper, relation.getMappingType()); + List targetObjectList = mapper.selectListByQueryAs(queryWrapper, relation.isOnlyTargetFieldBind() ? relation.getTargetEntityClass() : relation.getMappingType()); if (CollectionUtil.isNotEmpty(targetObjectList)) { //递归查询 diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ToManyRelation.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ToManyRelation.java index 694c3ca6..f8f2e8ae 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ToManyRelation.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ToManyRelation.java @@ -31,11 +31,11 @@ class ToManyRelation extends AbstractRelation { protected long limit = 0; - public ToManyRelation(String selfField, String targetSchema, String targetTable, String targetField, + public ToManyRelation(String selfField, String targetSchema, String targetTable, String targetField, String targetFieldBind, String joinTable, String joinSelfColumn, String joinTargetColumn, String dataSource, Class selfEntityClass, Field relationField, String extraCondition, String[] selectColumns) { - super(selfField, targetSchema, targetTable, targetField, + super(selfField, targetSchema, targetTable, targetField, targetFieldBind, joinTable, joinSelfColumn, joinTargetColumn, dataSource, selfEntityClass, relationField, extraCondition, selectColumns @@ -101,7 +101,12 @@ class ToManyRelation extends AbstractRelation { for (Object targetObject : targetObjectList) { Object targetValue = targetFieldWrapper.get(targetObject); if (targetValue != null && targetMappingValues.contains(targetValue.toString())) { - collection.add(targetObject); + if (onlyTargetFieldBind) { + //仅绑定某个字段 + collection.add(FieldWrapper.of(targetObject.getClass(), targetFieldBind).get(targetObject)); + } else { + collection.add(targetObject); + } } } relationFieldWrapper.set(collection, selfEntity); diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ToOneRelation.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ToOneRelation.java index 86690955..f04de754 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ToOneRelation.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ToOneRelation.java @@ -16,6 +16,7 @@ package com.mybatisflex.core.relation; import com.mybatisflex.core.row.Row; +import com.mybatisflex.core.util.FieldWrapper; import java.lang.reflect.Field; import java.util.List; @@ -23,10 +24,10 @@ import java.util.List; class ToOneRelation extends AbstractRelation { - public ToOneRelation(String selfField, String targetSchema, String targetTable, String targetField, + public ToOneRelation(String selfField, String targetSchema, String targetTable, String targetField, String targetFieldBind, String joinTable, String joinSelfColumn, String joinTargetColumn, String dataSource, Class selfEntityClass, Field relationField, String[] selectColumns) { - super(selfField, targetSchema, targetTable, targetField, + super(selfField, targetSchema, targetTable, targetField, targetFieldBind, joinTable, joinSelfColumn, joinTargetColumn, dataSource, selfEntityClass, relationField, null, selectColumns @@ -53,7 +54,12 @@ class ToOneRelation extends AbstractRelation { for (Object targetObject : targetObjectList) { Object targetValue = targetFieldWrapper.get(targetObject); if (targetValue != null && targetMappingValue.equals(targetValue.toString())) { - relationFieldWrapper.set(targetObject, selfEntity); + if (onlyTargetFieldBind) { + //仅绑定某个字段 + relationFieldWrapper.set(FieldWrapper.of(targetObject.getClass(), targetFieldBind).get(targetObject), selfEntity); + } else { + relationFieldWrapper.set(targetObject, selfEntity); + } break; } } diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO5.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO5.java new file mode 100644 index 00000000..7589fbdd --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO5.java @@ -0,0 +1,96 @@ +package com.mybatisflex.test.model; + +import com.mybatisflex.annotation.*; + +import java.io.Serializable; +import java.util.List; + +/** + * 字段绑定测试 + * @author Ice 2023/09/16 + * @version 1.0 + */ +@Table("tb_user") +public class UserVO5 implements Serializable { + private static final long serialVersionUID = 474700189859144273L; + + @Id + private Integer userId; + private String userName; + private String password; + + @RelationOneToOne( + selfField = "userId", + targetTable = "tb_id_card", + targetField = "id", + targetFieldBind = "idNumber" + ) + private String idNumberCustomFieldName; + + @RelationOneToMany( + selfField = "userId", + targetTable = "tb_user_order", + targetField = "userId", + targetFieldBind = "orderId" + ) + private List orderIdList; + + @RelationManyToMany( + selfField = "userId", + targetTable = "tb_role", + targetField = "roleId", + targetFieldBind = "roleName", + joinTable = "tb_user_role", + joinSelfColumn = "user_id", + joinTargetColumn = "role_id" + ) + private List roleNameList; + + public Integer getUserId() { + return userId; + } + + public void setUserId(Integer userId) { + this.userId = userId; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getIdNumberCustomFieldName() { + return idNumberCustomFieldName; + } + + public void setIdNumberCustomFieldName(String idNumberCustomFieldName) { + this.idNumberCustomFieldName = idNumberCustomFieldName; + } + + public List getOrderIdList() { + return orderIdList; + } + + public void setOrderIdList(List orderIdList) { + this.orderIdList = orderIdList; + } + + public List getRoleNameList() { + return roleNameList; + } + + public void setRoleNameList(List roleNameList) { + this.roleNameList = roleNameList; + } +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/UserMapperTest.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/UserMapperTest.java index 43b3e340..1ed5227e 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/UserMapperTest.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/UserMapperTest.java @@ -280,4 +280,10 @@ class UserMapperTest { System.err.println(user); } + @Test + public void testFieldBindRelations() { + List userVO5List = userMapper.selectListWithRelationsByQueryAs(QueryWrapper.create(), UserVO5.class); + System.out.println(userVO5List); + } + } From 922e449f5b92e84303edd62c78ce7989f08f065d Mon Sep 17 00:00:00 2001 From: Ice Date: Sun, 17 Sep 2023 23:06:40 +0800 Subject: [PATCH 22/47] =?UTF-8?q?optimize=EF=BC=9ARelation=E6=B3=A8?= =?UTF-8?q?=E8=A7=A3=E5=B1=9E=E6=80=A7targetFieldBind=E6=B3=A8=E9=87=8A=20?= =?UTF-8?q?add=EF=BC=9A=201=EF=BC=9ARelation=E6=B3=A8=E8=A7=A3=E5=B1=9E?= =?UTF-8?q?=E6=80=A7targetFieldBind=E4=BD=BF=E7=94=A8=E6=96=87=E6=A1=A3=20?= =?UTF-8?q?2=EF=BC=9AtargetFieldBind=E6=B5=8B=E8=AF=95Model=E7=9A=84toStri?= =?UTF-8?q?ng=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/zh/base/relations-query.md | 105 ++++++++++++++++++ .../annotation/RelationManyToMany.java | 2 +- .../annotation/RelationManyToOne.java | 2 +- .../annotation/RelationOneToMany.java | 2 +- .../annotation/RelationOneToOne.java | 2 +- .../com/mybatisflex/test/model/UserVO5.java | 12 ++ 6 files changed, 121 insertions(+), 4 deletions(-) diff --git a/docs/zh/base/relations-query.md b/docs/zh/base/relations-query.md index eb77a80c..1496989e 100644 --- a/docs/zh/base/relations-query.md +++ b/docs/zh/base/relations-query.md @@ -352,6 +352,111 @@ public class Account implements Serializable { } ``` +Relation结果集只使用某个字段值-`since v1.6.6` + +`RelationOneToOne`、`RelationOneToMany`、`RelationManyToOne`、`RelationManyToMany`新增属性`targetFieldBind` +```java {7-11} + /** + * 目标对象的关系实体类的属性绑定 + *

+ * 当字段不为空串时,只进行某个字段赋值(使用对应字段类型接收) + * @return 属性名称 + */ + String targetFieldBind() default ""; +``` +> 注解其他属性配置使用不变,当配置了`targetFieldBind`值时,只提取目标对象关系实体类的该属性 +> +> 注意:因为不是对象接收,所以该配置需要强制配置注解`targetTable`属性(因为是某个字段接收,并不是某个实体对应的表,所以需要增加`targetTable`获取目标表信息 +> +> 示例场景:有个业务有个操作日志,操作日志中有个createBy(操作人字段),此时在日志详情或者说日志列表中需要显示操作人名称,且只需要这一个字段,此时使用实体接收会导致不必要的字段出现,接口文档也会变得混乱。该场景也可以使用`Field Query`和`Join Query`实现 +> +假设一个账户 +- 每个账户有一个唯一对应的`id_number`列在表`tb_id_card`中 +- 每个账户拥有多个订单`tb_user_order` +- 一个账户可以有多个角色,一个角色也可以分配给多个账户,他们通过中间表`tb_user_role`进行关系映射 + +```java {7-11} +@Table("tb_user") +public class UserVO5 implements Serializable { + private static final long serialVersionUID = 474700189859144273L; + + @Id + private Integer userId; + private String userName; + private String password; + + @RelationOneToOne( + selfField = "userId", + targetTable = "tb_id_card", + targetField = "id", + targetFieldBind = "idNumber" + ) + //该处可以定义其他属性名,不一定要是目标对象的字段名 + private String idNumberCustomFieldName; + + @RelationOneToMany( + selfField = "userId", + targetTable = "tb_user_order", + targetField = "userId", + targetFieldBind = "orderId" + ) + private List orderIdList; + + @RelationManyToMany( + selfField = "userId", + targetTable = "tb_role", + targetField = "roleId", + targetFieldBind = "roleName", + joinTable = "tb_user_role", + joinSelfColumn = "user_id", + joinTargetColumn = "role_id" + ) + private List roleNameList; + + //getter setter toString +} +``` +进行查询 +```java +List userVO5List = userMapper.selectListWithRelationsByQueryAs(QueryWrapper.create(), UserVO5.class); +System.out.println(userVO5List); +``` +输出结果 +```json +[{ + userId = 1, + userName = '张三', + password = '12345678', + idNumberCustomFieldName = 'F281C807-C40B-472D-82F5-6130199C6328', + orderIdList = [1], + roleNameList = [普通用户] + }, + { + userId = 2, + userName = '李四', + password = '87654321', + idNumberCustomFieldName = '6176E9AD-36EF-4201-A5F7-CCE89B254952', + orderIdList = [3, 2], + roleNameList = [普通用户, 贵族用户] + }, + { + userId = 3, + userName = '王五', + password = '09897654', + idNumberCustomFieldName = 'A038E6EA-1FDE-4191-AA41-06F78E91F6C2', + orderIdList = [6, 5, 4], + roleNameList = [普通用户, 贵族用户, 超级贵族用户] + }, + { + userId = 4, + userName = '苏六', + password = '45678345', + idNumberCustomFieldName = 'A33E8BAA-93F2-4E28-A161-15CF7D0AE6D0', + orderIdList = [], + roleNameList = [普通用户, 贵族用户, 超级贵族用户, 管理员用户] +}] +``` + ## 父子关系查询 比如在一些系统中,比如菜单会有一些父子关系,例如菜单表如下: diff --git a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToMany.java b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToMany.java index 68e523e3..60a7ee85 100644 --- a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToMany.java +++ b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToMany.java @@ -68,7 +68,7 @@ public @interface RelationManyToMany { String targetField() default ""; /** - * 目标实体类的字段绑定 + * 目标对象的关系实体类的属性绑定 *

* 当字段不为空串时,只进行某个字段赋值(使用对应字段类型接收) * @return 属性名称 diff --git a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToOne.java b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToOne.java index 287a177b..5f605e62 100644 --- a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToOne.java +++ b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToOne.java @@ -68,7 +68,7 @@ public @interface RelationManyToOne { String targetField() default ""; /** - * 目标实体类的字段绑定 + * 目标对象的关系实体类的属性绑定 *

* 当字段不为空串时,只进行某个字段赋值(使用对应字段类型接收) * @return 属性名称 diff --git a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToMany.java b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToMany.java index 91aafab0..29730ab9 100644 --- a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToMany.java +++ b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToMany.java @@ -68,7 +68,7 @@ public @interface RelationOneToMany { String targetField(); /** - * 目标实体类的字段绑定 + * 目标对象的关系实体类的属性绑定 *

* 当字段不为空串时,只进行某个字段赋值(使用对应字段类型接收) * @return 属性名称 diff --git a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToOne.java b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToOne.java index b12d6759..895e36d2 100644 --- a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToOne.java +++ b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToOne.java @@ -68,7 +68,7 @@ public @interface RelationOneToOne { String targetField(); /** - * 目标实体类的字段绑定 + * 目标对象的关系实体类的属性绑定 *

* 当字段不为空串时,只进行某个字段赋值(使用对应字段类型接收) * @return 属性名称 diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO5.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO5.java index 7589fbdd..3ca6fd9c 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO5.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO5.java @@ -93,4 +93,16 @@ public class UserVO5 implements Serializable { public void setRoleNameList(List roleNameList) { this.roleNameList = roleNameList; } + + @Override + public String toString() { + return "UserVO5{" + + "userId=" + userId + + ", userName='" + userName + '\'' + + ", password='" + password + '\'' + + ", idNumberCustomFieldName='" + idNumberCustomFieldName + '\'' + + ", orderIdList=" + orderIdList + + ", roleNameList=" + roleNameList + + '}'; + } } From be8353abb0d766a7cc8c6ae1426f4aa2ec4777b6 Mon Sep 17 00:00:00 2001 From: Ice Date: Sun, 17 Sep 2023 23:09:16 +0800 Subject: [PATCH 23/47] =?UTF-8?q?optimize=EF=BC=9ARelation=E6=B3=A8?= =?UTF-8?q?=E8=A7=A3=E5=B1=9E=E6=80=A7targetFieldBind=E6=B3=A8=E9=87=8A=20?= =?UTF-8?q?add=EF=BC=9A=201=EF=BC=9ARelation=E6=B3=A8=E8=A7=A3=E5=B1=9E?= =?UTF-8?q?=E6=80=A7targetFieldBind=E4=BD=BF=E7=94=A8=E6=96=87=E6=A1=A3=20?= =?UTF-8?q?2=EF=BC=9AtargetFieldBind=E6=B5=8B=E8=AF=95Model=E7=9A=84toStri?= =?UTF-8?q?ng=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/zh/base/relations-query.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/zh/base/relations-query.md b/docs/zh/base/relations-query.md index 1496989e..3faec96b 100644 --- a/docs/zh/base/relations-query.md +++ b/docs/zh/base/relations-query.md @@ -366,7 +366,7 @@ Relation结果集只使用某个字段值-`since v1.6.6` ``` > 注解其他属性配置使用不变,当配置了`targetFieldBind`值时,只提取目标对象关系实体类的该属性 > -> 注意:因为不是对象接收,所以该配置需要强制配置注解`targetTable`属性(因为是某个字段接收,并不是某个实体对应的表,所以需要增加`targetTable`获取目标表信息 +> 注意:因为不是对象接收,所以该配置需要强制配置注解`targetTable`属性(因为是某个字段接收,并不是某个实体对应的表,所以需要增加`targetTable`获取目标表信息) > > 示例场景:有个业务有个操作日志,操作日志中有个createBy(操作人字段),此时在日志详情或者说日志列表中需要显示操作人名称,且只需要这一个字段,此时使用实体接收会导致不必要的字段出现,接口文档也会变得混乱。该场景也可以使用`Field Query`和`Join Query`实现 > From 0f4f31a30e7cc3d945b0e71410cd280e33893fbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Mon, 18 Sep 2023 09:11:20 +0800 Subject: [PATCH 24/47] refactor: rename "targetFieldBind" to "valueField" --- docs/zh/base/relations-query.md | 12 +++---- .../annotation/RelationManyToMany.java | 2 +- .../annotation/RelationManyToOne.java | 2 +- .../annotation/RelationOneToMany.java | 2 +- .../annotation/RelationOneToOne.java | 2 +- .../core/relation/AbstractRelation.java | 32 ++++++++++--------- .../mybatisflex/core/relation/ManyToMany.java | 2 +- .../mybatisflex/core/relation/ManyToOne.java | 2 +- .../mybatisflex/core/relation/OneToMany.java | 2 +- .../mybatisflex/core/relation/OneToOne.java | 2 +- .../core/relation/RelationManager.java | 2 +- .../core/relation/ToManyRelation.java | 8 ++--- .../core/relation/ToOneRelation.java | 8 ++--- .../com/mybatisflex/test/model/UserVO5.java | 6 ++-- 14 files changed, 43 insertions(+), 41 deletions(-) diff --git a/docs/zh/base/relations-query.md b/docs/zh/base/relations-query.md index 3faec96b..ba7be855 100644 --- a/docs/zh/base/relations-query.md +++ b/docs/zh/base/relations-query.md @@ -354,7 +354,7 @@ public class Account implements Serializable { Relation结果集只使用某个字段值-`since v1.6.6` -`RelationOneToOne`、`RelationOneToMany`、`RelationManyToOne`、`RelationManyToMany`新增属性`targetFieldBind` +`RelationOneToOne`、`RelationOneToMany`、`RelationManyToOne`、`RelationManyToMany`新增属性`valueField` ```java {7-11} /** * 目标对象的关系实体类的属性绑定 @@ -362,9 +362,9 @@ Relation结果集只使用某个字段值-`since v1.6.6` * 当字段不为空串时,只进行某个字段赋值(使用对应字段类型接收) * @return 属性名称 */ - String targetFieldBind() default ""; + String valueField() default ""; ``` -> 注解其他属性配置使用不变,当配置了`targetFieldBind`值时,只提取目标对象关系实体类的该属性 +> 注解其他属性配置使用不变,当配置了`valueField`值时,只提取目标对象关系实体类的该属性 > > 注意:因为不是对象接收,所以该配置需要强制配置注解`targetTable`属性(因为是某个字段接收,并不是某个实体对应的表,所以需要增加`targetTable`获取目标表信息) > @@ -389,7 +389,7 @@ public class UserVO5 implements Serializable { selfField = "userId", targetTable = "tb_id_card", targetField = "id", - targetFieldBind = "idNumber" + valueField = "idNumber" ) //该处可以定义其他属性名,不一定要是目标对象的字段名 private String idNumberCustomFieldName; @@ -398,7 +398,7 @@ public class UserVO5 implements Serializable { selfField = "userId", targetTable = "tb_user_order", targetField = "userId", - targetFieldBind = "orderId" + valueField = "orderId" ) private List orderIdList; @@ -406,7 +406,7 @@ public class UserVO5 implements Serializable { selfField = "userId", targetTable = "tb_role", targetField = "roleId", - targetFieldBind = "roleName", + valueField = "roleName", joinTable = "tb_user_role", joinSelfColumn = "user_id", joinTargetColumn = "role_id" diff --git a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToMany.java b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToMany.java index 60a7ee85..64d5448f 100644 --- a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToMany.java +++ b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToMany.java @@ -73,7 +73,7 @@ public @interface RelationManyToMany { * 当字段不为空串时,只进行某个字段赋值(使用对应字段类型接收) * @return 属性名称 */ - String targetFieldBind() default ""; + String valueField() default ""; /** * 当映射是一个 map 时,使用哪个内容来当做 map 的 Key diff --git a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToOne.java b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToOne.java index 5f605e62..475936a1 100644 --- a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToOne.java +++ b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToOne.java @@ -73,7 +73,7 @@ public @interface RelationManyToOne { * 当字段不为空串时,只进行某个字段赋值(使用对应字段类型接收) * @return 属性名称 */ - String targetFieldBind() default ""; + String valueField() default ""; /** * 中间表名称,一对一的关系是通过通过中间表维护时,需要添加此项配置。 diff --git a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToMany.java b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToMany.java index 29730ab9..35314fae 100644 --- a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToMany.java +++ b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToMany.java @@ -73,7 +73,7 @@ public @interface RelationOneToMany { * 当字段不为空串时,只进行某个字段赋值(使用对应字段类型接收) * @return 属性名称 */ - String targetFieldBind() default ""; + String valueField() default ""; /** * 当映射是一个 map 时,使用哪个内容来当做 map 的 Key diff --git a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToOne.java b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToOne.java index 895e36d2..e9bca7d0 100644 --- a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToOne.java +++ b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToOne.java @@ -73,7 +73,7 @@ public @interface RelationOneToOne { * 当字段不为空串时,只进行某个字段赋值(使用对应字段类型接收) * @return 属性名称 */ - String targetFieldBind() default ""; + String valueField() default ""; /** * 中间表名称,一对一的关系是通过通过中间表维护时,需要添加此项配置。 diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/AbstractRelation.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/AbstractRelation.java index 653614da..7cac0dd1 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/AbstractRelation.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/AbstractRelation.java @@ -46,8 +46,10 @@ abstract class AbstractRelation { protected String targetSchema; protected String targetTable; protected Field targetField; - protected String targetFieldBind; - protected boolean onlyTargetFieldBind; + + protected String valueField; + protected boolean onlyQueryValueField; + protected Class targetEntityClass; protected TableInfo targetTableInfo; protected FieldWrapper targetFieldWrapper; @@ -64,7 +66,7 @@ abstract class AbstractRelation { protected QueryColumn conditionColumn; protected String[] selectColumns; - public AbstractRelation(String selfField, String targetSchema, String targetTable, String targetField, String targetFieldBind, + public AbstractRelation(String selfField, String targetSchema, String targetTable, String targetField, String valueField, String joinTable, String joinSelfColumn, String joinTargetColumn, String dataSource, Class entityClass, Field relationField, String extraCondition, String[] selectColumns @@ -93,14 +95,14 @@ abstract class AbstractRelation { this.targetField = ClassUtil.getFirstField(targetEntityClass, field -> field.getName().equals(targetField)); this.targetFieldWrapper = FieldWrapper.of(targetEntityClass, targetField); - this.targetFieldBind = targetFieldBind; - this.onlyTargetFieldBind = StringUtil.isNotBlank(targetFieldBind); + this.valueField = valueField; + this.onlyQueryValueField = StringUtil.isNotBlank(valueField); this.conditionColumn = column(targetTable, targetTableInfo.getColumnByProperty(this.targetField.getName())); - if (onlyTargetFieldBind) { + if (onlyQueryValueField) { //仅绑定字段时只需要查询关联列和该字段列即可 - this.selectColumns = new String[]{conditionColumn.getName(), targetTableInfo != null ? targetTableInfo.getColumnByProperty(this.targetFieldBind) : StringUtil.camelToUnderline(this.targetFieldBind)}; + this.selectColumns = new String[]{conditionColumn.getName(), targetTableInfo != null ? targetTableInfo.getColumnByProperty(this.valueField) : StringUtil.camelToUnderline(this.valueField)}; } else { if (ArrayUtil.isNotEmpty(selectColumns)) { if (ArrayUtil.contains(selectColumns, conditionColumn.getName())) { @@ -259,20 +261,20 @@ abstract class AbstractRelation { this.targetTable = targetTable; } - public String getTargetFieldBind() { - return targetFieldBind; + public String getValueField() { + return valueField; } - public void setTargetFieldBind(String targetFieldBind) { - this.targetFieldBind = targetFieldBind; + public void setValueField(String valueField) { + this.valueField = valueField; } - public boolean isOnlyTargetFieldBind() { - return onlyTargetFieldBind; + public boolean isOnlyQueryValueField() { + return onlyQueryValueField; } - public void setOnlyTargetFieldBind(boolean onlyTargetFieldBind) { - this.onlyTargetFieldBind = onlyTargetFieldBind; + public void setOnlyQueryValueField(boolean onlyQueryValueField) { + this.onlyQueryValueField = onlyQueryValueField; } public String getJoinTable() { diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ManyToMany.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ManyToMany.java index 69fbd813..75229d19 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ManyToMany.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ManyToMany.java @@ -26,7 +26,7 @@ class ManyToMany extends ToManyRelation { , annotation.targetSchema() , annotation.targetTable() , getDefaultPrimaryProperty(annotation.targetField(), getTargetEntityClass(entityClass, relationField), "@RelationManyToMany.targetField can not be empty in field: \"" + entityClass.getName() + "." + relationField.getName() + "\"") - , annotation.targetFieldBind() + , annotation.valueField() , annotation.joinTable() , annotation.joinSelfColumn() , annotation.joinTargetColumn() diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ManyToOne.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ManyToOne.java index a49071c5..3ffdf92b 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ManyToOne.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ManyToOne.java @@ -27,7 +27,7 @@ class ManyToOne extends ToOneRelation { , annotation.targetTable() , getDefaultPrimaryProperty(annotation.targetField(), getTargetEntityClass(entityClass, relationField) , "@RelationManyToOne.selfField can not be empty in field: \"" + entityClass.getName() + "." + relationField.getName() + "\"") - , annotation.targetFieldBind() + , annotation.valueField() , annotation.joinTable() , annotation.joinSelfColumn() , annotation.joinTargetColumn() diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/OneToMany.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/OneToMany.java index a50cc48e..08e8472a 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/OneToMany.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/OneToMany.java @@ -27,7 +27,7 @@ class OneToMany extends ToManyRelation { , annotation.targetSchema() , annotation.targetTable() , annotation.targetField() - , annotation.targetFieldBind() + , annotation.valueField() , annotation.joinTable() , annotation.joinSelfColumn() , annotation.joinTargetColumn() diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/OneToOne.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/OneToOne.java index bf90c3a1..9a2100fc 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/OneToOne.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/OneToOne.java @@ -27,7 +27,7 @@ class OneToOne extends ToOneRelation { , annotation.targetSchema() , annotation.targetTable() , annotation.targetField() - , annotation.targetFieldBind() + , annotation.valueField() , annotation.joinTable() , annotation.joinSelfColumn() , annotation.joinTargetColumn() diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/RelationManager.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/RelationManager.java index 26cb6ce1..3264b676 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/RelationManager.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/RelationManager.java @@ -361,7 +361,7 @@ public class RelationManager { //仅绑定字段:As目标实体类 不进行字段绑定:As映射类型 QueryWrapper queryWrapper = relation.buildQueryWrapper(targetValues); - List targetObjectList = mapper.selectListByQueryAs(queryWrapper, relation.isOnlyTargetFieldBind() ? relation.getTargetEntityClass() : relation.getMappingType()); + List targetObjectList = mapper.selectListByQueryAs(queryWrapper, relation.isOnlyQueryValueField() ? relation.getTargetEntityClass() : relation.getMappingType()); if (CollectionUtil.isNotEmpty(targetObjectList)) { //递归查询 diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ToManyRelation.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ToManyRelation.java index f8f2e8ae..0eb6756b 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ToManyRelation.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ToManyRelation.java @@ -31,11 +31,11 @@ class ToManyRelation extends AbstractRelation { protected long limit = 0; - public ToManyRelation(String selfField, String targetSchema, String targetTable, String targetField, String targetFieldBind, + public ToManyRelation(String selfField, String targetSchema, String targetTable, String targetField, String valueField, String joinTable, String joinSelfColumn, String joinTargetColumn, String dataSource, Class selfEntityClass, Field relationField, String extraCondition, String[] selectColumns) { - super(selfField, targetSchema, targetTable, targetField, targetFieldBind, + super(selfField, targetSchema, targetTable, targetField, valueField, joinTable, joinSelfColumn, joinTargetColumn, dataSource, selfEntityClass, relationField, extraCondition, selectColumns @@ -101,9 +101,9 @@ class ToManyRelation extends AbstractRelation { for (Object targetObject : targetObjectList) { Object targetValue = targetFieldWrapper.get(targetObject); if (targetValue != null && targetMappingValues.contains(targetValue.toString())) { - if (onlyTargetFieldBind) { + if (onlyQueryValueField) { //仅绑定某个字段 - collection.add(FieldWrapper.of(targetObject.getClass(), targetFieldBind).get(targetObject)); + collection.add(FieldWrapper.of(targetObject.getClass(), valueField).get(targetObject)); } else { collection.add(targetObject); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ToOneRelation.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ToOneRelation.java index f04de754..591d79d6 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ToOneRelation.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ToOneRelation.java @@ -24,10 +24,10 @@ import java.util.List; class ToOneRelation extends AbstractRelation { - public ToOneRelation(String selfField, String targetSchema, String targetTable, String targetField, String targetFieldBind, + public ToOneRelation(String selfField, String targetSchema, String targetTable, String targetField, String valueField, String joinTable, String joinSelfColumn, String joinTargetColumn, String dataSource, Class selfEntityClass, Field relationField, String[] selectColumns) { - super(selfField, targetSchema, targetTable, targetField, targetFieldBind, + super(selfField, targetSchema, targetTable, targetField, valueField, joinTable, joinSelfColumn, joinTargetColumn, dataSource, selfEntityClass, relationField, null, selectColumns @@ -54,9 +54,9 @@ class ToOneRelation extends AbstractRelation { for (Object targetObject : targetObjectList) { Object targetValue = targetFieldWrapper.get(targetObject); if (targetValue != null && targetMappingValue.equals(targetValue.toString())) { - if (onlyTargetFieldBind) { + if (onlyQueryValueField) { //仅绑定某个字段 - relationFieldWrapper.set(FieldWrapper.of(targetObject.getClass(), targetFieldBind).get(targetObject), selfEntity); + relationFieldWrapper.set(FieldWrapper.of(targetObject.getClass(), valueField).get(targetObject), selfEntity); } else { relationFieldWrapper.set(targetObject, selfEntity); } diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO5.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO5.java index 3ca6fd9c..bac33783 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO5.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO5.java @@ -23,7 +23,7 @@ public class UserVO5 implements Serializable { selfField = "userId", targetTable = "tb_id_card", targetField = "id", - targetFieldBind = "idNumber" + valueField = "idNumber" ) private String idNumberCustomFieldName; @@ -31,7 +31,7 @@ public class UserVO5 implements Serializable { selfField = "userId", targetTable = "tb_user_order", targetField = "userId", - targetFieldBind = "orderId" + valueField = "orderId" ) private List orderIdList; @@ -39,7 +39,7 @@ public class UserVO5 implements Serializable { selfField = "userId", targetTable = "tb_role", targetField = "roleId", - targetFieldBind = "roleName", + valueField = "roleName", joinTable = "tb_user_role", joinSelfColumn = "user_id", joinTargetColumn = "role_id" From 23f4af9e57bb3cee72f51e92eea65b31859b3bb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Mon, 18 Sep 2023 09:27:41 +0800 Subject: [PATCH 25/47] doc: update docs --- docs/zh/base/relations-query.md | 63 ++++++++++++--------------------- 1 file changed, 22 insertions(+), 41 deletions(-) diff --git a/docs/zh/base/relations-query.md b/docs/zh/base/relations-query.md index ba7be855..328b4b78 100644 --- a/docs/zh/base/relations-query.md +++ b/docs/zh/base/relations-query.md @@ -352,34 +352,31 @@ public class Account implements Serializable { } ``` -Relation结果集只使用某个字段值-`since v1.6.6` +## 只查询一个字段值 `RelationOneToOne`、`RelationOneToMany`、`RelationManyToOne`、`RelationManyToMany`新增属性`valueField` -```java {7-11} - /** - * 目标对象的关系实体类的属性绑定 - *

- * 当字段不为空串时,只进行某个字段赋值(使用对应字段类型接收) - * @return 属性名称 - */ - String valueField() default ""; +```java 7 +/** + * 目标对象的关系实体类的属性绑定 + *

+ * 当字段不为空串时,只进行某个字段赋值(使用对应字段类型接收) + * @return 属性名称 + */ +String valueField() default ""; ``` > 注解其他属性配置使用不变,当配置了`valueField`值时,只提取目标对象关系实体类的该属性 > -> 注意:因为不是对象接收,所以该配置需要强制配置注解`targetTable`属性(因为是某个字段接收,并不是某个实体对应的表,所以需要增加`targetTable`获取目标表信息) -> -> 示例场景:有个业务有个操作日志,操作日志中有个createBy(操作人字段),此时在日志详情或者说日志列表中需要显示操作人名称,且只需要这一个字段,此时使用实体接收会导致不必要的字段出现,接口文档也会变得混乱。该场景也可以使用`Field Query`和`Join Query`实现 -> -假设一个账户 +> **使用场景**:例如,操作日志中有个 `createBy` (操作人)字段,此时在日志信息中需要显示操作人名称,且只需要这一个字段,此时使用实体接收会导致不必要的字段出现,接口文档也会变得混乱。 + + + +假设一个账户实体类 `UserVO5.java` - 每个账户有一个唯一对应的`id_number`列在表`tb_id_card`中 -- 每个账户拥有多个订单`tb_user_order` - 一个账户可以有多个角色,一个角色也可以分配给多个账户,他们通过中间表`tb_user_role`进行关系映射 -```java {7-11} +```java {12,21,29} @Table("tb_user") -public class UserVO5 implements Serializable { - private static final long serialVersionUID = 474700189859144273L; - +public class UserVO5 { @Id private Integer userId; private String userName; @@ -394,13 +391,6 @@ public class UserVO5 implements Serializable { //该处可以定义其他属性名,不一定要是目标对象的字段名 private String idNumberCustomFieldName; - @RelationOneToMany( - selfField = "userId", - targetTable = "tb_user_order", - targetField = "userId", - valueField = "orderId" - ) - private List orderIdList; @RelationManyToMany( selfField = "userId", @@ -419,16 +409,16 @@ public class UserVO5 implements Serializable { 进行查询 ```java List userVO5List = userMapper.selectListWithRelationsByQueryAs(QueryWrapper.create(), UserVO5.class); -System.out.println(userVO5List); +System.out.println(JSON.toJSONString(userVO5List)); ``` 输出结果 -```json -[{ +```json {6,7,13,14,20,21} +[ + { userId = 1, userName = '张三', password = '12345678', idNumberCustomFieldName = 'F281C807-C40B-472D-82F5-6130199C6328', - orderIdList = [1], roleNameList = [普通用户] }, { @@ -436,7 +426,6 @@ System.out.println(userVO5List); userName = '李四', password = '87654321', idNumberCustomFieldName = '6176E9AD-36EF-4201-A5F7-CCE89B254952', - orderIdList = [3, 2], roleNameList = [普通用户, 贵族用户] }, { @@ -444,17 +433,9 @@ System.out.println(userVO5List); userName = '王五', password = '09897654', idNumberCustomFieldName = 'A038E6EA-1FDE-4191-AA41-06F78E91F6C2', - orderIdList = [6, 5, 4], roleNameList = [普通用户, 贵族用户, 超级贵族用户] - }, - { - userId = 4, - userName = '苏六', - password = '45678345', - idNumberCustomFieldName = 'A33E8BAA-93F2-4E28-A161-15CF7D0AE6D0', - orderIdList = [], - roleNameList = [普通用户, 贵族用户, 超级贵族用户, 管理员用户] -}] + } +] ``` ## 父子关系查询 From ca152cfdd8a733fef301df4388f3516b99b5c02f Mon Sep 17 00:00:00 2001 From: tangxin-li <617054137@qq.com> Date: Mon, 18 Sep 2023 18:32:56 +0800 Subject: [PATCH 26/47] =?UTF-8?q?=E8=BD=AC=E9=A9=BC=E5=B3=B0=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=A4=9A=E6=AC=A1=E8=BD=AC=E6=8D=A2=E4=BF=9D=E6=8C=81?= =?UTF-8?q?=E7=BB=93=E6=9E=9C=E4=B8=80=E8=87=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mybatisflex/core/util/StringUtil.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/StringUtil.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/StringUtil.java index 2c3566cf..edbd7ab3 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/StringUtil.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/StringUtil.java @@ -88,14 +88,16 @@ public class StringUtil { if (isBlank(string)) { return ""; } - String temp = string.toLowerCase(); - int strLen = temp.length(); + if(Character.isUpperCase(string.charAt(0))){ + string = string.toLowerCase(); + } + int strLen = string.length(); StringBuilder sb = new StringBuilder(strLen); for (int i = 0; i < strLen; i++) { - char c = temp.charAt(i); + char c = string.charAt(i); if (c == '_') { if (++i < strLen) { - sb.append(Character.toUpperCase(temp.charAt(i))); + sb.append(Character.toUpperCase(string.charAt(i))); } } else { sb.append(c); From d33e4cf8d5d04a937b20af3d7ef033e78fa9cb0d Mon Sep 17 00:00:00 2001 From: tangxin-li <617054137@qq.com> Date: Mon, 18 Sep 2023 18:41:34 +0800 Subject: [PATCH 27/47] =?UTF-8?q?fix:Db=E6=88=96MyBatis=E5=8E=9F=E7=94=9F?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E9=A9=BC=E5=B3=B0=E8=BD=AC=E6=8D=A2=E9=9C=80?= =?UTF-8?q?=E5=A4=84=E7=90=86=E4=B8=8D=E5=8C=85=E5=90=AB=E4=B8=8B=E5=88=92?= =?UTF-8?q?=E7=BA=BF=E7=9A=84=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mybatisflex/core/mybatis/FlexWrapperFactory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexWrapperFactory.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexWrapperFactory.java index 94562d25..d5b34eff 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexWrapperFactory.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexWrapperFactory.java @@ -80,7 +80,7 @@ public class FlexWrapperFactory implements ObjectWrapperFactory { @Override public String findProperty(String name, boolean useCamelCaseMapping) { - return useCamelCaseMapping && name.contains("_") ? StringUtil.underlineToCamel(name) : name; + return useCamelCaseMapping && ( Character.isUpperCase(name.charAt(0)) || name.contains("_")) ? StringUtil.underlineToCamel(name) : name; } } From 0a114f552369a37d6bfb1781dd5618ab646169fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Tue, 19 Sep 2023 08:59:48 +0800 Subject: [PATCH 28/47] doc: update docs --- docs/zh/intro/parts/contributors.md | 37 +++++++++++++++-------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/docs/zh/intro/parts/contributors.md b/docs/zh/intro/parts/contributors.md index c9d7812f..fb490c4e 100644 --- a/docs/zh/intro/parts/contributors.md +++ b/docs/zh/intro/parts/contributors.md @@ -1,21 +1,22 @@ | | | | | | |-----|-----|-----|-----|-----| -|![](https://foruda.gitee.com/avatar/1676898191051702081/61279_fuhai_1578915942.png!avatar60)Michael Yang|![](https://foruda.gitee.com/avatar/1677105717059215385/7984572_suomm_1624454114.png!avatar60)王帅|丌冰|![](https://foruda.gitee.com/avatar/1691044597656855579/7563907_lifejwang11_1691044597.png!avatar60)life|snyk-bot| -|![](https://foruda.gitee.com/avatar/1691636027051962328/11233353_kamo-sama_1691636027.png!avatar60)卡莫sama|lhzsdnu|![](https://foruda.gitee.com/avatar/1683858335519306352/15535_noear_admin_1683858335.png!avatar60)西东|pengpeng|庄佳彬| -|Font_C|笨小孩|CloudPlayer|snow|Jerry| -|草语|Jerry_Zheng|wujl|![](https://foruda.gitee.com/avatar/1676895504036051867/8807_piggsoft_1578914592.jpg!avatar60)piggsoft|![](https://foruda.gitee.com/avatar/1674286432514482953/4807650_fandai_fandaidzsw_1674286432.png!avatar60)赤兮丷| -|![](https://foruda.gitee.com/avatar/1677053740056224121/5462387_i_tell_you_1618064317.png!avatar60)liibang|cainiao3853|黄沐鸿|barql|loong0306| -|yangs|菜鸟3853|![](https://foruda.gitee.com/avatar/1677086127012961929/7598208_robot-l_1590219712.png!avatar60)Robot.L|![](https://foruda.gitee.com/avatar/1677005791814507674/2130728_lemonbx_1622621180.png!avatar60)落羽er|沈君锋| -|![](https://foruda.gitee.com/avatar/1691034002435340221/1920167_qimincow_1691034002.png!avatar60)英雄路|tan90|BQ60ziOxlFI0R0|淡定|qixy| -|font-C|大周|yuanbaolong|zhijieqing|2han9wen71an| -|欢乐码农|![](https://foruda.gitee.com/avatar/1677237805724097193/11485875_bygkn_1660893367.png!avatar60)bygkn|Shark|hans|zhongyong| -|![](https://foruda.gitee.com/avatar/1676978624694631546/1600987_youthdream_1592959590.png!avatar60)锟斤拷|庄佳彬|![](https://foruda.gitee.com/avatar/1674121508509280199/9288653_saoforestt_1674121508.png!avatar60)Saoforest|XiaoLin|dgmico| -|![](https://foruda.gitee.com/avatar/1677111694079591934/8088436_yang-zzu_1604969134.png!avatar60)yang_zzu|![](https://foruda.gitee.com/avatar/1677162544015233775/9094323_lymph_java_1624796992.png!avatar60)Ikko Eltociear Ashimine|![](https://foruda.gitee.com/avatar/1676895416224286260/8331_chaosforever_1578914555.png!avatar60)锁力|chenjh3|![](https://foruda.gitee.com/avatar/1679885039814030308/5151444_yangbuyi_1679885039.png!avatar60)阿志同学| -|![](https://foruda.gitee.com/avatar/1684129987239221781/1731138_toycat93_1684129987.png!avatar60)玩具猫|chenjian835|![](https://foruda.gitee.com/avatar/1676896562075035262/20021_duxlei_1578915302.png!avatar60)duxlei|meng.liu3|yaochen4| -|![](https://foruda.gitee.com/avatar/1676959401839738321/1269497_zhy_balck_1578947791.png!avatar60)zhy_black|![](https://foruda.gitee.com/avatar/1676905453682965545/327218_gm173119755_1648555045.png!avatar60)豌豆粉|![](https://foruda.gitee.com/avatar/1676974596171836113/1532463_1395961821_1578953848.png!avatar60)ζั͡ ั͡ ั͡ ั͡Wm|18007559437|![](https://foruda.gitee.com/avatar/1676894749123859490/2132_hopper_1578914095.jpg!avatar60)陈国正| -|凌尘|luy|gongzhongqiang|乌鸦笑猪黑|EafonYoung| -|Alex|wnp|![](https://foruda.gitee.com/avatar/1676901646505077446/106613_myron_1578917779.png!avatar60)MyronLi|![](https://foruda.gitee.com/avatar/1677170868635098448/9319924_pioneer-sun_1624354686.png!avatar60)Pioneer-Sun|norkts| -|![](https://foruda.gitee.com/avatar/1677166292370951564/9173563_q-alex_1627784508.png!avatar60)Q_Alex|![](https://foruda.gitee.com/avatar/1677052070334379576/5421002_wlf213_1612139033.png!avatar60)wlf|aqnghu|winnerself|![](https://foruda.gitee.com/avatar/1676898238064465096/61541_whitedolphin_1578915956.png!avatar60)CrazyAirhead| -|![](https://foruda.gitee.com/avatar/1677182504887358627/9655223_animal553_1631088642.png!avatar60)她出去赚钱了|XiaoLin|张春根|![](https://foruda.gitee.com/avatar/1693449200752633970/11209107_jl_0417_1693449200.png!avatar60)疾浪|![](https://foruda.gitee.com/avatar/1676983827162237415/1697554_xinjump_1654653784.png!avatar60)xinjump| -|sppan|![](https://foruda.gitee.com/avatar/1662084101462823713/2079235_djxchi_1662084101.png!avatar60)时间淡忘一切| +|![](https://foruda.gitee.com/avatar/1676898191051702081/61279_fuhai_1578915942.png!avatar60)Michael Yang|![](https://foruda.gitee.com/avatar/1677105717059215385/7984572_suomm_1624454114.png!avatar60)王帅|丌冰|![](https://foruda.gitee.com/avatar/1691044597656855579/7563907_lifejwang11_1691044597.png!avatar60)life|![](https://foruda.gitee.com/avatar/1691636027051962328/11233353_kamo-sama_1691636027.png!avatar60)卡莫sama| +|snyk-bot|lhzsdnu|![](https://foruda.gitee.com/avatar/1683858335519306352/15535_noear_admin_1683858335.png!avatar60)西东|pengpeng|庄佳彬| +|Font_C|笨小孩|CloudPlayer|Jerry|snow| +|草语|Ice-samll|菜鸟3853|Jerry_Zheng|wujl| +|![](https://foruda.gitee.com/avatar/1676895504036051867/8807_piggsoft_1578914592.jpg!avatar60)piggsoft|![](https://foruda.gitee.com/avatar/1674286432514482953/4807650_fandai_fandaidzsw_1674286432.png!avatar60)赤兮丷|![](https://foruda.gitee.com/avatar/1677053740056224121/5462387_i_tell_you_1618064317.png!avatar60)liibang|黄沐鸿|cainiao3853| +|loong0306|yangs|barql|![](https://foruda.gitee.com/avatar/1677086127012961929/7598208_robot-l_1590219712.png!avatar60)Robot.L|tangxin| +|![](https://foruda.gitee.com/avatar/1677005791814507674/2130728_lemonbx_1622621180.png!avatar60)落羽er|沈君锋|![](https://foruda.gitee.com/avatar/1691034002435340221/1920167_qimincow_1691034002.png!avatar60)英雄路|aqnghu|tan90| +|BQ60ziOxlFI0R0|淡定|qixy|font-C|大周| +|yuanbaolong|zhijieqing|2han9wen71an|![](https://foruda.gitee.com/avatar/1677237805724097193/11485875_bygkn_1660893367.png!avatar60)bygkn|欢乐码农| +|hans|zhongyong|![](https://foruda.gitee.com/avatar/1676978624694631546/1600987_youthdream_1592959590.png!avatar60)锟斤拷|庄佳彬|![](https://foruda.gitee.com/avatar/1674121508509280199/9288653_saoforestt_1674121508.png!avatar60)Saoforest| +|![](https://foruda.gitee.com/avatar/1677111694079591934/8088436_yang-zzu_1604969134.png!avatar60)yang_zzu|dgmico|XiaoLin|Shark|![](https://foruda.gitee.com/avatar/1677162544015233775/9094323_lymph_java_1624796992.png!avatar60)Ikko Eltociear Ashimine| +|![](https://foruda.gitee.com/avatar/1678377314939642686/1604115_handy-git_1678377314.png!avatar60)handy|![](https://foruda.gitee.com/avatar/1676895416224286260/8331_chaosforever_1578914555.png!avatar60)锁力|chenjh3|![](https://foruda.gitee.com/avatar/1691737477130376308/1673084_wang_yong_ji_1691737477.png!avatar60)老吉丶|![](https://foruda.gitee.com/avatar/1684129987239221781/1731138_toycat93_1684129987.png!avatar60)玩具猫| +|chenjian835|![](https://foruda.gitee.com/avatar/1679885039814030308/5151444_yangbuyi_1679885039.png!avatar60)阿志同学|![](https://foruda.gitee.com/avatar/1676896562075035262/20021_duxlei_1578915302.png!avatar60)duxlei|meng.liu3|yaochen4| +|![](https://foruda.gitee.com/avatar/1676959401839738321/1269497_zhy_balck_1578947791.png!avatar60)zhy_black|![](https://foruda.gitee.com/avatar/1676905453682965545/327218_gm173119755_1648555045.png!avatar60)豌豆粉|![](https://foruda.gitee.com/avatar/1676974596171836113/1532463_1395961821_1578953848.png!avatar60)ζั͡ ั͡ ั͡ ั͡Wm|matthew|![](https://foruda.gitee.com/avatar/1676894749123859490/2132_hopper_1578914095.jpg!avatar60)陈国正| +|![](https://foruda.gitee.com/avatar/1691805683099463215/8904907_zhuhjay_1691805683.png!avatar60)ZhuHJay|凌尘|luy|gongzhongqiang|![](https://foruda.gitee.com/avatar/1677071665480088881/6561865_zoufang162_1585144118.png!avatar60)zoufang162| +|Alex|EafonYoung|乌鸦笑猪黑|wnp|![](https://foruda.gitee.com/avatar/1676901646505077446/106613_myron_1578917779.png!avatar60)MyronLi| +|![](https://foruda.gitee.com/avatar/1677170868635098448/9319924_pioneer-sun_1624354686.png!avatar60)Pioneer-Sun|norkts|![](https://foruda.gitee.com/avatar/1677166292370951564/9173563_q-alex_1627784508.png!avatar60)Q_Alex|拓宇在思考|![](https://foruda.gitee.com/avatar/1677052070334379576/5421002_wlf213_1612139033.png!avatar60)wlf| +|winnerself|![](https://foruda.gitee.com/avatar/1676898238064465096/61541_whitedolphin_1578915956.png!avatar60)CrazyAirhead|![](https://foruda.gitee.com/avatar/1677182504887358627/9655223_animal553_1631088642.png!avatar60)她出去赚钱了|张春根|XiaoLin| +|![](https://foruda.gitee.com/avatar/1693449200752633970/11209107_jl_0417_1693449200.png!avatar60)疾浪|![](https://foruda.gitee.com/avatar/1676983827162237415/1697554_xinjump_1654653784.png!avatar60)xinjump|sppan|![](https://foruda.gitee.com/avatar/1662084101462823713/2079235_djxchi_1662084101.png!avatar60)时间淡忘一切| From 3e2060b62ade72a877e999c10ebae6f60013e84f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Tue, 19 Sep 2023 09:07:54 +0800 Subject: [PATCH 29/47] chore: update issues template --- .gitee/ISSUE_TEMPLATE/bug.yml | 2 +- .gitee/ISSUE_TEMPLATE/question.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitee/ISSUE_TEMPLATE/bug.yml b/.gitee/ISSUE_TEMPLATE/bug.yml index 2f2a24d3..0cf461aa 100644 --- a/.gitee/ISSUE_TEMPLATE/bug.yml +++ b/.gitee/ISSUE_TEMPLATE/bug.yml @@ -7,7 +7,7 @@ body: attributes: label: 这个 Bug 是否已经存在: options: - - label: 我已经搜索过现有的问题 (https://gitee.com/mybatis-flex/mybatis-flex/issues) + - label: 我确定已经把 MyBatis-Flex 升级到最新版本 v1.6.5,并已搜索过现有的问题 (https://gitee.com/mybatis-flex/mybatis-flex/issues) required: true - type: textarea attributes: diff --git a/.gitee/ISSUE_TEMPLATE/question.yml b/.gitee/ISSUE_TEMPLATE/question.yml index 302ffab1..e2395e68 100644 --- a/.gitee/ISSUE_TEMPLATE/question.yml +++ b/.gitee/ISSUE_TEMPLATE/question.yml @@ -13,7 +13,7 @@ body: attributes: label: 这个问题是否已经存在: options: - - label: 我已经搜索过现有的问题 (https://gitee.com/mybatis-flex/mybatis-flex/issues) + - label: 我确定已经把 MyBatis-Flex 升级到最新版本 v1.6.5,并已搜索过现有的问题 (https://gitee.com/mybatis-flex/mybatis-flex/issues) required: true - type: textarea id: question-description From 9ddc6a95d91ade8a1e766e27b1c254490a420e3e Mon Sep 17 00:00:00 2001 From: Font_C <1020331126@qq.com> Date: Tue, 19 Sep 2023 10:42:53 +0800 Subject: [PATCH 30/47] =?UTF-8?q?=20Fixed=EF=BC=9AGenerate=20column=20alia?= =?UTF-8?q?s=20specifications=20to=20maintain=20the=20user's=20original=20?= =?UTF-8?q?column=20alias=20names=E3=80=82=20=20=E7=94=9F=E6=88=90?= =?UTF-8?q?=E5=88=97=E5=88=AB=E5=90=8D=E8=A7=84=E8=8C=83=EF=BC=8C=E4=BF=9D?= =?UTF-8?q?=E6=8C=81=E7=94=A8=E6=88=B7=E5=8E=9F=E5=A7=8B=E7=9A=84=E5=88=97?= =?UTF-8?q?=E5=88=AB=E5=90=8D=E5=91=BD=E5=90=8D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatisflex/core/dialect/IDialect.java | 2 + .../core/dialect/impl/CommonsDialectImpl.java | 4 +- .../core/query/DistinctQueryColumn.java | 2 +- .../mybatisflex/core/query/QueryColumn.java | 2 +- .../core/query/RawQueryColumn.java | 2 +- .../mybatisflex/core/query/WrapperUtil.java | 6 ++- .../coretest/AccountSqlTester.java | 42 +++++++++++++++---- 7 files changed, 48 insertions(+), 12 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/IDialect.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/IDialect.java index 8b38d31d..8c60b851 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/IDialect.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/IDialect.java @@ -29,6 +29,8 @@ public interface IDialect { String wrap(String keyword); + String wrapColumnAlias(String keyword); + default String getRealTable(String table) { return TableManager.getRealTable(table); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java index 29a422e5..c74ae30f 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java @@ -62,6 +62,9 @@ public class CommonsDialectImpl implements IDialect { return ASTERISK.equals(keyword) ? keyword : keywordWrap.wrap(keyword); } + public String wrapColumnAlias(String keyword) { + return ASTERISK.equals(keyword) ? keyword : keywordWrap.getPrefix() + keyword + keywordWrap.getSuffix(); + } @Override public String forHint(String hintString) { @@ -865,7 +868,6 @@ public class CommonsDialectImpl implements IDialect { } - @Override public String forSelectOneEntityById(TableInfo tableInfo) { StringBuilder sql = new StringBuilder(); diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/DistinctQueryColumn.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/DistinctQueryColumn.java index d6e03ebb..8b917e2a 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/DistinctQueryColumn.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/DistinctQueryColumn.java @@ -39,7 +39,7 @@ public class DistinctQueryColumn extends QueryColumn { String sql = SqlConsts.DISTINCT + StringUtil.join(SqlConsts.DELIMITER, queryColumns, queryColumn -> queryColumn.toSelectSql(queryTables, dialect)); - return sql + WrapperUtil.buildAlias(alias, dialect); + return sql + WrapperUtil.buildColumnAlias(alias, dialect); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryColumn.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryColumn.java index 1477f20b..5dc7d584 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryColumn.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryColumn.java @@ -946,7 +946,7 @@ public class QueryColumn implements CloneSupport, Conditional queryTables, IDialect dialect) { - return toConditionSql(queryTables, dialect) + WrapperUtil.buildAlias(alias, dialect); + return toConditionSql(queryTables, dialect) + WrapperUtil.buildColumnAlias(alias, dialect); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/RawQueryColumn.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/RawQueryColumn.java index 0558b46a..3a48cba6 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/RawQueryColumn.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/RawQueryColumn.java @@ -42,7 +42,7 @@ public class RawQueryColumn extends QueryColumn implements HasParamsColumn { @Override String toSelectSql(List queryTables, IDialect dialect) { - return content + WrapperUtil.buildAlias(alias, dialect); + return content + WrapperUtil.buildColumnAlias(alias, dialect); } @Override diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/WrapperUtil.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/WrapperUtil.java index 4e1eb0ad..dc51b06f 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/WrapperUtil.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/WrapperUtil.java @@ -148,13 +148,17 @@ class WrapperUtil { } static String withAlias(String sql, String alias, IDialect dialect) { - return SqlConsts.BRACKET_LEFT + sql + SqlConsts.BRACKET_RIGHT + getAsKeyWord(dialect) + dialect.wrap(alias); + return SqlConsts.BRACKET_LEFT + sql + SqlConsts.BRACKET_RIGHT + getAsKeyWord(dialect) + buildColumnAlias(alias, dialect); } static String buildAlias(String alias, IDialect dialect) { return StringUtil.isBlank(alias) ? SqlConsts.EMPTY : getAsKeyWord(dialect) + dialect.wrap(alias); } + static String buildColumnAlias(String alias, IDialect dialect) { + return StringUtil.isBlank(alias) ? SqlConsts.EMPTY : getAsKeyWord(dialect) + dialect.wrapColumnAlias(alias); + } + private static String getAsKeyWord(IDialect dialect) { return dialect instanceof OracleDialect ? SqlConsts.BLANK : SqlConsts.AS; } diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java index 7b2b9cf9..0144e9d1 100644 --- a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java @@ -21,13 +21,13 @@ import com.mybatisflex.core.dialect.IDialect; import com.mybatisflex.core.dialect.KeywordWrap; import com.mybatisflex.core.dialect.LimitOffsetProcessor; import com.mybatisflex.core.dialect.impl.CommonsDialectImpl; -import com.mybatisflex.core.query.CPI; -import com.mybatisflex.core.query.QueryWrapper; +import com.mybatisflex.core.dialect.impl.DmDialect; +import com.mybatisflex.core.dialect.impl.OracleDialect; +import com.mybatisflex.core.query.*; import com.mybatisflex.core.table.DynamicTableProcessor; import com.mybatisflex.core.table.TableInfo; import com.mybatisflex.core.table.TableInfoFactory; import com.mybatisflex.core.table.TableManager; -import com.mybatisflex.core.query.SqlOperators; import org.junit.Test; import java.util.Arrays; @@ -107,7 +107,8 @@ public class AccountSqlTester { public void testSelect1ColumnsSql() { QueryWrapper query = new QueryWrapper() .select(ACCOUNT.ID, ACCOUNT.USER_NAME, - ARTICLE.ID.as("articleId"), ARTICLE.TITLE) + ARTICLE.ID.as("articleId"), ARTICLE.TITLE, + max(ACCOUNT.AGE).as("ageMax")) .from(ACCOUNT.as("a"), ARTICLE.as("b")) .where(ACCOUNT.ID.eq(ARTICLE.ACCOUNT_ID)); @@ -119,14 +120,41 @@ public class AccountSqlTester { @Test public void testSelectColumnsAndFunctionsSql() { QueryWrapper query = new QueryWrapper() - .select(ACCOUNT.ID, ACCOUNT.USER_NAME, max(ACCOUNT.BIRTHDAY), avg(ACCOUNT.SEX).as("sex_avg")) - .from(ACCOUNT); + .select(ACCOUNT.ID, ACCOUNT.USER_NAME, ACCOUNT.AGE.as("aGe"), max(ACCOUNT.BIRTHDAY).as("Max_BirthDay"), avg(ACCOUNT.SEX).as("sex_avg")) + .from(ACCOUNT.as("tableAlias")); - IDialect dialect = new CommonsDialectImpl(); + IDialect dialect = new OracleDialect(); String sql = dialect.forSelectByQuery(query); System.out.println(sql); } + @Test + public void testSelectColumnsAndFunctionsSqlAlias() { + QueryWrapper query = new QueryWrapper() + .select(ACCOUNT.ID, ACCOUNT.USER_NAME, ACCOUNT.AGE.as("aGe"), max(ACCOUNT.BIRTHDAY).as("Max_BirthDay"), avg(ACCOUNT.SEX).as("sex_avg")) + .from(ACCOUNT.as("tableAlias")); + + IDialect dialect = new DmDialect(); + String sql = dialect.forSelectByQuery(query); + System.out.println(sql); + } + + @Test + public void testDistinctColumnAlias() { + QueryWrapper queryWrapper = new QueryWrapper() + .select( + new DistinctQueryColumn(ACCOUNT.SEX).as("sexDis")) + .select( ACCOUNT.USER_NAME.add(ACCOUNT.AGE).as("addAlias")) + .select(new RawQueryColumn("abc").as("aBc")) + .from(ACCOUNT); +// IDialect dialect = new CommonsDialectImpl(); + IDialect dialect = new OracleDialect(); +// IDialect dialect = new DmDialect(); + String sql = dialect.forSelectByQuery(queryWrapper); + System.out.println("sql = " + sql); + + } + @Test public void testSelectAllColumnsSql() { From fbffec18addb17ab0e7a99d05d891ed50ae7bd92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Tue, 19 Sep 2023 16:16:08 +0800 Subject: [PATCH 31/47] feat: add more UpdateChain.of method --- .../java/com/mybatisflex/core/update/UpdateChain.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/UpdateChain.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/UpdateChain.java index 25beb88f..272c710c 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/UpdateChain.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/UpdateChain.java @@ -48,12 +48,20 @@ public class UpdateChain extends QueryWrapperAdapter> implemen return new UpdateChain<>(baseMapper); } + public static UpdateChain of(BaseMapper baseMapper) { + return new UpdateChain<>(baseMapper); + } + public static UpdateChain of(T entityObject) { Class entityClass = (Class) ClassUtil.getUsefulClass(entityObject.getClass()); BaseMapper baseMapper = Mappers.ofEntityClass(entityClass); return new UpdateChain<>(baseMapper, entityObject); } + public static UpdateChain of(T entityObject, BaseMapper baseMapper) { + return new UpdateChain<>(baseMapper, entityObject); + } + public UpdateChain(BaseMapper baseMapper) { this.baseMapper = baseMapper; From 5fc7201b026d71189aa55bac7ca8a60dc99f4ffa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Tue, 19 Sep 2023 16:21:46 +0800 Subject: [PATCH 32/47] doc: update docs --- docs/zh/base/batch.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/zh/base/batch.md b/docs/zh/base/batch.md index d2ceadbf..94a06a95 100644 --- a/docs/zh/base/batch.md +++ b/docs/zh/base/batch.md @@ -87,7 +87,18 @@ Db.executeBatch(accounts, 1000, AccountMapper.class ``` 以上的 **错误** 示例,是因为没有用到 `mapper` 参数,因此,不仅仅 `Db.executeBatch` 返回的都是失败的内容,而且也起不到批量操作的作用。 +以上代码需要修改为: +```java +List accounts = .... +Db.executeBatch(accounts, 1000, AccountMapper.class + , (mapper, account) -> { + + UpdateChina.of(mapper) //使用 mapper 参数,才能起到批量执行的效果 + .set(Account::getUserName, "张三") + .update(); +}); +``` ## `Db.updateBatch` 方法 From c6517b4b270f9e6b61427dead48ac645d9761dea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Tue, 19 Sep 2023 19:58:57 +0800 Subject: [PATCH 33/47] style: update code style --- .../com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java | 1 + 1 file changed, 1 insertion(+) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java index c74ae30f..8f078f98 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java @@ -62,6 +62,7 @@ public class CommonsDialectImpl implements IDialect { return ASTERISK.equals(keyword) ? keyword : keywordWrap.wrap(keyword); } + @Override public String wrapColumnAlias(String keyword) { return ASTERISK.equals(keyword) ? keyword : keywordWrap.getPrefix() + keyword + keywordWrap.getSuffix(); } From 860160266520b11b521488aabadf51c8f535d260 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Wed, 20 Sep 2023 14:08:51 +0800 Subject: [PATCH 34/47] feat: add QueryWrapper.select(Iterable) method --- .../java/com/mybatisflex/core/query/QueryWrapper.java | 9 +++++++++ .../com/mybatisflex/core/query/QueryWrapperAdapter.java | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java index 6be007ad..bd4e3b5d 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java @@ -118,6 +118,15 @@ public class QueryWrapper extends BaseQueryWrapper { return this; } + public QueryWrapper select(Iterable queryColumns) { + for (QueryColumn column : queryColumns) { + if (column != null) { + addSelectColumn(column); + } + } + return this; + } + public QueryWrapper select(QueryColumn[]... queryColumns) { for (QueryColumn[] columnArray : queryColumns) { if (columnArray != null) { diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapperAdapter.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapperAdapter.java index b7850911..840b2e5c 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapperAdapter.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapperAdapter.java @@ -77,6 +77,12 @@ public class QueryWrapperAdapter> extends Query return (R) this; } + @Override + public R select(Iterable queryColumns) { + super.select(queryColumns); + return (R) this; + } + @Override public R select(QueryColumn[]... queryColumns) { super.select(queryColumns); From a1c2d75041d4c33970c0435ed2dde7eaaa674ba4 Mon Sep 17 00:00:00 2001 From: wanggaoquan Date: Thu, 21 Sep 2023 16:37:02 +0800 Subject: [PATCH 35/47] =?UTF-8?q?fix:=20=E6=9F=A5=E8=AF=A2=E6=9D=A1?= =?UTF-8?q?=E4=BB=B6OperatorQueryCondition\OperatorSelectCondition?= =?UTF-8?q?=E6=8F=90=E5=8F=96=E5=8F=82=E6=95=B0=E5=80=BC=E8=A6=81=E6=A3=80?= =?UTF-8?q?=E6=9F=A5effective(=E5=90=A6=E5=88=99sql=E4=B8=AD=E7=9A=84=3F?= =?UTF-8?q?=E6=95=B0=E9=87=8F=E4=B8=8E=E5=8F=82=E6=95=B0=E4=B8=8D=E5=AF=B9?= =?UTF-8?q?=E5=BA=94)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mybatisflex/core/query/OperatorQueryCondition.java | 2 +- .../com/mybatisflex/core/query/OperatorSelectCondition.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/OperatorQueryCondition.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/OperatorQueryCondition.java index 0974ffe0..258d75ff 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/OperatorQueryCondition.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/OperatorQueryCondition.java @@ -68,7 +68,7 @@ public class OperatorQueryCondition extends QueryCondition { @Override public Object getValue() { - return WrapperUtil.getValues(childCondition); + return checkEffective() ? WrapperUtil.getValues(childCondition) : null; } @Override diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/OperatorSelectCondition.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/OperatorSelectCondition.java index 6d9594d5..2de9e3ae 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/OperatorSelectCondition.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/OperatorSelectCondition.java @@ -71,7 +71,7 @@ public class OperatorSelectCondition extends QueryCondition { @Override public Object getValue() { - return queryWrapper.getAllValueArray(); + return checkEffective() ? queryWrapper.getAllValueArray() : null; } @Override From 22bf974899a536c4cbba780cbbfa3abf04a11011 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Fri, 22 Sep 2023 09:21:33 +0800 Subject: [PATCH 36/47] build: v1.6.6 release (^.^)YYa!! --- .../main/java/com/mybatisflex/test/ContributorsDocGen.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/ContributorsDocGen.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/ContributorsDocGen.java index b50b7b83..e76dc7cb 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/ContributorsDocGen.java +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/ContributorsDocGen.java @@ -73,11 +73,12 @@ public class ContributorsDocGen { if (userName.contains("@")) { userName = userName.substring(0, userName.indexOf("@")); } + if (StringUtil.isBlank(src)) { + src = "https://api.dicebear.com/7.x/initials/svg?seed=" + userName; + } markdown.append("|"); - if (StringUtil.isNotBlank(src)) { - markdown.append("![](" + src + ")"); - } + markdown.append("![](" + src + ")"); markdown.append(userName); startIndex++; From 1ada60e98c6f53e68836f091057be93b4943394b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Fri, 22 Sep 2023 09:32:02 +0800 Subject: [PATCH 37/47] build: v1.6.6 release (^.^)YYa!! --- mybatis-flex-annotation/pom.xml | 2 +- mybatis-flex-codegen/pom.xml | 2 +- mybatis-flex-core/pom.xml | 2 +- .../src/main/java/com/mybatisflex/core/FlexConsts.java | 2 +- mybatis-flex-dependencies/pom.xml | 2 +- mybatis-flex-processor/pom.xml | 2 +- mybatis-flex-solon-plugin/pom.xml | 2 +- mybatis-flex-spring-boot-starter/pom.xml | 2 +- mybatis-flex-spring/pom.xml | 2 +- mybatis-flex-test/mybatis-flex-native-test/pom.xml | 2 +- mybatis-flex-test/mybatis-flex-seata-test/pom.xml | 2 +- mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml | 2 +- mybatis-flex-test/mybatis-flex-spring-cloud-test/pom.xml | 2 +- mybatis-flex-test/mybatis-flex-spring-test/pom.xml | 2 +- mybatis-flex-test/pom.xml | 2 +- 15 files changed, 15 insertions(+), 15 deletions(-) diff --git a/mybatis-flex-annotation/pom.xml b/mybatis-flex-annotation/pom.xml index 716ad913..e2d60e17 100644 --- a/mybatis-flex-annotation/pom.xml +++ b/mybatis-flex-annotation/pom.xml @@ -5,7 +5,7 @@ parent com.mybatis-flex - 1.6.5 + 1.6.6 4.0.0 diff --git a/mybatis-flex-codegen/pom.xml b/mybatis-flex-codegen/pom.xml index 7a576415..a8f8a999 100644 --- a/mybatis-flex-codegen/pom.xml +++ b/mybatis-flex-codegen/pom.xml @@ -5,7 +5,7 @@ parent com.mybatis-flex - 1.6.5 + 1.6.6 4.0.0 diff --git a/mybatis-flex-core/pom.xml b/mybatis-flex-core/pom.xml index 9003cbcb..33b53d9c 100644 --- a/mybatis-flex-core/pom.xml +++ b/mybatis-flex-core/pom.xml @@ -5,7 +5,7 @@ parent com.mybatis-flex - 1.6.5 + 1.6.6 4.0.0 diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexConsts.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexConsts.java index 74ddd48e..2603c091 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexConsts.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexConsts.java @@ -27,7 +27,7 @@ public class FlexConsts { } public static final String NAME = "MyBatis-Flex"; - public static final String VERSION = "1.6.5"; + public static final String VERSION = "1.6.6"; public static final String SQL = "$$sql"; diff --git a/mybatis-flex-dependencies/pom.xml b/mybatis-flex-dependencies/pom.xml index 69524f00..838747a7 100644 --- a/mybatis-flex-dependencies/pom.xml +++ b/mybatis-flex-dependencies/pom.xml @@ -6,7 +6,7 @@ com.mybatis-flex parent - 1.6.5 + 1.6.6 mybatis-flex-dependencies diff --git a/mybatis-flex-processor/pom.xml b/mybatis-flex-processor/pom.xml index 1c0b06d7..11b5edd7 100644 --- a/mybatis-flex-processor/pom.xml +++ b/mybatis-flex-processor/pom.xml @@ -5,7 +5,7 @@ parent com.mybatis-flex - 1.6.5 + 1.6.6 4.0.0 diff --git a/mybatis-flex-solon-plugin/pom.xml b/mybatis-flex-solon-plugin/pom.xml index e36bafa1..ad0733a1 100644 --- a/mybatis-flex-solon-plugin/pom.xml +++ b/mybatis-flex-solon-plugin/pom.xml @@ -5,7 +5,7 @@ parent com.mybatis-flex - 1.6.5 + 1.6.6 4.0.0 diff --git a/mybatis-flex-spring-boot-starter/pom.xml b/mybatis-flex-spring-boot-starter/pom.xml index 04211803..21d8b60c 100644 --- a/mybatis-flex-spring-boot-starter/pom.xml +++ b/mybatis-flex-spring-boot-starter/pom.xml @@ -5,7 +5,7 @@ parent com.mybatis-flex - 1.6.5 + 1.6.6 4.0.0 diff --git a/mybatis-flex-spring/pom.xml b/mybatis-flex-spring/pom.xml index 51663020..cc01fb41 100644 --- a/mybatis-flex-spring/pom.xml +++ b/mybatis-flex-spring/pom.xml @@ -5,7 +5,7 @@ parent com.mybatis-flex - 1.6.5 + 1.6.6 4.0.0 diff --git a/mybatis-flex-test/mybatis-flex-native-test/pom.xml b/mybatis-flex-test/mybatis-flex-native-test/pom.xml index 7a228f4f..8fef2ffa 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/pom.xml +++ b/mybatis-flex-test/mybatis-flex-native-test/pom.xml @@ -5,7 +5,7 @@ mybatis-flex-test com.mybatis-flex - 1.6.5 + 1.6.6 4.0.0 diff --git a/mybatis-flex-test/mybatis-flex-seata-test/pom.xml b/mybatis-flex-test/mybatis-flex-seata-test/pom.xml index 5258a777..fb4363c5 100644 --- a/mybatis-flex-test/mybatis-flex-seata-test/pom.xml +++ b/mybatis-flex-test/mybatis-flex-seata-test/pom.xml @@ -4,7 +4,7 @@ mybatis-flex-test com.mybatis-flex - 1.6.5 + 1.6.6 4.0.0 diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml b/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml index 07b77ed5..514179c2 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml @@ -5,7 +5,7 @@ mybatis-flex-test com.mybatis-flex - 1.6.5 + 1.6.6 4.0.0 diff --git a/mybatis-flex-test/mybatis-flex-spring-cloud-test/pom.xml b/mybatis-flex-test/mybatis-flex-spring-cloud-test/pom.xml index 030feed2..4a2ec1a4 100644 --- a/mybatis-flex-test/mybatis-flex-spring-cloud-test/pom.xml +++ b/mybatis-flex-test/mybatis-flex-spring-cloud-test/pom.xml @@ -4,7 +4,7 @@ mybatis-flex-test com.mybatis-flex - 1.6.5 + 1.6.6 4.0.0 diff --git a/mybatis-flex-test/mybatis-flex-spring-test/pom.xml b/mybatis-flex-test/mybatis-flex-spring-test/pom.xml index 733cc45c..be5dcc8f 100644 --- a/mybatis-flex-test/mybatis-flex-spring-test/pom.xml +++ b/mybatis-flex-test/mybatis-flex-spring-test/pom.xml @@ -5,7 +5,7 @@ mybatis-flex-test com.mybatis-flex - 1.6.5 + 1.6.6 4.0.0 diff --git a/mybatis-flex-test/pom.xml b/mybatis-flex-test/pom.xml index 39f7d108..8ca60dce 100644 --- a/mybatis-flex-test/pom.xml +++ b/mybatis-flex-test/pom.xml @@ -5,7 +5,7 @@ parent com.mybatis-flex - 1.6.5 + 1.6.6 4.0.0 From 40bba6783b63d89d7a478d14dc91e630c6ab3c99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Fri, 22 Sep 2023 09:32:06 +0800 Subject: [PATCH 38/47] build: v1.6.6 release (^.^)YYa!! --- .gitee/ISSUE_TEMPLATE/bug.yml | 2 +- .gitee/ISSUE_TEMPLATE/question.yml | 2 +- changes.md | 14 ++++++++++++++ docs/zh/changes.md | 14 ++++++++++++++ docs/zh/intro/getting-started.md | 2 +- docs/zh/intro/gradle.md | 16 ++++++++-------- docs/zh/intro/maven.md | 14 +++++++------- docs/zh/others/apt.md | 2 +- docs/zh/others/codegen.md | 2 +- pom.xml | 4 ++-- 10 files changed, 50 insertions(+), 22 deletions(-) diff --git a/.gitee/ISSUE_TEMPLATE/bug.yml b/.gitee/ISSUE_TEMPLATE/bug.yml index 0cf461aa..4a491472 100644 --- a/.gitee/ISSUE_TEMPLATE/bug.yml +++ b/.gitee/ISSUE_TEMPLATE/bug.yml @@ -7,7 +7,7 @@ body: attributes: label: 这个 Bug 是否已经存在: options: - - label: 我确定已经把 MyBatis-Flex 升级到最新版本 v1.6.5,并已搜索过现有的问题 (https://gitee.com/mybatis-flex/mybatis-flex/issues) + - label: 我确定已经把 MyBatis-Flex 升级到最新版本 v1.6.6,并已搜索过现有的问题 (https://gitee.com/mybatis-flex/mybatis-flex/issues) required: true - type: textarea attributes: diff --git a/.gitee/ISSUE_TEMPLATE/question.yml b/.gitee/ISSUE_TEMPLATE/question.yml index e2395e68..2bf2cd8c 100644 --- a/.gitee/ISSUE_TEMPLATE/question.yml +++ b/.gitee/ISSUE_TEMPLATE/question.yml @@ -13,7 +13,7 @@ body: attributes: label: 这个问题是否已经存在: options: - - label: 我确定已经把 MyBatis-Flex 升级到最新版本 v1.6.5,并已搜索过现有的问题 (https://gitee.com/mybatis-flex/mybatis-flex/issues) + - label: 我确定已经把 MyBatis-Flex 升级到最新版本 v1.6.6,并已搜索过现有的问题 (https://gitee.com/mybatis-flex/mybatis-flex/issues) required: true - type: textarea id: question-description diff --git a/changes.md b/changes.md index b0d00571..7d397b7c 100644 --- a/changes.md +++ b/changes.md @@ -3,6 +3,20 @@ 查看 [全部代码贡献者](/zh/intro/what-is-mybatisflex.html#贡献者)。 + +## v1.6.6 20230922: +- 新增:UpdateChain.of 使用 Mapper 进行构造方便在批量操作使用的功能 +- 新增:QueryWrapper.select(Iterable) 方法,方便 Kotlin 扩展 +- 新增:Relation 注解新增 valueField 配置,当不为空串时值进行某个字段赋值,感谢 @ice-samll +- 优化:转驼峰方法多次转换保持结果一致,感谢 @617054137 +- 优化:生成列别名规范,保持用户原始的列别名命名,感谢 @font-c +- 修复:Db 或 MyBatis 原生查询驼峰转换需处理不包含下划线的字段,感谢 @617054137 +- 测试:增加 Relation 注解单字段赋值 Springboot 测试,感谢 @ice-samll +- 文档:添加 Relation 注解单字段赋值的相关文档,感谢 @ice-samll +- 文档:添加关于批量操作使用 UpdateChain 的相关示例 + + + ## v1.6.5 20230914: - 新增:代码生成器为 Oracle 的 JdbcTypeMapping 类型 OracleBlob 添加映射处理 - 新增:LogicDeleteManager 和 TenantManager 添加 Runnable 无返回值重载,感谢 @Suomm diff --git a/docs/zh/changes.md b/docs/zh/changes.md index b0d00571..7d397b7c 100644 --- a/docs/zh/changes.md +++ b/docs/zh/changes.md @@ -3,6 +3,20 @@ 查看 [全部代码贡献者](/zh/intro/what-is-mybatisflex.html#贡献者)。 + +## v1.6.6 20230922: +- 新增:UpdateChain.of 使用 Mapper 进行构造方便在批量操作使用的功能 +- 新增:QueryWrapper.select(Iterable) 方法,方便 Kotlin 扩展 +- 新增:Relation 注解新增 valueField 配置,当不为空串时值进行某个字段赋值,感谢 @ice-samll +- 优化:转驼峰方法多次转换保持结果一致,感谢 @617054137 +- 优化:生成列别名规范,保持用户原始的列别名命名,感谢 @font-c +- 修复:Db 或 MyBatis 原生查询驼峰转换需处理不包含下划线的字段,感谢 @617054137 +- 测试:增加 Relation 注解单字段赋值 Springboot 测试,感谢 @ice-samll +- 文档:添加 Relation 注解单字段赋值的相关文档,感谢 @ice-samll +- 文档:添加关于批量操作使用 UpdateChain 的相关示例 + + + ## v1.6.5 20230914: - 新增:代码生成器为 Oracle 的 JdbcTypeMapping 类型 OracleBlob 添加映射处理 - 新增:LogicDeleteManager 和 TenantManager 添加 Runnable 无返回值重载,感谢 @Suomm diff --git a/docs/zh/intro/getting-started.md b/docs/zh/intro/getting-started.md index 816fe812..61bc5e26 100644 --- a/docs/zh/intro/getting-started.md +++ b/docs/zh/intro/getting-started.md @@ -62,7 +62,7 @@ VALUES (1, '张三', 18, '2020-01-11'), com.mybatis-flex mybatis-flex-spring-boot-starter - 1.6.5 + 1.6.6 com.mysql diff --git a/docs/zh/intro/gradle.md b/docs/zh/intro/gradle.md index 014113c4..f2ec8521 100644 --- a/docs/zh/intro/gradle.md +++ b/docs/zh/intro/gradle.md @@ -10,7 +10,7 @@ ```kotlin dependencies { - implementation("com.mybatis-flex:mybatis-flex-core:1.6.5") + implementation("com.mybatis-flex:mybatis-flex-core:1.6.6") } ``` @@ -18,7 +18,7 @@ dependencies { ```groovy dependencies { - implementation 'com.mybatis-flex:mybatis-flex-core:1.6.5' + implementation 'com.mybatis-flex:mybatis-flex-core:1.6.6' } ``` @@ -28,7 +28,7 @@ dependencies { ```kotlin dependencies { - implementation("com.mybatis-flex:mybatis-flex-spring:1.6.5") + implementation("com.mybatis-flex:mybatis-flex-spring:1.6.6") } ``` @@ -36,7 +36,7 @@ dependencies { ```groovy dependencies { - implementation 'com.mybatis-flex:mybatis-flex-spring:1.6.5' + implementation 'com.mybatis-flex:mybatis-flex-spring:1.6.6' } ``` @@ -46,7 +46,7 @@ dependencies { ```kotlin dependencies { - implementation("com.mybatis-flex:mybatis-flex-spring-boot-starter:1.6.5") + implementation("com.mybatis-flex:mybatis-flex-spring-boot-starter:1.6.6") } ``` @@ -54,7 +54,7 @@ dependencies { ```groovy dependencies { - implementation 'com.mybatis-flex:mybatis-flex-spring-boot-starter:1.6.5' + implementation 'com.mybatis-flex:mybatis-flex-spring-boot-starter:1.6.6' } ``` @@ -70,7 +70,7 @@ dependencies { ```kotlin dependencies { - annotationProcessor("com.mybatis-flex:mybatis-flex-processor:1.6.5") + annotationProcessor("com.mybatis-flex:mybatis-flex-processor:1.6.6") } ``` @@ -78,6 +78,6 @@ dependencies { ```groovy dependencies { - annotationProcessor 'com.mybatis-flex:mybatis-flex-processor:1.6.5' + annotationProcessor 'com.mybatis-flex:mybatis-flex-processor:1.6.6' } ``` diff --git a/docs/zh/intro/maven.md b/docs/zh/intro/maven.md index 4f8c0078..28e2d4c8 100644 --- a/docs/zh/intro/maven.md +++ b/docs/zh/intro/maven.md @@ -12,12 +12,12 @@ com.mybatis-flex mybatis-flex-core - 1.6.5 + 1.6.6 com.mybatis-flex mybatis-flex-processor - 1.6.5 + 1.6.6 provided ``` @@ -28,12 +28,12 @@ com.mybatis-flex mybatis-flex-spring - 1.6.5 + 1.6.6 com.mybatis-flex mybatis-flex-processor - 1.6.5 + 1.6.6 provided `````` @@ -44,12 +44,12 @@ com.mybatis-flex mybatis-flex-spring-boot-starter - 1.6.5 + 1.6.6 com.mybatis-flex mybatis-flex-processor - 1.6.5 + 1.6.6 provided ``` @@ -72,7 +72,7 @@ com.mybatis-flex mybatis-flex-processor - 1.6.5 + 1.6.6 diff --git a/docs/zh/others/apt.md b/docs/zh/others/apt.md index f592ae69..2e23535a 100644 --- a/docs/zh/others/apt.md +++ b/docs/zh/others/apt.md @@ -220,7 +220,7 @@ pom.xml 添加 `annotationProcessorPaths` 配置, ``` dependencies { ... - annotationProcessor 'com.mybatis-flex:mybatis-flex-processor:1.6.5' + annotationProcessor 'com.mybatis-flex:mybatis-flex-processor:1.6.6' } ``` diff --git a/docs/zh/others/codegen.md b/docs/zh/others/codegen.md index ecb6ef03..2d1394e3 100644 --- a/docs/zh/others/codegen.md +++ b/docs/zh/others/codegen.md @@ -10,7 +10,7 @@ com.mybatis-flex mybatis-flex-codegen - 1.6.5 + 1.6.6 ``` diff --git a/pom.xml b/pom.xml index 882b8bcc..88270291 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ com.mybatis-flex parent pom - 1.6.5 + 1.6.6 mybatis-flex https://mybatis-flex.com @@ -55,7 +55,7 @@ 8 8 - 1.6.5 + 1.6.6 3.5.13 2.1.0 From 2ad08677505886ea0194fafafc8404b17c450c28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Fri, 22 Sep 2023 09:42:41 +0800 Subject: [PATCH 39/47] build: v1.6.6 release (^.^)YYa!! --- changes.md | 1 + docs/zh/changes.md | 1 + .../src/main/java/com/mybatisflex/core/query/WrapperUtil.java | 4 ++-- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/changes.md b/changes.md index 7d397b7c..8e38ae39 100644 --- a/changes.md +++ b/changes.md @@ -10,6 +10,7 @@ - 新增:Relation 注解新增 valueField 配置,当不为空串时值进行某个字段赋值,感谢 @ice-samll - 优化:转驼峰方法多次转换保持结果一致,感谢 @617054137 - 优化:生成列别名规范,保持用户原始的列别名命名,感谢 @font-c +- 修复:QueryWrapper 在某些场景下构建 SQL 会出现两个 AS 关键字的问题 - 修复:Db 或 MyBatis 原生查询驼峰转换需处理不包含下划线的字段,感谢 @617054137 - 测试:增加 Relation 注解单字段赋值 Springboot 测试,感谢 @ice-samll - 文档:添加 Relation 注解单字段赋值的相关文档,感谢 @ice-samll diff --git a/docs/zh/changes.md b/docs/zh/changes.md index 7d397b7c..8e38ae39 100644 --- a/docs/zh/changes.md +++ b/docs/zh/changes.md @@ -10,6 +10,7 @@ - 新增:Relation 注解新增 valueField 配置,当不为空串时值进行某个字段赋值,感谢 @ice-samll - 优化:转驼峰方法多次转换保持结果一致,感谢 @617054137 - 优化:生成列别名规范,保持用户原始的列别名命名,感谢 @font-c +- 修复:QueryWrapper 在某些场景下构建 SQL 会出现两个 AS 关键字的问题 - 修复:Db 或 MyBatis 原生查询驼峰转换需处理不包含下划线的字段,感谢 @617054137 - 测试:增加 Relation 注解单字段赋值 Springboot 测试,感谢 @ice-samll - 文档:添加 Relation 注解单字段赋值的相关文档,感谢 @ice-samll diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/WrapperUtil.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/WrapperUtil.java index dc51b06f..039b8f78 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/WrapperUtil.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/WrapperUtil.java @@ -122,7 +122,7 @@ class WrapperUtil { if (enumWrapper.hasEnumValueAnnotation()) { paras.add(enumWrapper.getEnumValue((Enum) value)); } else { - paras.add(((Enum)value).name()); + paras.add(((Enum) value).name()); } } else { paras.add(value); @@ -148,7 +148,7 @@ class WrapperUtil { } static String withAlias(String sql, String alias, IDialect dialect) { - return SqlConsts.BRACKET_LEFT + sql + SqlConsts.BRACKET_RIGHT + getAsKeyWord(dialect) + buildColumnAlias(alias, dialect); + return SqlConsts.BRACKET_LEFT + sql + SqlConsts.BRACKET_RIGHT + buildColumnAlias(alias, dialect); } static String buildAlias(String alias, IDialect dialect) { From 63d3f0f347332f6aacda9dd4a15027bc817fa2bc Mon Sep 17 00:00:00 2001 From: dhc Date: Fri, 22 Sep 2023 10:38:20 +0800 Subject: [PATCH 40/47] Fixed an issue where aliases that required parenthesis wrapping appeared "as" twice --- .../src/main/java/com/mybatisflex/core/query/WrapperUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/WrapperUtil.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/WrapperUtil.java index dc51b06f..dcd12b18 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/WrapperUtil.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/WrapperUtil.java @@ -148,7 +148,7 @@ class WrapperUtil { } static String withAlias(String sql, String alias, IDialect dialect) { - return SqlConsts.BRACKET_LEFT + sql + SqlConsts.BRACKET_RIGHT + getAsKeyWord(dialect) + buildColumnAlias(alias, dialect); + return SqlConsts.BRACKET_LEFT + sql + SqlConsts.BRACKET_RIGHT + buildColumnAlias(alias, dialect); } static String buildAlias(String alias, IDialect dialect) { From 0e1467a77c45ce3d1920976b7db6d81b84563542 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Fri, 22 Sep 2023 11:55:27 +0800 Subject: [PATCH 41/47] fix: apt generate code error if the entity class and field have the same name. --- .../processor/MybatisFlexProcessor.java | 4 ++ .../processor/builder/ContentBuilder.java | 25 ++++++++++-- .../java/com/mybatisflex/test/Account4.java | 38 +++++++++++++++++++ 3 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account4.java diff --git a/mybatis-flex-processor/src/main/java/com/mybatisflex/processor/MybatisFlexProcessor.java b/mybatis-flex-processor/src/main/java/com/mybatisflex/processor/MybatisFlexProcessor.java index a737c093..23e4d4cd 100644 --- a/mybatis-flex-processor/src/main/java/com/mybatisflex/processor/MybatisFlexProcessor.java +++ b/mybatis-flex-processor/src/main/java/com/mybatisflex/processor/MybatisFlexProcessor.java @@ -232,6 +232,10 @@ public class MybatisFlexProcessor extends AbstractProcessor { return SourceVersion.latestSupported(); } + + /** + * 通过 classElement 操作起所有字段,生成 ColumnInfo 并填充 columnInfos 结合 + */ private void fillColumnInfoList(Set columnInfos, List defaultColumns, TypeElement baseElement, TypeElement classElement, boolean camelToUnderline) { for (Element fieldElement : classElement.getEnclosedElements()) { diff --git a/mybatis-flex-processor/src/main/java/com/mybatisflex/processor/builder/ContentBuilder.java b/mybatis-flex-processor/src/main/java/com/mybatisflex/processor/builder/ContentBuilder.java index 3bf30f6c..ee056724 100644 --- a/mybatis-flex-processor/src/main/java/com/mybatisflex/processor/builder/ContentBuilder.java +++ b/mybatis-flex-processor/src/main/java/com/mybatisflex/processor/builder/ContentBuilder.java @@ -69,6 +69,9 @@ public class ContentBuilder { content.append("import com.mybatisflex.core.table.TableDef;\n\n"); content.append("// Auto generate by mybatis-flex, do not modify it.\n"); content.append("public class ").append(tableDefClassName).append(" extends TableDef {\n\n"); + + //TableDef 类的属性名称 + String tableDefPropertyName = null; if (!allInTablesEnable) { String entityComment = tableInfo.getEntityComment(); if (!StrUtil.isBlank(entityComment)) { @@ -76,9 +79,13 @@ public class ContentBuilder { .append(" * ").append(entityComment.trim()).append("\n") .append(" */\n"); } - content.append(" public static final ").append(tableDefClassName).append(' ').append(StrUtil.buildFieldName(tableInfo.getEntitySimpleName().concat(tableDefInstanceSuffix != null ? tableDefInstanceSuffix.trim() : ""), tableDefPropertiesNameStyle)) + tableDefPropertyName = StrUtil.buildFieldName(tableInfo.getEntitySimpleName().concat(tableDefInstanceSuffix != null ? tableDefInstanceSuffix.trim() : ""), tableDefPropertiesNameStyle); + content.append(" public static final ").append(tableDefClassName).append(' ').append(tableDefPropertyName) .append(" = new ").append(tableDefClassName).append("();\n\n"); } + + + String finalTableDefPropertyName = tableDefPropertyName; columnInfos.forEach(columnInfo -> { String comment = columnInfo.getComment(); if (!StrUtil.isBlank(comment)) { @@ -86,8 +93,16 @@ public class ContentBuilder { .append(" * ").append(comment.trim()).append("\n") .append(" */\n"); } + + // QueryColumn 属性定义的名称 + String columnPropertyName = StrUtil.buildFieldName(columnInfo.getProperty(), tableDefPropertiesNameStyle); + + //当字段名称和表名一样时,自动为字段添加一个小尾巴 "_",例如 account_ + if (columnPropertyName.equals(finalTableDefPropertyName)) { + columnPropertyName = columnPropertyName + "_"; + } content.append(" public final QueryColumn ") - .append(StrUtil.buildFieldName(columnInfo.getProperty(), tableDefPropertiesNameStyle)) + .append(columnPropertyName) .append(" = new QueryColumn(this, \"") .append(columnInfo.getColumn()).append("\""); if (columnInfo.getAlias() != null && columnInfo.getAlias().length > 0) { @@ -102,7 +117,11 @@ public class ContentBuilder { StringJoiner defaultColumnJoiner = new StringJoiner(", "); columnInfos.forEach(columnInfo -> { if (defaultColumns.contains(columnInfo.getColumn())) { - defaultColumnJoiner.add(StrUtil.buildFieldName(columnInfo.getProperty(), tableDefPropertiesNameStyle)); + String columnPropertyName = StrUtil.buildFieldName(columnInfo.getProperty(), tableDefPropertiesNameStyle); + if (columnPropertyName.equals(finalTableDefPropertyName)) { + columnPropertyName = columnPropertyName + "_"; + } + defaultColumnJoiner.add(columnPropertyName); } }); content.append("\n /**\n") diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account4.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account4.java new file mode 100644 index 00000000..4301f080 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account4.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.mybatisflex.test; + +import com.mybatisflex.annotation.Table; + +import java.io.Serializable; + +@Table(value = "tb_account_4") +public class Account4 extends Account2 implements Serializable { + + private static final long serialVersionUID = 1L; + + //字段名和类名相同 + private int account4; + + public int getAccount4() { + return account4; + } + + public void setAccount4(int account4) { + this.account4 = account4; + } +} From 0e27cd248f8465e14461e28e24c7c53d9f3d23a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Sat, 23 Sep 2023 10:43:11 +0800 Subject: [PATCH 42/47] fix: FlexConfiguration replace resultMap error --- .../java/com/mybatisflex/core/mybatis/FlexConfiguration.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java index 13e6f1a3..effcaf7e 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java @@ -188,7 +188,8 @@ public class FlexConfiguration extends Configuration { Class clazz = resultMap.getType(); //判断是否为表实体类 if (clazz.getDeclaredAnnotation(Table.class) != null) { - ms = replaceResultMap(ms, getTableInfo(ms)); + TableInfo tableInfo = TableInfoFactory.ofEntityClass(clazz); + ms = replaceResultMap(ms, tableInfo); } } } From af9fa76b2e83fcb125b731ea8c4838955745ea2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Sat, 23 Sep 2023 15:11:53 +0800 Subject: [PATCH 43/47] fix: AbstractRelation NPE --- .../java/com/mybatisflex/core/relation/AbstractRelation.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/AbstractRelation.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/AbstractRelation.java index 7cac0dd1..fb6f1fce 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/AbstractRelation.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/AbstractRelation.java @@ -98,7 +98,8 @@ abstract class AbstractRelation { this.valueField = valueField; this.onlyQueryValueField = StringUtil.isNotBlank(valueField); - this.conditionColumn = column(targetTable, targetTableInfo.getColumnByProperty(this.targetField.getName())); + this.conditionColumn = targetTableInfo == null ? column(targetTable, StringUtil.camelToUnderline(this.targetField.getName())) + : column(targetTable, targetTableInfo.getColumnByProperty(this.targetField.getName())); if (onlyQueryValueField) { //仅绑定字段时只需要查询关联列和该字段列即可 From 57a725af53b9137319b0295ad6d41b64f1ce1879 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Sat, 23 Sep 2023 17:47:39 +0800 Subject: [PATCH 44/47] optimize: remove unused property --- .../java/com/mybatisflex/core/datasource/DataSourceKey.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/DataSourceKey.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/DataSourceKey.java index 144a05fb..1979b471 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/DataSourceKey.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/DataSourceKey.java @@ -32,8 +32,6 @@ public class DataSourceKey { */ private static ThreadLocal manualKeyThreadLocal = new ThreadLocal<>(); - public static String manualKey; - private DataSourceKey() { } From 893a1f3455902ca8ac60ab8b2e6906345ff4e9b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Sat, 23 Sep 2023 18:44:30 +0800 Subject: [PATCH 45/47] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E8=AF=BB?= =?UTF-8?q?=E5=86=99=E5=88=86=E7=A6=BB=E7=BB=84=E4=BB=B6=20DataSourceShard?= =?UTF-8?q?ingStrategy.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/.vitepress/config.ts | 1 + docs/zh/core/read-write-splitting.md | 97 +++++++++++++++++++ .../core/datasource/DataSourceKey.java | 6 ++ .../core/datasource/DataSourceManager.java | 12 +++ .../DataSourceShardingStrategy.java | 22 +++++ .../core/mybatis/MapperInvocationHandler.java | 7 ++ 6 files changed, 145 insertions(+) create mode 100644 docs/zh/core/read-write-splitting.md create mode 100644 mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/DataSourceShardingStrategy.java diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index 557164ad..ab98cf90 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -88,6 +88,7 @@ export default defineConfig({ {text: 'SQL 审计', link: '/zh/core/audit'}, {text: 'SQL 打印', link: '/zh/core/sql-print'}, {text: '多数据源', link: '/zh/core/multi-datasource'}, + {text: '读写分离 💥', link: '/zh/core/read-write-splitting'}, {text: '数据源加密', link: '/zh/core/datasource-encryption'}, {text: '动态表名', link: '/zh/core/dynamic-table'}, {text: '事务管理', link: '/zh/core/tx'}, diff --git a/docs/zh/core/read-write-splitting.md b/docs/zh/core/read-write-splitting.md new file mode 100644 index 00000000..0648a5b0 --- /dev/null +++ b/docs/zh/core/read-write-splitting.md @@ -0,0 +1,97 @@ +# 读写分离 + +MyBatis-Flex 的读写分离功能是基于 【多数据源】 功能来实现的。 + +读写分离的功能,要求当前的环境必须是多个数据库(也可理解为多个数据源),其原理是: +让主数据库(master)处理事务性操作,比如:增、删、改(INSERT、DELETE、UPDATE),而从数据库(slave)处理 SELECT 查询操作。 + +在 MyBatis 框架中,我们知道: 所有关于数据库的的操作都是通过 Mapper 来实现的,Mapper 里的一个方法,往往是和一个 SQL 一一对应。 + +因此,在 MyBatis-Flex 中,提供了一种基于 Mapper 方法的读写分离策略。 + +## 分片策略 + +自定义 `DataSourceShardingStrategy` 例如: + +```java +public class MyStrategy implements DataSourceShardingStrategy { + + public String doSharding(String currentDataSourceKey + , Object mapper, Method mapperMethod, Object[] methodArgs){ + + //返回新的数据源 key + return "newDataSourceKey"; + } +} +``` + +doSharding 的参数分别为: + +- currentDataSourceKey:当前由用户端已配置的 key +- mapper:当前的 mapper 对象 +- mapperMethod: 当前的 mapper 方法 +- methodArgs:当前的 mapper 方法的参数内容 + +自定义好 数据源分片策略后,在项目启动时,需要通过 `DataSourceManager` 配置自己的自定义分片策略: + +```java +DataSourceManager.setDataSourceShardingStrategy(new MyStrategy()); +``` + +## 示例代码 + +假设数据源配置如下: + + +```yaml +mybatis-flex: + datasource: + master: + type: druid + url: jdbc:mysql://127.0.0.1:3306/master-db + username: root + password: 123456 + slave1: + type: com.your.datasource.type2 + url: jdbc:mysql://127.0.0.1:3306/slave1 + username: root + password: 123456 + slave2: + type: com.your.datasource.type2 + url: jdbc:mysql://127.0.0.1:3306/slave2 + username: root + password: 123456 + other: + type: com.your.datasource.type2 + url: jdbc:mysql://127.0.0.1:3306/other + username: root + password: 123456 +``` +以上配置中,一共有 4 个数据源,分别为 `master`、`slave1`、`slave2`、`other`。 +假设我们的需求是:在 增删改 时,走 master,而在查询时,自动使用 `slave1`、`slave2` 进行负载均衡。 + + +那么,我们的分片策略代码如下: + +```java +public class MyStrategy implements DataSourceShardingStrategy { + + public String doSharding(String currentDataSourceKey + , Object mapper, Method mapperMethod, Object[] methodArgs){ + + // 不管 other 数据源的情况 + if ("other".equals(currentDataSourceKey)){ + return currentDataSourceKey; + } + + // 如果 mapper 的方法属于 增删改,使用 master 数据源 + if (StringUtil.startWithAny(mapperMethod.getName(), + "insert", "delete", "update")){ + return "master"; + } + + //其他场景,使用 slave1 或者 slave2 进行负载均衡 + return "slave*"; + } +} +``` diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/DataSourceKey.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/DataSourceKey.java index 1979b471..a8cd92e2 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/DataSourceKey.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/DataSourceKey.java @@ -15,6 +15,7 @@ */ package com.mybatisflex.core.datasource; +import java.lang.reflect.Method; import java.util.function.Supplier; /** @@ -86,4 +87,9 @@ public class DataSourceKey { public static void setManualKeyThreadLocal(ThreadLocal manualKeyThreadLocal) { DataSourceKey.manualKeyThreadLocal = manualKeyThreadLocal; } + + public static String getByShardingStrategy(String dataSource, Object mapper, Method method, Object[] args) { + String shardingDsKey = DataSourceManager.getByShardingStrategy(dataSource, mapper, method, args); + return shardingDsKey != null ? shardingDsKey : dataSource; + } } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/DataSourceManager.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/DataSourceManager.java index 343ccd3e..497142a0 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/DataSourceManager.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/DataSourceManager.java @@ -37,6 +37,15 @@ public class DataSourceManager { DataSourceManager.decipher = decipher; } + private static DataSourceShardingStrategy dataSourceShardingStrategy; + + public static DataSourceShardingStrategy getDataSourceShardingStrategy() { + return dataSourceShardingStrategy; + } + + public static void setDataSourceShardingStrategy(DataSourceShardingStrategy dataSourceShardingStrategy) { + DataSourceManager.dataSourceShardingStrategy = dataSourceShardingStrategy; + } public static void decryptDataSource(DataSource dataSource) { if (decipher == null) { @@ -87,4 +96,7 @@ public class DataSourceManager { } + static String getByShardingStrategy(String dataSource, Object mapper, Method method, Object[] args) { + return dataSourceShardingStrategy != null ? dataSourceShardingStrategy.doSharding(dataSource, mapper, method, args) : null; + } } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/DataSourceShardingStrategy.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/DataSourceShardingStrategy.java new file mode 100644 index 00000000..a2b3fa8b --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/DataSourceShardingStrategy.java @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.mybatisflex.core.datasource; + +import java.lang.reflect.Method; + +public interface DataSourceShardingStrategy { + String doSharding(String currentDataSourceKey, Object mapper, Method mapperMethod, Object[] methodArgs); +} diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/MapperInvocationHandler.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/MapperInvocationHandler.java index 63636dc0..db6e1430 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/MapperInvocationHandler.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/MapperInvocationHandler.java @@ -67,6 +67,13 @@ public class MapperInvocationHandler implements InvocationHandler { } } + //最终通过数据源 自定义分片 策略去获取 + String shardingDataSourceKey = DataSourceKey.getByShardingStrategy(dataSourceKey, proxy, method, args); + if (shardingDataSourceKey != null && !shardingDataSourceKey.equals(dataSourceKey)) { + DataSourceKey.use(dataSourceKey); + needClearDsKey = true; + } + //优先获取用户自己配置的 dbType DbType dbType = DialectFactory.getHintDbType(); if (dbType == null) { From f839dfdd24182ba4c3303ae6dc5795be8bc2c819 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, 24 Sep 2023 09:24:50 +0800 Subject: [PATCH 46/47] doc: update docs --- docs/zh/core/multi-tenancy.md | 52 ++++++++++++++++++++++++++-- docs/zh/core/read-write-splitting.md | 23 +++++++----- 2 files changed, 65 insertions(+), 10 deletions(-) diff --git a/docs/zh/core/multi-tenancy.md b/docs/zh/core/multi-tenancy.md index ecac0dec..64e41e62 100644 --- a/docs/zh/core/multi-tenancy.md +++ b/docs/zh/core/multi-tenancy.md @@ -60,7 +60,56 @@ public interface TenantFactory { 除了显示租户自己的数据以外,还包含下级租户的数据,这种场景则要求 `getTenantIds` 返回多个值。 - **场景3**:忽略租户条件,由代码自定义条件查询,此项要求 `getTenantIds` 返回 null 或者 空数组。 +**注意!注意!注意!** +> 在整个应用中,应该只有一个 `TenantFactory` 实例,然后再通过其 `getTenantIds()` 方法里去获取当前的租户 ID,在 Spring 常见中,我们可以通过在 +> RequestContextHolder 中去获取当前的租户 ID。在其他框架中,我们可以通过自定义 ThreadLocal 去获取 TenantId。 +## 示例代码 + +```java +public class MyTenantFactory implements TenantFactory { + + public Object[] getTenantIds(){ + RequestAttributes attributes = RequestContextHolder.getRequestAttributes(); + Long tenantId = attributes.getAttribute("tenantId", RequestAttributes.SCOPE_REQUEST); + + return new Object[]{tenantId}; + } +} +``` + +当然,`MyTenantFactory` 需要正常工作,我们需要在 Spring 拦截器里,需要通过 request 去获取当前的租户 ID,并设置到 request 的 attribute,如下代码所示: + +```java +public class TenantInterceptor implements HandlerInterceptor { + + @Override + public boolean preHandle(HttpServletRequest request + , HttpServletResponse response, Object handler) throws Exception { + + //通过 request 去获取租户 ID + Long tenantId = getTenantIdByReuqest(request); + + //设置租户ID到 request 的 attribute + request.setAttribute("tenantId", tenantId); + + return true; + } +} +``` + +同时,在 `WebMvcConfigurer` 中,通过重写 `addInterceptors` 方法添加一下我们自定义的多租户拦截器:`TenantInterceptor`。 + +```java +@Configuration +public class WebConfig implements WebMvcConfigurer { + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(new TenantInterceptor()); + } +} +``` ## SpringBoot 支持 @@ -69,10 +118,9 @@ public interface TenantFactory { ```java @Configuration public class MyConfiguration { - @Bean public TenantFactory tenantFactory(){ - TenantFactory tenantFactory = new ....; + TenantFactory tenantFactory = new MyTenantFactory(); return tenantFactory; } diff --git a/docs/zh/core/read-write-splitting.md b/docs/zh/core/read-write-splitting.md index 0648a5b0..21852f4e 100644 --- a/docs/zh/core/read-write-splitting.md +++ b/docs/zh/core/read-write-splitting.md @@ -1,17 +1,20 @@ # 读写分离 -MyBatis-Flex 的读写分离功能是基于 【多数据源】 功能来实现的。 +MyBatis-Flex 的读写分离功能是基于 【[多数据源](./multi-datasource.md)】 功能来实现的。 -读写分离的功能,要求当前的环境必须是多个数据库(也可理解为多个数据源),其原理是: -让主数据库(master)处理事务性操作,比如:增、删、改(INSERT、DELETE、UPDATE),而从数据库(slave)处理 SELECT 查询操作。 +读写分离的功能,要求当前环境必须是多个数据库(也可理解为多个数据源),其原理是: +让主数据库(master)处理事务性操作,比如:增、删、改(INSERT、DELETE、UPDATE),而从数据库(slave)处理查询(SELECT)操作。 -在 MyBatis 框架中,我们知道: 所有关于数据库的的操作都是通过 Mapper 来实现的,Mapper 里的一个方法,往往是和一个 SQL 一一对应。 + +## 实现原理 + +在 MyBatis 框架中,我们知道: 所有关于数据库的的操作都是通过 Mapper 来进行的,Mapper 里的一个方法,往往是和一个执行 SQL 一一对应。 因此,在 MyBatis-Flex 中,提供了一种基于 Mapper 方法的读写分离策略。 -## 分片策略 +## 数据源分片策略 -自定义 `DataSourceShardingStrategy` 例如: +在 MyBatis-Flex 框架中,我们需要通过实现 `DataSourceShardingStrategy` 接口来自定义自己的数据源读写分离策略(分片策略)例如: ```java public class MyStrategy implements DataSourceShardingStrategy { @@ -27,7 +30,7 @@ public class MyStrategy implements DataSourceShardingStrategy { doSharding 的参数分别为: -- currentDataSourceKey:当前由用户端已配置的 key +- currentDataSourceKey:当前使用的数据源 key - mapper:当前的 mapper 对象 - mapperMethod: 当前的 mapper 方法 - methodArgs:当前的 mapper 方法的参数内容 @@ -68,7 +71,7 @@ mybatis-flex: password: 123456 ``` 以上配置中,一共有 4 个数据源,分别为 `master`、`slave1`、`slave2`、`other`。 -假设我们的需求是:在 增删改 时,走 master,而在查询时,自动使用 `slave1`、`slave2` 进行负载均衡。 +我们的需求是:在 增删改 时,走 master 数据源,而在查询时,随机自动使用 `slave1`、`slave2` 数据源进行负载均衡。 那么,我们的分片策略代码如下: @@ -95,3 +98,7 @@ public class MyStrategy implements DataSourceShardingStrategy { } } ``` + +## 注意事项 + +> MyBatis-Flex 的读写分离组件,只进行数据查询和数据操作时的读写分离,并不涉及主从数据库之间的数据同步,主从数据库同步需要用户自己在数据库服务器,通过第三方组件去实现。 From 1f1d9cdffe620aed5aac344a8c18df87d4d9c4e5 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, 24 Sep 2023 09:45:40 +0800 Subject: [PATCH 47/47] refactor: remove `@Inherited` from Table annotation --- .../src/main/java/com/mybatisflex/annotation/Table.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/Table.java b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/Table.java index 9c60c599..9abc8761 100644 --- a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/Table.java +++ b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/Table.java @@ -24,7 +24,7 @@ import java.lang.annotation.*; */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) -@Inherited +//@Inherited 需要注释,否则会在 vo 等继承 model 的实体类中,生成多余的、或冲突的 tableDef public @interface Table { /**