From a37bdaabf1afaba86ae244d4b7937e3563669f97 Mon Sep 17 00:00:00 2001 From: hewei Date: Sat, 10 May 2025 11:15:30 +0800 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20=E5=AF=B9=E4=BA=8E=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=B8=B8=E7=94=A8=E7=B1=BB=EF=BC=8C=E5=90=AF=E7=94=A8?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=8A=98=E5=8F=A0=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/config/GlobalConfig.java | 30 +++++++----- .../java/com/mybatisflex/core/BaseMapper.java | 14 +++--- .../mybatisflex/core/query/QueryMethods.java | 48 ++++++++++++------- .../mybatisflex/core/service/IService.java | 22 +++++---- 4 files changed, 71 insertions(+), 43 deletions(-) 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 a22d0a1b..7fe44b32 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 @@ -42,7 +42,7 @@ public class GlobalConfig implements Serializable { private final FileType fileType; - // === 必须配置 === + //region === 必须配置 === public GlobalConfig() { this(FileType.JAVA); @@ -52,8 +52,9 @@ public class GlobalConfig implements Serializable { private final PackageConfig packageConfig; private final StrategyConfig strategyConfig; private final TemplateConfig templateConfig; + //endregion === 必须配置 === - // === 可选配置 === + //region === 可选配置 === private EntityConfig entityConfig; private MapperConfig mapperConfig; @@ -62,11 +63,13 @@ public class GlobalConfig implements Serializable { private ControllerConfig controllerConfig; private TableDefConfig tableDefConfig; private MapperXmlConfig mapperXmlConfig; + //endregion === 可选配置 === - // === 其他自定义配置 === + //region === 其他自定义配置 === private Map customConfig = new HashMap<>(); + //endregion === 其他自定义配置 === - // === 是否启用生成 === + //region === 是否启用生成 === private boolean entityGenerateEnable; private boolean mapperGenerateEnable; @@ -122,8 +125,9 @@ public class GlobalConfig implements Serializable { } } + //endregion === 是否启用生成 === - // === 分类配置 === + //region === 分类配置 === public JavadocConfig getJavadocConfig() { return javadocConfig; @@ -189,8 +193,9 @@ public class GlobalConfig implements Serializable { } return mapperXmlConfig; } + //endregion === 分类配置 === - // === 启用配置 === + //region === 启用配置 === public EntityConfig enableEntity() { entityGenerateEnable = true; @@ -230,8 +235,9 @@ public class GlobalConfig implements Serializable { public void enablePackageInfo() { packageInfoGenerateEnable = true; } + //endregion === 启用配置 === - // === 禁用配置 === + //region === 禁用配置 === public void disableEntity() { entityGenerateEnable = false; @@ -264,9 +270,9 @@ public class GlobalConfig implements Serializable { public void disablePackageInfo() { packageInfoGenerateEnable = false; } + //endregion === 禁用配置 === - - // === 自定义配置 === + //region === 自定义配置 === public Object getCustomConfig(String key) { return customConfig.get(key); @@ -283,7 +289,9 @@ public class GlobalConfig implements Serializable { public void setCustomConfig(Map customConfig) { this.customConfig = customConfig; } - // === 分项配置 === + //endregion === 自定义配置 === + + //region === 分项配置 === /** * @see JavadocConfig#getAuthor() @@ -1566,5 +1574,5 @@ public class GlobalConfig implements Serializable { public void setPackageInfoGenerateEnable(boolean packageInfoGenerateEnable) { this.packageInfoGenerateEnable = packageInfoGenerateEnable; } - + //endregion === 分项配置 === } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java index 396938c5..d83b7271 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java @@ -74,7 +74,7 @@ public interface BaseMapper { */ int DEFAULT_BATCH_SIZE = 1000; - // === 增(insert) === + //region === 增(insert) === /** * 插入实体类数据,不忽略 {@code null} 值。 @@ -253,8 +253,9 @@ public interface BaseMapper { return update(entity, ignoreNulls); } } + //endregion === 增(insert) === - // === 删(delete) === + //region === 删(delete) === /** * 根据实体主键来删除数据。 @@ -343,8 +344,9 @@ public interface BaseMapper { */ @DeleteProvider(type = EntitySqlProvider.class, method = "deleteByQuery") int deleteByQuery(@Param(FlexConsts.QUERY) QueryWrapper queryWrapper); + //endregion === 删(delete) === - // === 改(update) === + //region === 改(update) === /** * 根据主键来更新数据,若实体类属性数据为 {@code null},该属性不会更新到数据库。 @@ -439,9 +441,9 @@ public interface BaseMapper { */ @UpdateProvider(type = EntitySqlProvider.class, method = "updateByQuery") int updateByQuery(@Param(FlexConsts.ENTITY) T entity, @Param(FlexConsts.IGNORE_NULLS) boolean ignoreNulls, @Param(FlexConsts.QUERY) QueryWrapper queryWrapper); + //endregion === 改(update) === - - // === 查(select) === + //region === 改(update) === /** * 根据实体主键查询数据。 @@ -1264,5 +1266,5 @@ public interface BaseMapper { } return page; } - + //endregion === 改(update) === } 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 88198e37..67f8c08a 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 @@ -35,7 +35,7 @@ public class QueryMethods { private QueryMethods() { } - // === 数学函数 === + //region === 数学函数 === /** * 返回 x 的绝对值。 @@ -680,8 +680,9 @@ public class QueryMethods { public static QueryColumn cot(LambdaGetter columnX) { return new FunctionQueryColumn(COT, LambdaUtil.getQueryColumn(columnX)); } + //endregion === 数学函数 === - // === 字符串函数 === + //region === 字符串函数 === /** * 返回字符串 s 的字符数。 @@ -1181,8 +1182,9 @@ public class QueryMethods { public static QueryColumn findInSet(LambdaGetter columnS1, LambdaGetter columnS2) { return new FunctionQueryColumn(FIND_IN_SET, LambdaUtil.getQueryColumn(columnS1), LambdaUtil.getQueryColumn(columnS2)); } + //endregion === 字符串函数 === - // === 日期时间函数 === + //region === 日期时间函数 === /** * 返回当前日期。 @@ -1884,8 +1886,9 @@ public class QueryMethods { public static QueryColumn getFormat(LambdaGetter columnType, LambdaGetter columnS) { return new FunctionQueryColumn(GET_FORMAT, LambdaUtil.getQueryColumn(columnType), LambdaUtil.getQueryColumn(columnS)); } + //endregion === 日期时间函数 === - // === 系统信息函数 === + //region === 系统信息函数 === /** * 返回数据库的版本号。 @@ -1970,8 +1973,9 @@ public class QueryMethods { public static QueryColumn lastInsertId() { return new FunctionQueryColumn(LAST_INSERT_ID); } + //endregion === 系统信息函数 === - // === 加密函数 === + //region === 加密函数 === /** * 对字符串 str 进行加密。 @@ -2056,8 +2060,9 @@ public class QueryMethods { public static QueryColumn decode(LambdaGetter columnCryptStr, LambdaGetter

columnPswdStr) { return new FunctionQueryColumn(DECODE, LambdaUtil.getQueryColumn(columnCryptStr), LambdaUtil.getQueryColumn(columnPswdStr)); } + //endregion === 加密函数 === - // === 其他函数 === + //region === 其他函数 === /** * 格式化函数,可以将数字 x 进行格式化,将 x 保留到小数点后 n 位,这个过程需要进行四舍五入。 @@ -2226,8 +2231,9 @@ public class QueryMethods { public static QueryColumn inetNtoa(LambdaGetter columnN) { return new FunctionQueryColumn(INET_NTOA, LambdaUtil.getQueryColumn(columnN)); } + //endregion === 其他函数 === - // === 聚合函数 === + //region === 聚合函数 === /** * 返回指定列的最大值。 @@ -2312,8 +2318,9 @@ public class QueryMethods { public static FunctionQueryColumn sum(LambdaGetter column) { return new FunctionQueryColumn(SUM, LambdaUtil.getQueryColumn(column)); } + //endregion === 聚合函数 === - // === COUNT === + //region === COUNT === /** * 返回指定列的总行数。 @@ -2342,9 +2349,9 @@ public class QueryMethods { public static FunctionQueryColumn count(LambdaGetter column) { return new FunctionQueryColumn(COUNT, LambdaUtil.getQueryColumn(column)); } + //endregion === COUNT === - - // === DISTINCT === + //region === DISTINCT === /** * 对指定列进行去重。 @@ -2358,8 +2365,9 @@ public class QueryMethods { return new DistinctQueryColumn(Arrays.stream(columns) .map(LambdaUtil::getQueryColumn).toArray(QueryColumn[]::new)); } + //endregion === DISTINCT === - // === CASE THEN ELSE === + //region === CASE THEN ELSE === /** * 构建 case then when 语句。 @@ -2374,8 +2382,9 @@ public class QueryMethods { public static CaseSearchQueryColumn.Builder case_(QueryColumn column) { return new CaseSearchQueryColumn.Builder(column); } + //endregion === CASE THEN ELSE === - // === CONVERT === + //region === CONVERT === /** * 将所给类型类型转换为另一种类型。 @@ -2383,8 +2392,9 @@ public class QueryMethods { public static StringFunctionQueryColumn convert(String... params) { return new StringFunctionQueryColumn(CONVERT, params); } + //endregion === CONVERT === - // === 构建 column 列 === + //region === 构建 column 列 === /** * 构建 TRUE 常量。 @@ -2494,8 +2504,9 @@ public class QueryMethods { } return queryColumns; } + //endregion === 构建 column 列 === - // === IF 函数 === + //region === IF 函数 === /** * IF 函数。 @@ -2552,9 +2563,9 @@ public class QueryMethods { public static QueryColumn ifNull(LambdaGetter nullColumn, String elseColumn) { return ifNull(nullColumn, new QueryColumn(elseColumn)); } + //endregion === IF 函数 === - - // === 构建 QueryCondition 查询条件 === + //region === 构建 QueryCondition 查询条件 === /** * EXIST (SELECT ...) @@ -2597,8 +2608,9 @@ public class QueryMethods { public static QueryCondition bracket(QueryCondition condition) { return new Brackets(condition); } + //endregion === 构建 QueryCondition 查询条件 === - // === 构建 QueryWrapper 查询 === + //region === 构建 QueryWrapper 查询 === /** * SELECT queryColumns FROM table @@ -2673,5 +2685,5 @@ public class QueryMethods { public static FunctionQueryColumn date(QueryColumn column) { return new FunctionQueryColumn("DATE", column); } - + //endregion === 构建 QueryWrapper 查询 === } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/service/IService.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/service/IService.java index 830bf816..81a85761 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/service/IService.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/service/IService.java @@ -51,7 +51,7 @@ public interface IService { */ BaseMapper getMapper(); - // ===== 保存(增)操作 ===== + //region ===== 保存(增)操作 ===== /** *

