bugfix[issues#7]:修正TablePrefixPlugin和TableRenamePlugin的实现逻辑
This commit is contained in:
@@ -17,7 +17,11 @@
|
||||
package com.itfsw.mybatis.generator.plugins;
|
||||
|
||||
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.IntrospectedTableTools;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.config.TableConfiguration;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
@@ -32,10 +36,26 @@ public class TablePrefixPlugin extends BasePlugin {
|
||||
public static final String PRE_PREFIX = "prefix"; // 前缀 property
|
||||
private String prefix; // 前缀
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean validate(List<String> warnings) {
|
||||
// 如果table配置了domainObjectName或者mapperName就不要再启动该插件了
|
||||
for (TableConfiguration tableConfiguration : context.getTableConfigurations()) {
|
||||
if (tableConfiguration.getDomainObjectName() != null || tableConfiguration.getMapperName() != null) {
|
||||
logger.warn("itfsw:插件" + this.getClass().getTypeName() + "插件请不要配合table的domainObjectName或者mapperName一起使用!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return super.validate(warnings);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 初始化阶段
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
*
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
@@ -44,61 +64,19 @@ public class TablePrefixPlugin extends BasePlugin {
|
||||
// 1. 首先获取全局配置
|
||||
this.prefix = getProperties().getProperty(PRE_PREFIX);
|
||||
// 2. 获取每个table 具体的
|
||||
if (introspectedTable.getTableConfigurationProperty(PRE_PREFIX) != null){
|
||||
if (introspectedTable.getTableConfigurationProperty(PRE_PREFIX) != null) {
|
||||
this.prefix = introspectedTable.getTableConfigurationProperty(PRE_PREFIX);
|
||||
}
|
||||
// 3. 判断是否配置了前缀
|
||||
if (this.prefix != null){
|
||||
// 3.1. 为Model增加前缀
|
||||
if (introspectedTable.getBaseRecordType() != null){
|
||||
introspectedTable.setBaseRecordType(this.renameJavaType(introspectedTable.getBaseRecordType()));
|
||||
}
|
||||
|
||||
// 3.2. 为ModelKey添加前缀
|
||||
if (introspectedTable.getPrimaryKeyType() != null){
|
||||
introspectedTable.setPrimaryKeyType(this.renameJavaType(introspectedTable.getPrimaryKeyType()));
|
||||
}
|
||||
|
||||
// 3.3. WithBLOBs Model 添加前缀
|
||||
if (introspectedTable.getRecordWithBLOBsType() != null){
|
||||
introspectedTable.setRecordWithBLOBsType(this.renameJavaType(introspectedTable.getRecordWithBLOBsType()));
|
||||
}
|
||||
|
||||
// 3.4. mapper 添加前缀
|
||||
if (introspectedTable.getMyBatis3JavaMapperType() != null){
|
||||
introspectedTable.setMyBatis3JavaMapperType(this.renameJavaType(introspectedTable.getMyBatis3JavaMapperType()));
|
||||
}
|
||||
|
||||
// 3.5. example 添加前缀
|
||||
if (introspectedTable.getExampleType() != null){
|
||||
introspectedTable.setExampleType(this.renameJavaType(introspectedTable.getExampleType()));
|
||||
}
|
||||
|
||||
// 3.6. Dao 添加前缀
|
||||
if (introspectedTable.getDAOInterfaceType() != null){
|
||||
introspectedTable.setDAOInterfaceType(this.renameJavaType(introspectedTable.getDAOInterfaceType()));
|
||||
}
|
||||
|
||||
// 3.7. DAOImpl 添加前缀
|
||||
if (introspectedTable.getDAOImplementationType() != null){
|
||||
introspectedTable.setDAOImplementationType(this.renameJavaType(introspectedTable.getDAOImplementationType()));
|
||||
}
|
||||
|
||||
// 3.8. 修正xml文件前缀
|
||||
if (introspectedTable.getMyBatis3XmlMapperFileName() != null){
|
||||
introspectedTable.setMyBatis3XmlMapperFileName(this.prefix + introspectedTable.getMyBatis3XmlMapperFileName());
|
||||
// !!! TableRenamePlugin 插件的 tableOverride 优先级最高
|
||||
if (this.prefix != null && introspectedTable.getTableConfigurationProperty(TableRenamePlugin.PRE_TABLE_OVERRIDE) == null) {
|
||||
String domainObjectName = introspectedTable.getFullyQualifiedTable().getDomainObjectName();
|
||||
domainObjectName = prefix + domainObjectName;
|
||||
try {
|
||||
IntrospectedTableTools.setDomainObjectName(introspectedTable, getContext(), domainObjectName);
|
||||
} catch (Exception e) {
|
||||
logger.error("itfsw:插件" + this.getClass().getTypeName() + "使用prefix替换时异常!", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 为类型添加前缀
|
||||
*
|
||||
* @param type
|
||||
* @return
|
||||
*/
|
||||
private String renameJavaType(String type){
|
||||
int lastDot = type.lastIndexOf(".") + 1;
|
||||
return type.substring(0, lastDot) + this.prefix + type.substring(lastDot);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,10 @@
|
||||
package com.itfsw.mybatis.generator.plugins;
|
||||
|
||||
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.IntrospectedTableTools;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.config.TableConfiguration;
|
||||
import org.mybatis.generator.internal.util.JavaBeansUtil;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
@@ -49,6 +52,14 @@ public class TableRenamePlugin extends BasePlugin {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 如果table配置了domainObjectName或者mapperName就不要再启动该插件了
|
||||
for (TableConfiguration tableConfiguration: context.getTableConfigurations()) {
|
||||
if (tableConfiguration.getDomainObjectName() != null || tableConfiguration.getMapperName() != null){
|
||||
logger.warn("itfsw:插件" + this.getClass().getTypeName() + "插件请不要配合table的domainObjectName或者mapperName一起使用!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return super.validate(warnings);
|
||||
}
|
||||
|
||||
@@ -63,122 +74,26 @@ public class TableRenamePlugin extends BasePlugin {
|
||||
// 1. 获取表单独配置
|
||||
if (introspectedTable.getTableConfigurationProperty(PRE_TABLE_OVERRIDE) != null) {
|
||||
String override = introspectedTable.getTableConfigurationProperty(PRE_TABLE_OVERRIDE);
|
||||
|
||||
// 3.1. Model修正名称
|
||||
if (introspectedTable.getBaseRecordType() != null){
|
||||
introspectedTable.setBaseRecordType(this.renameJavaType(introspectedTable.getBaseRecordType(), override, ""));
|
||||
}
|
||||
|
||||
// 3.2. ModelKey修正名称
|
||||
if (introspectedTable.getPrimaryKeyType() != null){
|
||||
introspectedTable.setPrimaryKeyType(this.renameJavaType(introspectedTable.getPrimaryKeyType(), override, "Key"));
|
||||
}
|
||||
|
||||
// 3.3. WithBLOBs Model 修正名称
|
||||
if (introspectedTable.getRecordWithBLOBsType() != null){
|
||||
introspectedTable.setRecordWithBLOBsType(this.renameJavaType(introspectedTable.getRecordWithBLOBsType(), override, "WithBLOBs"));
|
||||
}
|
||||
|
||||
// 3.4. mapper 修正名称
|
||||
if (introspectedTable.getMyBatis3JavaMapperType() != null){
|
||||
introspectedTable.setMyBatis3JavaMapperType(this.renameJavaType(introspectedTable.getMyBatis3JavaMapperType(), override, "Mapper"));
|
||||
}
|
||||
|
||||
// 3.5. example 修正名称
|
||||
if (introspectedTable.getExampleType() != null){
|
||||
introspectedTable.setExampleType(this.renameJavaType(introspectedTable.getExampleType(), override, "Example"));
|
||||
}
|
||||
|
||||
// 3.6. Dao 添加前缀
|
||||
if (introspectedTable.getDAOInterfaceType() != null){
|
||||
introspectedTable.setDAOInterfaceType(this.renameJavaType(introspectedTable.getDAOInterfaceType(), override, "Dao"));
|
||||
}
|
||||
|
||||
// 3.7. DAOImpl 添加前缀
|
||||
if (introspectedTable.getDAOImplementationType() != null){
|
||||
introspectedTable.setDAOImplementationType(this.renameJavaType(introspectedTable.getDAOImplementationType(), override, "DAOImpl"));
|
||||
}
|
||||
|
||||
// 3.8. 修正xml文件名称
|
||||
if (introspectedTable.getMyBatis3XmlMapperFileName() != null){
|
||||
introspectedTable.setMyBatis3XmlMapperFileName(override + "Mapper.xml");
|
||||
try {
|
||||
IntrospectedTableTools.setDomainObjectName(introspectedTable, getContext(), override);
|
||||
} catch (Exception e) {
|
||||
logger.error("itfsw:插件" + this.getClass().getTypeName() + "使用tableOverride替换时异常!", e);
|
||||
}
|
||||
} else if (getProperties().getProperty(PRE_SEARCH_STRING) != null){
|
||||
String searchString = getProperties().getProperty(PRE_SEARCH_STRING);
|
||||
String replaceString = getProperties().getProperty(PRE_REPLACE_STRING);
|
||||
|
||||
// 3.1. Model修正名称
|
||||
if (introspectedTable.getBaseRecordType() != null){
|
||||
introspectedTable.setBaseRecordType(this.renameJavaType(introspectedTable.getBaseRecordType(), searchString, replaceString, ""));
|
||||
}
|
||||
|
||||
// 3.2. ModelKey修正名称
|
||||
if (introspectedTable.getPrimaryKeyType() != null){
|
||||
introspectedTable.setPrimaryKeyType(this.renameJavaType(introspectedTable.getPrimaryKeyType(), searchString, replaceString, "Key"));
|
||||
}
|
||||
|
||||
// 3.3. WithBLOBs Model 修正名称
|
||||
if (introspectedTable.getRecordWithBLOBsType() != null){
|
||||
introspectedTable.setRecordWithBLOBsType(this.renameJavaType(introspectedTable.getRecordWithBLOBsType(), searchString, replaceString, "WithBLOBs"));
|
||||
}
|
||||
|
||||
// 3.4. mapper 修正名称
|
||||
if (introspectedTable.getMyBatis3JavaMapperType() != null){
|
||||
introspectedTable.setMyBatis3JavaMapperType(this.renameJavaType(introspectedTable.getMyBatis3JavaMapperType(), searchString, replaceString, "Mapper"));
|
||||
}
|
||||
|
||||
// 3.5. example 修正名称
|
||||
if (introspectedTable.getExampleType() != null){
|
||||
introspectedTable.setExampleType(this.renameJavaType(introspectedTable.getExampleType(), searchString, replaceString, "Example"));
|
||||
}
|
||||
|
||||
// 3.6. Dao 添加前缀
|
||||
if (introspectedTable.getDAOInterfaceType() != null){
|
||||
introspectedTable.setDAOInterfaceType(this.renameJavaType(introspectedTable.getDAOInterfaceType(), searchString, replaceString, "Dao"));
|
||||
}
|
||||
|
||||
// 3.7. DAOImpl 添加前缀
|
||||
if (introspectedTable.getDAOImplementationType() != null){
|
||||
introspectedTable.setDAOImplementationType(this.renameJavaType(introspectedTable.getDAOImplementationType(), searchString, replaceString, "DAOImpl"));
|
||||
}
|
||||
|
||||
// 3.8. 修正xml文件名称
|
||||
if (introspectedTable.getMyBatis3XmlMapperFileName() != null){
|
||||
Pattern pattern = Pattern.compile(searchString);
|
||||
Matcher matcher = pattern.matcher(introspectedTable.getMyBatis3XmlMapperFileName());
|
||||
String fileName = matcher.replaceAll(replaceString);
|
||||
introspectedTable.setMyBatis3XmlMapperFileName(fileName);
|
||||
String domainObjectName = introspectedTable.getFullyQualifiedTable().getDomainObjectName();
|
||||
Pattern pattern = Pattern.compile(searchString);
|
||||
Matcher matcher = pattern.matcher(domainObjectName);
|
||||
domainObjectName = matcher.replaceAll(replaceString);
|
||||
// 命名规范化
|
||||
domainObjectName = JavaBeansUtil.getCamelCaseString(domainObjectName, true);
|
||||
try {
|
||||
IntrospectedTableTools.setDomainObjectName(introspectedTable, getContext(), domainObjectName);
|
||||
} catch (Exception e) {
|
||||
logger.error("itfsw:插件" + this.getClass().getTypeName() + "使用searchString、replaceString替换时异常!", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重命名类型
|
||||
*
|
||||
* @param type
|
||||
* @param override
|
||||
* @return
|
||||
*/
|
||||
private String renameJavaType(String type, String override, String suffix) {
|
||||
int lastDot = type.lastIndexOf(".");
|
||||
return type.substring(0, lastDot) + "." + override + suffix;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重命名类型
|
||||
*
|
||||
* @param type
|
||||
* @param searchString
|
||||
* @param replaceString
|
||||
* @return
|
||||
*/
|
||||
private String renameJavaType(String type, String searchString, String replaceString, String suffix) {
|
||||
int lastDot = type.lastIndexOf(".");
|
||||
String shortName = type.substring(lastDot + 1, type.length() - 1);
|
||||
|
||||
Pattern pattern = Pattern.compile(searchString);
|
||||
Matcher matcher = pattern.matcher(shortName);
|
||||
|
||||
return type.substring(0, lastDot) + "." + matcher.replaceAll(replaceString) + suffix;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright (c) 2017.
|
||||
*
|
||||
* 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.itfsw.mybatis.generator.plugins.utils;
|
||||
|
||||
import com.itfsw.mybatis.generator.plugins.ExampleTargetPlugin;
|
||||
import org.mybatis.generator.api.FullyQualifiedTable;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.config.Context;
|
||||
import org.mybatis.generator.config.JavaModelGeneratorConfiguration;
|
||||
import org.mybatis.generator.config.PluginConfiguration;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
* IntrospectedTable 的一些拓展增强
|
||||
* ---------------------------------------------------------------------------
|
||||
* @author: hewei
|
||||
* @time:2017/6/13 13:48
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
public class IntrospectedTableTools {
|
||||
|
||||
/**
|
||||
* 设置DomainObjectName和MapperName
|
||||
*
|
||||
* @param introspectedTable
|
||||
* @param context
|
||||
* @param domainObjectName
|
||||
*/
|
||||
public static void setDomainObjectName(IntrospectedTable introspectedTable, Context context, String domainObjectName) throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
|
||||
// 配置信息(没啥用)
|
||||
introspectedTable.getTableConfiguration().setDomainObjectName(domainObjectName);
|
||||
|
||||
// FullyQualifiedTable修正
|
||||
Field domainObjectNameField = FullyQualifiedTable.class.getDeclaredField("domainObjectName");
|
||||
domainObjectNameField.setAccessible(true);
|
||||
domainObjectNameField.set(introspectedTable.getFullyQualifiedTable(), domainObjectName);
|
||||
|
||||
// 重新修正introspectedTable属性信息
|
||||
Method calculateJavaClientAttributes = IntrospectedTable.class.getDeclaredMethod("calculateJavaClientAttributes");
|
||||
calculateJavaClientAttributes.setAccessible(true);
|
||||
calculateJavaClientAttributes.invoke(introspectedTable);
|
||||
|
||||
Method calculateModelAttributes = IntrospectedTable.class.getDeclaredMethod("calculateModelAttributes");
|
||||
calculateModelAttributes.setAccessible(true);
|
||||
calculateModelAttributes.invoke(introspectedTable);
|
||||
|
||||
Method calculateXmlAttributes = IntrospectedTable.class.getDeclaredMethod("calculateXmlAttributes");
|
||||
calculateXmlAttributes.setAccessible(true);
|
||||
calculateXmlAttributes.invoke(introspectedTable);
|
||||
|
||||
// 注意!! 如果配置了ExampleTargetPlugin插件,要修正Example 位置
|
||||
PluginConfiguration configuration = PluginTools.getPluginConfiguration(ExampleTargetPlugin.class, context);
|
||||
if (configuration != null && configuration.getProperty(ExampleTargetPlugin.TARGET_PACKAGE_KEY) != null){
|
||||
String exampleType = introspectedTable.getExampleType();
|
||||
// 修改包名
|
||||
JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = context.getJavaModelGeneratorConfiguration();
|
||||
String targetPackage = javaModelGeneratorConfiguration.getTargetPackage();
|
||||
String newExampleType = exampleType.replace(targetPackage, configuration.getProperty(ExampleTargetPlugin.TARGET_PACKAGE_KEY));
|
||||
|
||||
introspectedTable.setExampleType(newExampleType);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user