Commit 46793b36 authored by hewei's avatar hewei

新增注释插件

parent cc043ac3
/*
* 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;
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
import java.util.Properties;
/**
* ---------------------------------------------------------------------------
* 评论插件
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2017/6/8 11:21
* ---------------------------------------------------------------------------
*/
public class CommentPlugin extends BasePlugin {
public static final String PRE_TEMPLATE = "template"; // 模板 property
/**
* 插件具体实现查看BasePlugin
* @param properties
*/
@Override
public void setProperties(Properties properties) {
super.setProperties(properties);
}
}
......@@ -16,14 +16,18 @@
package com.itfsw.mybatis.generator.plugins.utils;
import com.itfsw.mybatis.generator.plugins.CommentPlugin;
import com.itfsw.mybatis.generator.plugins.utils.enhanced.DefaultCommentGenerator;
import com.itfsw.mybatis.generator.plugins.utils.enhanced.TemplateCommentGenerator;
import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.PluginConfiguration;
import org.mybatis.generator.internal.util.StringUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Field;
import java.util.List;
/**
......@@ -49,13 +53,34 @@ public class BasePlugin extends PluginAdapter {
super.setContext(context);
// 配置插件使用的模板引擎
if (context.getCommentGenerator() instanceof org.mybatis.generator.internal.DefaultCommentGenerator){
PluginConfiguration cfg = PluginTools.getPluginConfiguration(CommentPlugin.class, context);
if (cfg == null || cfg.getProperty(CommentPlugin.PRE_TEMPLATE) == null){
if (context.getCommentGeneratorConfiguration().getConfigurationType().equals("DEFAULT")){
// 使用默认模板引擎
commentGenerator = new DefaultCommentGenerator();
} else {
// 用户自定义
commentGenerator = context.getCommentGenerator();
}
} else {
TemplateCommentGenerator templateCommentGenerator = new TemplateCommentGenerator(cfg.getProperty(CommentPlugin.PRE_TEMPLATE));
// ITFSW 插件使用的注释生成器
commentGenerator = templateCommentGenerator;
// 修正系统插件
try {
// 先执行一次生成CommentGenerator操作,然后再替换
context.getCommentGenerator();
Field field = Context.class.getDeclaredField("commentGenerator");
field.setAccessible(true);
field.set(context, templateCommentGenerator);
} catch (Exception e) {
logger.error("反射异常",e);
}
}
}
/**
......
......@@ -81,4 +81,19 @@ public class PluginTools {
}
return new ArrayList<>();
}
/**
* 获取插件配置
*
* @param plugin 插件
* @param ctx 上下文
* @return
*/
public static PluginConfiguration getPluginConfiguration(Class plugin, Context ctx){
int index = getPluginIndex(plugin, ctx);
if (index > -1){
return getConfigPlugins(ctx).get(index);
}
return null;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment