From f8ba0f3f9c3a1c18c246e7a62297a5a6d2c237f1 Mon Sep 17 00:00:00 2001 From: Tang Date: Fri, 28 Jun 2024 09:43:15 +0800 Subject: [PATCH] Support chinese pinyin search of mybatis flex for java generate template --- .../mybatisflex/query/QueryFunctions.java | 31 +++++++++++++++++++ .../mybatisflex/constants/FuncName.kt | 12 +++++++ .../vm/java/mybatisflex/mapper.java.vm | 4 ++- 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 tang-extension/tang-mybatis-flex/src/main/java/com/tang/extension/mybatisflex/query/QueryFunctions.java create mode 100644 tang-extension/tang-mybatis-flex/src/main/kotlin/com/tang/extension/mybatisflex/constants/FuncName.kt diff --git a/tang-extension/tang-mybatis-flex/src/main/java/com/tang/extension/mybatisflex/query/QueryFunctions.java b/tang-extension/tang-mybatis-flex/src/main/java/com/tang/extension/mybatisflex/query/QueryFunctions.java new file mode 100644 index 00000000..6a2a4bb2 --- /dev/null +++ b/tang-extension/tang-mybatis-flex/src/main/java/com/tang/extension/mybatisflex/query/QueryFunctions.java @@ -0,0 +1,31 @@ +package com.tang.extension.mybatisflex.query; + +import com.mybatisflex.core.query.FunctionQueryColumn; +import com.mybatisflex.core.query.QueryColumn; +import com.mybatisflex.core.util.LambdaGetter; +import com.mybatisflex.core.util.LambdaUtil; +import com.tang.extension.mybatisflex.constants.FuncName; + +/** + * 自定义查询函数 + * + * @author Tang + */ +public class QueryFunctions { + + private QueryFunctions() { + } + + public static QueryColumn toPinyin(String columnX) { + return new FunctionQueryColumn(FuncName.TO_PINYIN, columnX); + } + + public static QueryColumn toPinyin(QueryColumn columnX) { + return new FunctionQueryColumn(FuncName.TO_PINYIN, columnX); + } + + public static QueryColumn toPinyin(LambdaGetter columnX) { + return new FunctionQueryColumn(FuncName.TO_PINYIN, LambdaUtil.getQueryColumn(columnX)); + } + +} diff --git a/tang-extension/tang-mybatis-flex/src/main/kotlin/com/tang/extension/mybatisflex/constants/FuncName.kt b/tang-extension/tang-mybatis-flex/src/main/kotlin/com/tang/extension/mybatisflex/constants/FuncName.kt new file mode 100644 index 00000000..5161c09a --- /dev/null +++ b/tang-extension/tang-mybatis-flex/src/main/kotlin/com/tang/extension/mybatisflex/constants/FuncName.kt @@ -0,0 +1,12 @@ +package com.tang.extension.mybatisflex.constants + +/** + * 自定义函数名 + * + * @author Tang + */ +object FuncName { + + const val TO_PINYIN = "TO_PINYIN" + +} diff --git a/tang-tool/tang-generator/src/main/resources/vm/java/mybatisflex/mapper.java.vm b/tang-tool/tang-generator/src/main/resources/vm/java/mybatisflex/mapper.java.vm index 6c30744f..600ca330 100644 --- a/tang-tool/tang-generator/src/main/resources/vm/java/mybatisflex/mapper.java.vm +++ b/tang-tool/tang-generator/src/main/resources/vm/java/mybatisflex/mapper.java.vm @@ -6,6 +6,7 @@ import com.mybatisflex.core.BaseMapper; import com.mybatisflex.core.query.QueryWrapper; import com.tang.commons.utils.If; import com.tang.commons.utils.page.PageResult; +import com.tang.extension.mybatisflex.query.QueryFunctions; import com.tang.extension.mybatisflex.utils.PageUtils; import ${packageName}.entity.$ClassName; @@ -33,7 +34,8 @@ public interface ${ClassName}Mapper extends BaseMapper<$ClassName> { queryWrapper.and(${CLASS_NAME}.${column.columnNameUpperUnderscore}.eq(${className}.get${column.columnNameUpperCamel}(), If::nonEmpty)); #end #if($column.queryType == "fuzzy") - queryWrapper.and(${CLASS_NAME}.${column.columnNameUpperUnderscore}.like(${className}.get${column.columnNameUpperCamel}(), If::nonEmpty)); + queryWrapper.and(${CLASS_NAME}.${column.columnNameUpperUnderscore}.like(${className}.get${column.columnNameUpperCamel}(), If::nonEmpty) + .or(QueryFunctions.toPinyin(${CLASS_NAME}.${column.columnNameUpperUnderscore}).like(${className}.get${column.columnNameUpperCamel}(), If::nonEmpty))); #end #if($column.queryType == "findInSet") queryWrapper.and(${CLASS_NAME}.${column.columnNameUpperUnderscore}.in(${className}.get${column.columnNameUpperCamel}()));