Commit ed19ffa9 authored by hewei's avatar hewei

Example 目标包修改插件

parent b564e9d9
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
* MySQL分页插件 * MySQL分页插件
* 数据Model链式构建插件 * 数据Model链式构建插件
* Example Criteria 增强插件 * Example Criteria 增强插件
* Example 目标包修改插件
--------------------------------------- ---------------------------------------
Maven引用: Maven引用:
...@@ -159,4 +160,14 @@ public class Test { ...@@ -159,4 +160,14 @@ public class Test {
.example(); .example();
} }
} }
```
### 5. Example 目标包修改插件
Mybatis Generator 插件默认把Model类和Example类都生成到一个包下,这样该包下类就会很多不方便区分,该插件目的就是把Example类独立到一个新包下,方便查看。
插件:
```xml
<!-- Example 目标包修改插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.ExampleTargetPlugin">
<!-- 修改Example类生成到目标包下 -->
<property name="targetPackage" value="com.itfsw.mybatis.generator.dao.example"/>
</plugin>
``` ```
\ No newline at end of file
/*
*
* * 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<String> 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);
}
}
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