From 389b2a47bdf9325c2bd79c08c78c867b0d9db1db Mon Sep 17 00:00:00 2001 From: luodexin143 Date: Sun, 1 Dec 2019 22:11:45 +0800 Subject: [PATCH 1/4] TargetPackagePlugin --- README.md | 22 ++- .../plugins/TargetPackagePlugin.java | 138 ++++++++++++++++++ 2 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/itfsw/mybatis/generator/plugins/TargetPackagePlugin.java diff --git a/README.md b/README.md index 2bcb6859..5c704b50 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ * [状态枚举生成插件(EnumTypeStatusPlugin)](#21-状态枚举生成插件) * [增量插件(IncrementPlugin)](#22-增量插件) * [Mapper注解插件(MapperAnnotationPlugin)](#23-Mapper注解插件) +* [修改目标包名插件](#24-修改目标包名插件) --------------------------------------- Maven引用: @@ -1654,4 +1655,23 @@ public class Test { -``` \ No newline at end of file +``` + +### 24.修改目标包名插件 +有的项目组要求按照业务逻辑划分子包,在业务子包中进一步分controller、service, entity、mapper...., ModelClass需要生成在entity包中,ClientClass需要生成在mapper包中,而SQLMAP文件需要在/src/map/xxx/文件夹下,`targetPackage`属性和`domainObjectName`无法实现 +TargetPackagePlugin可用为一个表的ModelClass、ClientClass、SQLMap分别设置不同的targetPackage + +插件: +```xml + + + + + + + +
+
+``` + +生成的三类文件的包名分别为标签中配置的`targetPackage`属性与标签中配置的`xxxTargetPackage`属性的拼接结果 diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/TargetPackagePlugin.java b/src/main/java/com/itfsw/mybatis/generator/plugins/TargetPackagePlugin.java new file mode 100644 index 00000000..aace5eb9 --- /dev/null +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/TargetPackagePlugin.java @@ -0,0 +1,138 @@ +package com.itfsw.mybatis.generator.plugins; + +import com.itfsw.mybatis.generator.plugins.utils.BasePlugin; +import org.mybatis.generator.api.IntrospectedTable; +import org.mybatis.generator.config.JavaClientGeneratorConfiguration; +import org.mybatis.generator.config.JavaModelGeneratorConfiguration; +import org.mybatis.generator.config.SqlMapGeneratorConfiguration; +import org.mybatis.generator.config.TableConfiguration; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/*** + * Copyright (C), 2010,9 szcport + * File name and path: com.itfsw.mybatis.generator.plugins.TargetPackagePlugin + * Author : luodexin, Version : 1.0, First complete date: + * Description : + * + * Others : + * Function List: + * 1. xxx + * 2. xxx + * + * History: + * 1. Date: 2019-11-26 + * Version: + * Author: + * Modification: + * + **/ + +public class TargetPackagePlugin extends BasePlugin { + /** + * 模型类目标子包 + */ + public static final String PRO_CLIENT_TARGET_PACKAGE = "clientTargetPackage"; + /** + * mapper接口目标子包 + */ + public static final String PRO_MODEL_TARGET_PACKAGE = "modelTargetPackage"; + /** + * mapper文件目标子包 + */ + public static final String PRO_SQL_MAP_TARGET_PACKAGE = "sqlMapTargetPackage"; + + private static Pattern regex = Pattern.compile("[a-zA-Z]+[0-9a-zA-Z_]*(\\.[a-zA-Z]+[0-9a-zA-Z_]*)*"); + + @Override + public boolean validate(List warnings) { + // 如果table配置了domainObjectName或者mapperName就不要再启动该插件了 + for (TableConfiguration tableConfiguration : context.getTableConfigurations()) { + if (tableConfiguration.getDomainObjectName() != null || tableConfiguration.getMapperName() != null) { + warnings.add("itfsw:插件" + this.getClass().getTypeName() + "插件请不要配合table的domainObjectName或者mapperName一起使用!"); + return false; + } + } + return super.validate(warnings); + } + + @Override + public void initialized(IntrospectedTable introspectedTable) { + super.initialized(introspectedTable); + // 1.设置 Client 的子包 + String clientTargetPackage = getValidTargetPackage(introspectedTable, PRO_CLIENT_TARGET_PACKAGE); + if (null == clientTargetPackage) { + logger.warn("itfsw:插件" + this.getClass().getTypeName() + " 设置的clientTargetPackage属性不是有效包名"); + } else { + JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = context.getJavaClientGeneratorConfiguration(); + String targetPackage = javaClientGeneratorConfiguration.getTargetPackage() + "." + clientTargetPackage; + javaClientGeneratorConfiguration.setTargetPackage(targetPackage); + + try { + Method calculateJavaClientAttributes = IntrospectedTable.class.getDeclaredMethod("calculateJavaClientAttributes"); + calculateJavaClientAttributes.setAccessible(true); + calculateJavaClientAttributes.invoke(introspectedTable); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + logger.error("itfsw:插件" + this.getClass().getTypeName() + "设置clientTargetPackage时异常"); + } + } + + // 2.设置 Model 的子包 + String modelTargetPackage = getValidTargetPackage(introspectedTable, PRO_MODEL_TARGET_PACKAGE); + if (null == modelTargetPackage) { + logger.warn("itfsw:插件" + this.getClass().getTypeName() + " 设置的modelTargetPackage属性不是有效包名"); + } else { + JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = context.getJavaModelGeneratorConfiguration(); + String targetPackage = javaModelGeneratorConfiguration.getTargetPackage() + "." + modelTargetPackage; + logger.info("targetPackage:" + targetPackage); + javaModelGeneratorConfiguration.setTargetPackage(targetPackage); + + try { + Method calculateModelAttributes = IntrospectedTable.class.getDeclaredMethod("calculateModelAttributes"); + calculateModelAttributes.setAccessible(true); + calculateModelAttributes.invoke(introspectedTable); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + logger.error("itfsw:插件" + this.getClass().getTypeName() + "设置modelTargetPackage时异常"); + } + } + + // 3.设置 sqlMap 的子包 + String sqlMapTargetPackage = getValidTargetPackage(introspectedTable, PRO_SQL_MAP_TARGET_PACKAGE); + if (null == sqlMapTargetPackage) { + logger.warn("itfsw:插件" + this.getClass().getTypeName() + " 设置的sqlMapTargetPackage属性不是有效包名"); + } else { + SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = context.getSqlMapGeneratorConfiguration(); + String targetPackage = sqlMapGeneratorConfiguration.getTargetPackage() + "." + sqlMapTargetPackage; + sqlMapGeneratorConfiguration.setTargetPackage(targetPackage); + + try { + Method calculateXmlAttributes = IntrospectedTable.class.getDeclaredMethod("calculateXmlAttributes"); + calculateXmlAttributes.setAccessible(true); + calculateXmlAttributes.invoke(introspectedTable); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + logger.error("itfsw:插件" + this.getClass().getTypeName() + "设置sqlMapTargetPackage时异常"); + } + } + } + + /** + * 检查包名格式是否正确 + */ + private String getValidTargetPackage(IntrospectedTable introspectedTable, final String PROPERTY_NAME) { + String targetPacakge = introspectedTable.getTableConfigurationProperty(PROPERTY_NAME); + if (null == targetPacakge || "".equals(targetPacakge.trim())) { + return null; + } + + targetPacakge = targetPacakge.trim(); + Matcher matcher = regex.matcher(targetPacakge); + return matcher.matches() ? targetPacakge : null; + } +} From e0589407ea5e87a1d390b5ec5ef7f5d5fc4d3068 Mon Sep 17 00:00:00 2001 From: luodexin143 Date: Sun, 1 Dec 2019 22:17:28 +0800 Subject: [PATCH 2/4] update --- .../generator/plugins/TargetPackagePlugin.java | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/TargetPackagePlugin.java b/src/main/java/com/itfsw/mybatis/generator/plugins/TargetPackagePlugin.java index aace5eb9..97609764 100644 --- a/src/main/java/com/itfsw/mybatis/generator/plugins/TargetPackagePlugin.java +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/TargetPackagePlugin.java @@ -14,21 +14,8 @@ import java.util.regex.Pattern; /*** - * Copyright (C), 2010,9 szcport - * File name and path: com.itfsw.mybatis.generator.plugins.TargetPackagePlugin - * Author : luodexin, Version : 1.0, First complete date: - * Description : * - * Others : - * Function List: - * 1. xxx - * 2. xxx - * - * History: - * 1. Date: 2019-11-26 - * Version: - * Author: - * Modification: + * TargetPackagePlugin可用为一个表的ModelClass、ClientClass、SQLMap分别设置不同的targetPackage * **/ From 19666ad559c4e5260dd46704ff2fe7df3a80fe95 Mon Sep 17 00:00:00 2001 From: luodexin143 Date: Sun, 1 Dec 2019 22:21:24 +0800 Subject: [PATCH 3/4] update --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5c704b50..01580c93 100644 --- a/README.md +++ b/README.md @@ -1657,7 +1657,7 @@ public class Test { ``` -### 24.修改目标包名插件 +### 24. 修改目标包名插件 有的项目组要求按照业务逻辑划分子包,在业务子包中进一步分controller、service, entity、mapper...., ModelClass需要生成在entity包中,ClientClass需要生成在mapper包中,而SQLMAP文件需要在/src/map/xxx/文件夹下,`targetPackage`属性和`domainObjectName`无法实现 TargetPackagePlugin可用为一个表的ModelClass、ClientClass、SQLMap分别设置不同的targetPackage From 049b94eb1afdada197cfc80373b08a2ab85a280a Mon Sep 17 00:00:00 2001 From: luodexin143 Date: Sun, 1 Dec 2019 22:33:19 +0800 Subject: [PATCH 4/4] update readme.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 01580c93..283bc6ea 100644 --- a/README.md +++ b/README.md @@ -1674,4 +1674,4 @@ TargetPackagePlugin可用为一个表的ModelClass、ClientClass、SQLMap分别 ``` -生成的三类文件的包名分别为标签中配置的`targetPackage`属性与
标签中配置的`xxxTargetPackage`属性的拼接结果 +生成的三类文件的包名分别为``、``、``标签中配置的`targetPackage`属性与`
`标签中配置的`xxxTargetPackage`属性的拼接结果