保存实体类对象数据。 @@ -128,8 +128,9 @@ public interface IService { Class> usefulClass = (Class>) ClassUtil.getUsefulClass(getMapper().getClass()); return SqlUtil.toBool(Db.executeBatch(entities, batchSize, usefulClass, BaseMapper::insertOrUpdateSelective)); } + //endregion ===== 保存(增)操作 ===== - // ===== 删除(删)操作 ===== + //region ===== 删除(删)操作 ===== /** *

根据查询条件删除数据。 @@ -197,8 +198,9 @@ public interface IService { } return remove(query().where(query)); } + //endregion ===== 删除(删)操作 ===== - // ===== 更新(改)操作 ===== + //region ===== 更新(改)操作 ===== /** *

根据数据主键更新数据。 @@ -313,8 +315,9 @@ public interface IService { Class> usefulClass = (Class>) ClassUtil.getUsefulClass(getMapper().getClass()); return SqlUtil.toBool(Db.executeBatch(entities, batchSize, usefulClass, (mapper, entity) -> mapper.update(entity, ignoreNulls))); } + //endregion ===== 更新(改)操作 ===== - // ===== 查询(查)操作 ===== + //region ===== 查询(查)操作 ===== /** *

根据数据主键查询一条数据。 @@ -546,8 +549,9 @@ public interface IService { default List listByMap(Map query) { return list(query().where(query)); } + //endregion ===== 查询(查)操作 ===== - // ===== 数量查询操作 ===== + //region ===== 数量查询操作 ===== /** *

根据查询条件判断数据是否存在。 @@ -605,8 +609,9 @@ public interface IService { default long count(QueryCondition condition) { return count(query().where(condition)); } + //endregion ===== 数量查询操作 ===== - // ===== 分页查询操作 ===== + //region ===== 分页查询操作 ===== /** *

分页查询所有数据。 @@ -651,8 +656,9 @@ public interface IService { default Page pageAs(Page page, QueryWrapper query, Class asType) { return getMapper().paginateAs(page, query, asType); } + //endregion ===== 分页查询操作 ===== - // ===== 查询包装器操作 ===== + //region ===== 查询包装器操作 ===== /** * 默认 {@link QueryWrapper} 构建。 @@ -680,5 +686,5 @@ public interface IService { default UpdateChain updateChain() { return UpdateChain.create(getMapper()); } - + //endregion ===== 查询包装器操作 ===== } From c969ee388de0fa5ff7a5fd338d6dae03febe9b26 Mon Sep 17 00:00:00 2001 From: wcc1433 <37837522+wcc1433@users.noreply.github.com> Date: Sun, 25 May 2025 15:40:10 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E5=A4=9A=E7=8E=AF=E5=A2=83=E5=AF=BC?= =?UTF-8?q?=E5=85=A5=E7=9B=B8=E5=90=8C=20Mapper=20=E5=86=B2=E7=AA=81?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 创建多个环境,每个环境加载相同 Mapper (例如 RowMapper)会导致环境冲突。 代码: // 环境实例1 MybatisFlexBootstrap client1 = ...; client1.addMapper(RowMapper.class); // 环境实例2: MybatisFlexBootstrap client2 = // ; client2.addMapper(RowMapper.class); client1.getMapper(RowMapper.class); // client2 拿到的永远都是 client1 的 RowMapper client2.getMapper(RowMapper.class); --- .../main/java/com/mybatisflex/core/mybatis/Mappers.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/Mappers.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/Mappers.java index 89b1da1d..f30512ec 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/Mappers.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/Mappers.java @@ -28,6 +28,7 @@ import com.mybatisflex.core.util.MapUtil; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; +import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -43,7 +44,7 @@ public class Mappers { private Mappers() { } - private static final Map, Object> MAPPER_OBJECTS = new ConcurrentHashMap<>(); + private static final Map, Object>> MAPPER_OBJECTS_OF_ENV = new ConcurrentHashMap<>(); private static final Map, Class> ENTITY_MAPPER_MAP = new ConcurrentHashMap<>(); @@ -79,14 +80,16 @@ public class Mappers { * @return {@link BaseMapper} 对象 */ public static M ofMapperClass(Class mapperClass) { - Object mapperObject = MapUtil.computeIfAbsent(MAPPER_OBJECTS, mapperClass, clazz -> + Map, Object> mapperObjects = MapUtil.computeIfAbsent(MAPPER_OBJECTS_OF_ENV, "default", envId -> new HashMap<>()); + Object mapperObject = MapUtil.computeIfAbsent(mapperObjects, mapperClass, clazz -> Proxy.newProxyInstance(mapperClass.getClassLoader() , new Class[]{mapperClass} , new MapperHandler(mapperClass))); return (M) mapperObject; } public static M ofMapperClass(String environmentId, Class mapperClass) { - Object mapperObject = MapUtil.computeIfAbsent(MAPPER_OBJECTS, mapperClass, clazz -> + Map, Object> mapperObjects = MapUtil.computeIfAbsent(MAPPER_OBJECTS_OF_ENV, environmentId, envId -> new HashMap<>()); + Object mapperObject = MapUtil.computeIfAbsent(mapperObjects, mapperClass, clazz -> Proxy.newProxyInstance(mapperClass.getClassLoader() , new Class[]{mapperClass} , new MapperHandler(environmentId, mapperClass))); From cc374a6aa224c4475df66edc4d0d78759028848f Mon Sep 17 00:00:00 2001 From: wcc1433 <37837522+wcc1433@users.noreply.github.com> Date: Mon, 26 May 2025 19:19:00 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E5=A4=9A=E4=B8=AA=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=EF=BC=8C=E6=AF=8F=E4=B8=AA=E7=8E=AF=E5=A2=83=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E7=9B=B8=E5=90=8C=20Mapper=20=EF=BC=88=E4=BE=8B=E5=A6=82=20Row?= =?UTF-8?q?Mapper=EF=BC=89=E4=BC=9A=E5=AF=BC=E8=87=B4=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E5=86=B2=E7=AA=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 创建多个环境,每个环境加载相同 Mapper (例如 RowMapper)会导致环境冲突。 代码: // 环境实例1 MybatisFlexBootstrap client1 = ...; client1.addMapper(RowMapper.class); // 环境实例2: MybatisFlexBootstrap client2 = // ; client2.addMapper(RowMapper.class); client1.getMapper(RowMapper.class); // client2 拿到的永远都是 client1 的 RowMapper client2.getMapper(RowMapper.class); --- .../src/main/java/com/mybatisflex/core/mybatis/Mappers.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/Mappers.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/Mappers.java index f30512ec..95d5491c 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/Mappers.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/Mappers.java @@ -28,7 +28,6 @@ import com.mybatisflex.core.util.MapUtil; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; -import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -80,7 +79,7 @@ public class Mappers { * @return {@link BaseMapper} 对象 */ public static M ofMapperClass(Class mapperClass) { - Map, Object> mapperObjects = MapUtil.computeIfAbsent(MAPPER_OBJECTS_OF_ENV, "default", envId -> new HashMap<>()); + Map, Object> mapperObjects = MapUtil.computeIfAbsent(MAPPER_OBJECTS_OF_ENV, "default", envId -> new ConcurrentHashMap<>()); Object mapperObject = MapUtil.computeIfAbsent(mapperObjects, mapperClass, clazz -> Proxy.newProxyInstance(mapperClass.getClassLoader() , new Class[]{mapperClass} @@ -88,7 +87,7 @@ public class Mappers { return (M) mapperObject; } public static M ofMapperClass(String environmentId, Class mapperClass) { - Map, Object> mapperObjects = MapUtil.computeIfAbsent(MAPPER_OBJECTS_OF_ENV, environmentId, envId -> new HashMap<>()); + Map, Object> mapperObjects = MapUtil.computeIfAbsent(MAPPER_OBJECTS_OF_ENV, environmentId, envId -> new ConcurrentHashMap<>()); Object mapperObject = MapUtil.computeIfAbsent(mapperObjects, mapperClass, clazz -> Proxy.newProxyInstance(mapperClass.getClassLoader() , new Class[]{mapperClass} From 62b28a33a4cc64ebf367ce886fedb348c619f2ee Mon Sep 17 00:00:00 2001 From: benshi <807629978@qq.com> Date: Tue, 27 May 2025 21:09:18 +0800 Subject: [PATCH 4/5] feat(Column): add setter for property Signed-off-by: benshi <807629978@qq.com> --- .../src/main/java/com/mybatisflex/codegen/entity/Column.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java index 2520e337..5df83580 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java @@ -93,6 +93,10 @@ public class Column { this.property = buildPropertyName(); } + public void setProperty(String property) { + this.property = property; + } + public String getProperty() { return property; } From dcc9dfb187fba8904964fc6547c381d45ecf9bd7 Mon Sep 17 00:00:00 2001 From: benshi <807629978@qq.com> Date: Wed, 28 May 2025 22:35:29 +0800 Subject: [PATCH 5/5] =?UTF-8?q?feat(Column):=20=E6=94=AF=E6=8C=81=E5=B8=A6?= =?UTF-8?q?=E6=9C=89=E8=8C=83=E5=9E=8B=E7=9A=84=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 例如`java.util.Optional`这种类型 Signed-off-by: benshi <807629978@qq.com> --- .../mybatisflex/codegen/entity/Column.java | 173 ++++++++++++------ 1 file changed, 122 insertions(+), 51 deletions(-) diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java index 2520e337..4fc461bc 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java @@ -110,12 +110,58 @@ public class Column { if (!columnConfig.getPropertyType().contains(".")) { return columnConfig.getPropertyType(); } - return StringUtil.substringAfterLast(columnConfig.getPropertyType(), "."); + return convertToSimpleGenericType(columnConfig.getPropertyType()); } else { - return StringUtil.substringAfterLast(propertyType, "."); + return convertToSimpleGenericType(propertyType); } } + private String convertToSimpleGenericType(String fullType) { + if (fullType == null || fullType.isEmpty()) { + return fullType; + } + + // 如果不包含泛型,直接处理 + if (!fullType.contains("<") && !fullType.endsWith(">")) { + return StringUtil.substringAfterLast(fullType, "."); + } + + StringBuilder result = new StringBuilder(); + int i = 0; + + while (i < fullType.length()) { + char c = fullType.charAt(i); + + if (c == '<' || c == ',' || c == '>') { + result.append(c); + if (c == ',' || c == '<') { + // 跳过空格 + while (i + 1 < fullType.length() && fullType.charAt(i + 1) == ' ') { + i++; + result.append(' '); + } + } + i++; + } else { + // 提取类型名称 + int start = i; + while (i < fullType.length() && fullType.charAt(i) != '<' && + fullType.charAt(i) != ',' && fullType.charAt(i) != '>') { + i++; + } + + String typeName = fullType.substring(start, i).trim(); + if (!typeName.isEmpty()) { + // 转换为简单类名 + String simpleType = StringUtil.substringAfterLast(typeName, "."); + result.append(simpleType); + } + } + } + + return result.toString(); + } + public void setPropertyType(String propertyType) { this.propertyType = propertyType; } @@ -197,8 +243,8 @@ public class Column { return ""; } else { return "/**\n" + - " * " + comment + "\n" + - " */"; + " * " + comment + "\n" + + " */"; } } @@ -212,16 +258,41 @@ public class Column { */ private static void addImportClass(Set importClasses, String importClass) { importClass = importClass.trim(); + extractAllTypes(importClasses, importClass); + } - // java.util.List >>>>> java.util.List - if (importClass.contains("<") && importClass.endsWith(">")) { - importClass = importClass.substring(0, importClass.indexOf("<")); + private static void extractAllTypes(Set importClasses, String typeString) { + if (typeString == null || typeString.isEmpty()) { + return; } - // 不包含“.”则认为是原始类型,不需要import - // lang 包不需要显式导入 - if (importClass.contains(".") && !importClass.startsWith("java.lang.")) { - importClasses.add(importClass); + int i = 0; + while (i < typeString.length()) { + // 跳过非字母字符 + while (i < typeString.length() && !Character.isLetter(typeString.charAt(i)) + && typeString.charAt(i) != '_') { + i++; + } + + if (i >= typeString.length()) { + break; + } + + // 提取类名 + int start = i; + while (i < typeString.length() && + (Character.isLetterOrDigit(typeString.charAt(i)) || + typeString.charAt(i) == '_' || + typeString.charAt(i) == '.')) { + i++; + } + + String className = typeString.substring(start, i); + + // 检查是否包含包路径且不是java.lang包 + if (className.contains(".") && !className.startsWith("java.lang.")) { + importClasses.add(className); + } } } @@ -234,7 +305,7 @@ public class Column { public String buildAnnotations() { StringBuilder annotations = new StringBuilder(); - //@Id 的注解 + // @Id 的注解 if (isPrimaryKey || columnConfig.isPrimaryKey()) { annotations.append("@Id("); @@ -259,14 +330,13 @@ public class Column { needComma = true; } - if (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.hasText(comment)) { addComma(annotations, needComma); annotations.append("comment = \"") - .append(this.comment.replace("\n", "") - .replace("\"", "\\\"") - .trim()) - .append("\""); + .append(this.comment.replace("\n", "") + .replace("\"", "\\\"") + .trim()) + .append("\""); } if (annotations.length() == 4) { @@ -281,25 +351,25 @@ public class Column { } boolean needGenColumnAnnotation = (entityConfig != null && entityConfig.isAlwaysGenColumnAnnotation()) - || !name.equalsIgnoreCase(StringUtil.camelToUnderline(property)) - || (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.hasText(this.comment) && annotations.length() == 0); + || !name.equalsIgnoreCase(StringUtil.camelToUnderline(property)) + || (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.hasText(this.comment) + && annotations.length() == 0); StringBuilder columnAnnotation = new StringBuilder("@Column("); - //@Column 注解 + // @Column 注解 if (columnConfig.getOnInsertValue() != null - || columnConfig.getOnUpdateValue() != null - || columnConfig.getLarge() != null - || columnConfig.getLogicDelete() != null - || columnConfig.getVersion() != null - || columnConfig.getJdbcType() != null - || columnConfig.getTypeHandler() != null - || columnConfig.getTenantId() != null - || needGenColumnAnnotation - ) { + || columnConfig.getOnUpdateValue() != null + || columnConfig.getLarge() != null + || columnConfig.getLogicDelete() != null + || columnConfig.getVersion() != null + || columnConfig.getJdbcType() != null + || columnConfig.getTypeHandler() != null + || columnConfig.getTenantId() != null + || needGenColumnAnnotation) { boolean needComma = false; if (entityConfig != null && entityConfig.isAlwaysGenColumnAnnotation() - || !name.equalsIgnoreCase(StringUtil.camelToUnderline(property))) { + || !name.equalsIgnoreCase(StringUtil.camelToUnderline(property))) { columnAnnotation.append("value = \"").append(name).append("\""); needComma = true; } @@ -336,7 +406,8 @@ public class Column { } if (columnConfig.getTypeHandler() != null) { addComma(columnAnnotation, needComma); - columnAnnotation.append("typeHandler = ").append(columnConfig.getTypeHandler().getSimpleName()).append(".class"); + columnAnnotation.append("typeHandler = ").append(columnConfig.getTypeHandler().getSimpleName()) + .append(".class"); needComma = true; } if (Boolean.TRUE.equals(columnConfig.getTenantId())) { @@ -347,10 +418,10 @@ public class Column { if (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.hasText(comment)) { addComma(columnAnnotation, needComma); columnAnnotation.append("comment = \"") - .append(this.comment.replace("\n", "") - .replace("\"", "\\\"") - .trim()) - .append("\""); + .append(this.comment.replace("\n", "") + .replace("\"", "\\\"") + .trim()) + .append("\""); } columnAnnotation.append(")"); @@ -418,19 +489,19 @@ public class Column { } boolean needGenColumnAnnotation = (entityConfig != null && entityConfig.isAlwaysGenColumnAnnotation()) - || !name.equalsIgnoreCase(StringUtil.camelToUnderline(property)) - || (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.hasText(this.comment)); + || !name.equalsIgnoreCase(StringUtil.camelToUnderline(property)) + || (entityConfig != null && entityConfig.isColumnCommentEnable() + && StringUtil.hasText(this.comment)); if (columnConfig.getOnInsertValue() != null - || columnConfig.getOnUpdateValue() != null - || columnConfig.getLarge() != null - || columnConfig.getLogicDelete() != null - || columnConfig.getVersion() != null - || columnConfig.getJdbcType() != null - || columnConfig.getTypeHandler() != null - || Boolean.TRUE.equals(columnConfig.getTenantId()) - || needGenColumnAnnotation - ) { + || columnConfig.getOnUpdateValue() != null + || columnConfig.getLarge() != null + || columnConfig.getLogicDelete() != null + || columnConfig.getVersion() != null + || columnConfig.getJdbcType() != null + || columnConfig.getTypeHandler() != null + || Boolean.TRUE.equals(columnConfig.getTenantId()) + || needGenColumnAnnotation) { addImportClass(importClasses, com.mybatisflex.annotation.Column.class.getName()); } } @@ -450,11 +521,11 @@ public class Column { @Override public String toString() { return "Column{" + - "name='" + name + '\'' + - ", className='" + propertyType + '\'' + - ", remarks='" + comment + '\'' + - ", isAutoIncrement=" + isAutoIncrement + - '}'; + "name='" + name + '\'' + + ", className='" + propertyType + '\'' + + ", remarks='" + comment + '\'' + + ", isAutoIncrement=" + isAutoIncrement + + '}'; } }