diff --git a/README.md b/README.md index ccfca36..e32433d 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ * MySQL分页插件 * 数据Model链式构建插件 * Example Criteria 增强插件 +* Example 目标包修改插件 --------------------------------------- Maven引用: @@ -159,4 +160,14 @@ public class Test { .example(); } } +``` +### 5. Example 目标包修改插件 +Mybatis Generator 插件默认把Model类和Example类都生成到一个包下,这样该包下类就会很多不方便区分,该插件目的就是把Example类独立到一个新包下,方便查看。 +插件: +```xml + + + + + ``` \ No newline at end of file diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/ExampleTargetPlugin.java b/src/main/java/com/itfsw/mybatis/generator/plugins/ExampleTargetPlugin.java new file mode 100644 index 0000000..c1ab8da --- /dev/null +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/ExampleTargetPlugin.java @@ -0,0 +1,80 @@ +/* + * + * * Copyright (c) 2014. + * * + * * 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 org.mybatis.generator.api.IntrospectedTable; +import org.mybatis.generator.api.PluginAdapter; +import org.mybatis.generator.config.Context; +import org.mybatis.generator.config.JavaModelGeneratorConfiguration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.Properties; + +/** + * --------------------------------------------------------------------------- + * Example类生成位置修改 + * --------------------------------------------------------------------------- + * @author: hewei + * @time:2017/1/12 12:36 + * --------------------------------------------------------------------------- + */ +public class ExampleTargetPlugin extends PluginAdapter { + public static final String TARGET_PACKAGE_KEY = "targetPackage"; // 配置targetPackage名 + private static final Logger logger = LoggerFactory.getLogger(CriteriaBuilderPlugin.class); + + private String targetPackage; // 目标包 + + /** + * {@inheritDoc} + */ + @Override + public boolean validate(List warnings) { + // 获取配置的目标package + Properties properties = getProperties(); + this.targetPackage = properties.getProperty(TARGET_PACKAGE_KEY); + if (this.targetPackage == null){ + logger.warn("请配置com.itfsw.mybatis.generator.plugins.ExampleTargetPlugin插件的目标包名(targetPackage)!"); + return false; + } + return true; + } + + /** + * 初始化阶段 + * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html + * + * @param introspectedTable + */ + @Override + public void initialized(IntrospectedTable introspectedTable) { + String exampleType = introspectedTable.getExampleType(); + // 修改包名 + Context context = getContext(); + JavaModelGeneratorConfiguration configuration = context.getJavaModelGeneratorConfiguration(); + String targetPackage = configuration.getTargetPackage(); + String newExampleType = exampleType.replace(targetPackage, this.targetPackage); + + introspectedTable.setExampleType(newExampleType); + + logger.debug("itfsw:修改"+exampleType+"的包到"+this.targetPackage); + } + +}