Compare commits

...
8 Commits
Author SHA1 Message Date
hewei 672b704452 [maven-release-plugin] prepare release V1.0.10 2017-06-07 17:31:59 +08:00
hewei 27b7734da1 v1.0.10文档 2017-06-07 17:26:57 +08:00
hewei 46069323ed v1.0.10文档 2017-06-06 18:36:47 +08:00
hewei 2e697df7d9 插件重命名 2017-06-06 17:55:06 +08:00
hewei 719e862712 插件重命名 2017-06-06 17:54:47 +08:00
hewei 0b63df6f7a 插件重命名 2017-06-06 17:50:42 +08:00
hewei efc26b78b4 新增: table 重命名插件 2017-06-06 17:50:24 +08:00
hewei 88d480e2ca [maven-release-plugin] prepare for next development iteration 2017-05-19 18:49:13 +08:00
4 changed files with 264 additions and 16 deletions
+54 -4
View File
@@ -14,7 +14,8 @@
* [数据Model属性对应Column获取插件(ModelColumnPlugin)](#8-数据model属性对应column获取插件)
* [存在即更新插件(UpsertPlugin)](#9-存在即更新插件)
* [Selective选择插入更新增强插件(SelectiveEnhancedPlugin)](#10-selective选择插入更新增强插件)
* [Table增加前缀插件(TableSuffixPlugin)](#11-table增加前缀插件)
* [Table增加前缀插件(TablePrefixPlugin)](#11-table增加前缀插件)
* [Table重命名插件(TableRenamePlugin)](#12-table重命名插件)
---------------------------------------
Maven引用:
@@ -22,7 +23,7 @@ Maven引用:
<dependency>
<groupId>com.itfsw</groupId>
<artifactId>mybatis-generator-plugin</artifactId>
<version>1.0.9</version>
<version>1.0.10</version>
</dependency>
```
---------------------------------------
@@ -554,14 +555,15 @@ public class Test {
### 11. Table增加前缀插件
项目中有时会遇到配置多数据源对应多业务的情况,这种情况下可能会出现不同数据源出现重复表名,造成异常冲突。
该插件允许为表增加前缀,改变最终生成的Model、Mapper、Example类名以及xml名。
warning: 请原谅我蹩脚的英文水平,一直没发现插件名称错了,1.0.9版本错误命名为TableSuffixPlugin,属性为suffix请新版本用户对应修正!
插件:
```xml
<!-- Table增加前缀插件 -->
<xml>
<plugin type="com.itfsw.mybatis.generator.plugins.TableSuffixPlugin">
<plugin type="com.itfsw.mybatis.generator.plugins.TablePrefixPlugin">
<!-- 这里配置的是全局表前缀,当然在table中配置的值会覆盖该全局配置 -->
<property name="suffix" value="Cm"/>
<property name="prefix" value="Cm"/>
</plugin>
<table tableName="tb">
@@ -581,4 +583,52 @@ public class Test {
// xml文件名: TbMapper.xml -> Db1TbMapper.xml
}
}
```
### 12. Table重命名插件
插件由来:
记得才开始工作时某个隔壁项目组的坑爹项目,由于某些特定的原因数据库设计表名为t1~tn,字段名为f1~fn。
这种情况下如何利用Mybatis Generator生成可读的代码呢,字段可以用columnOverride来替换,Model、Mapper等则需要使用domainObjectName+mapperName来实现方便辨识的代码。
>该插件解决:domainObjectName+mapperName?好吧我想简化一下,所以直接使用tableOverride(仿照columnOverride)来实现便于配置的理解。
某些DBA喜欢在数据库设计时使用t_、f_这种类似的设计([[issues#4]](https://github.com/itfsw/mybatis-generator-plugin/issues/4)),
这种情况下我们就希望能有类似[columnRenamingRule](http://www.mybatis.org/generator/configreference/columnRenamingRule.html)这种重命名插件来修正最终生成的Model、Mapper等命名。
>该插件解决:使用正则替换table生成的Model、Example、Mapper等命名。
warning:和插件[Table增加前缀插件](#11-table增加前缀插件)联合使用时,需注意顺序问题。
插件:
```xml
<!-- Table重命名插件 -->
<xml>
<plugin type="com.itfsw.mybatis.generator.plugins.TablePrefixPlugin">
<!-- 可根据具体需求确定是否配置 -->
<property name="searchString" value="^T"/>
<property name="replaceString" value=""/>
</plugin>
<table tableName="tb">
<!-- 这个优先级最高,会忽略searchString、replaceString的配置 -->
<property name="tableOverride" value="TestDb"/>
</table>
</xml>
```
使用:
```java
public class Test {
public static void main(String[] args) {
// 1. 使用searchString和replaceString时,和Mybatis Generator一样使用的是java.util.regex.Matcher.replaceAll去进行正则替换
// 表: T_USER
// Model类名: TUser -> User
// Mapper类名: TUserMapper -> UserMapper
// Example类名: TUserExample -> UserExample
// xml文件名: TUserMapper.xml -> UserMapper.xml
// 2. 使用表tableOverride时,该配置优先级最高
// 表: T_BOOK
// Model类名: TBook -> TestDb
// Mapper类名: TBookMapper -> TestDbMapper
// Example类名: TBookExample -> TestDbExample
// xml文件名: TBookMapper.xml -> TestDbMapper.xml
}
}
```
+2 -2
View File
@@ -3,7 +3,7 @@
<groupId>com.itfsw</groupId>
<artifactId>mybatis-generator-plugin</artifactId>
<version>1.0.9</version>
<version>1.0.10</version>
<packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name>
@@ -175,7 +175,7 @@
<connection>scm:git:https://github.com/itfsw/mybatis-generator-plugin.git</connection>
<developerConnection>scm:git:https://github.com/itfsw/mybatis-generator-plugin.git</developerConnection>
<url>https://github.com/itfsw/mybatis-generator-plugin</url>
<tag>V1.0.9</tag>
<tag>V1.0.10</tag>
</scm>
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++distribution++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
@@ -27,10 +27,10 @@ import org.mybatis.generator.api.IntrospectedTable;
* @time:2017/5/18 13:54
* ---------------------------------------------------------------------------
*/
public class TableSuffixPlugin extends BasePlugin {
public class TablePrefixPlugin extends BasePlugin {
public static final String PRE_SUFFIX = "suffix"; // 前缀 property
private String suffix; // 前缀
public static final String PRE_PREFIX = "prefix"; // 前缀 property
private String prefix; // 前缀
/**
* 初始化阶段
@@ -42,13 +42,13 @@ public class TableSuffixPlugin extends BasePlugin {
@Override
public void initialized(IntrospectedTable introspectedTable) {
// 1. 首先获取全局配置
this.suffix = getProperties().getProperty(PRE_SUFFIX);
this.prefix = getProperties().getProperty(PRE_PREFIX);
// 2. 获取每个table 具体的
if (introspectedTable.getTableConfigurationProperty(PRE_SUFFIX) != null){
this.suffix = introspectedTable.getTableConfigurationProperty(PRE_SUFFIX);
if (introspectedTable.getTableConfigurationProperty(PRE_PREFIX) != null){
this.prefix = introspectedTable.getTableConfigurationProperty(PRE_PREFIX);
}
// 3. 判断是否配置了前缀
if (this.suffix != null){
if (this.prefix != null){
// 3.1. 为Model增加前缀
if (introspectedTable.getBaseRecordType() != null){
introspectedTable.setBaseRecordType(this.renameJavaType(introspectedTable.getBaseRecordType()));
@@ -74,9 +74,19 @@ public class TableSuffixPlugin extends BasePlugin {
introspectedTable.setExampleType(this.renameJavaType(introspectedTable.getExampleType()));
}
// 3.6. 修正xml文件前缀
// 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.suffix + introspectedTable.getMyBatis3XmlMapperFileName());
introspectedTable.setMyBatis3XmlMapperFileName(this.prefix + introspectedTable.getMyBatis3XmlMapperFileName());
}
}
}
@@ -89,6 +99,6 @@ public class TableSuffixPlugin extends BasePlugin {
*/
private String renameJavaType(String type){
int lastDot = type.lastIndexOf(".") + 1;
return type.substring(0, lastDot) + this.suffix + type.substring(lastDot);
return type.substring(0, lastDot) + this.prefix + type.substring(lastDot);
}
}
@@ -0,0 +1,188 @@
/*
* 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 org.mybatis.generator.api.IntrospectedTable;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* ---------------------------------------------------------------------------
* table 重命名插件
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2017/6/6 15:18
* ---------------------------------------------------------------------------
*/
public class TableRenamePlugin extends BasePlugin {
public static final String PRE_SEARCH_STRING = "searchString"; // 查找 property
public static final String PRE_REPLACE_STRING = "replaceString"; // 替换 property
public static final String PRE_TABLE_OVERRIDE = "tableOverride"; // table 重命名 property
/**
* {@inheritDoc}
*/
@Override
public boolean validate(List<String> warnings) {
// 如果配置了searchString 或者 replaceString,二者不允许单独存在
if ((getProperties().getProperty(PRE_SEARCH_STRING) == null && getProperties().getProperty(PRE_REPLACE_STRING) != null)
|| (getProperties().getProperty(PRE_SEARCH_STRING) != null && getProperties().getProperty(PRE_REPLACE_STRING) == null)) {
logger.warn("itfsw:插件" + this.getClass().getTypeName() + "插件的searchString、replaceString属性需配合使用,不能单独存在!");
return false;
}
return super.validate(warnings);
}
/**
* 初始化阶段
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* @param introspectedTable
* @return
*/
@Override
public void initialized(IntrospectedTable introspectedTable) {
// 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");
}
} 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);
}
}
}
/**
* 重命名类型
*
* @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);
logger.warn("===============================================================");
logger.warn(shortName);
logger.warn(type.substring(0, lastDot) + "." + matcher.replaceAll(replaceString) + suffix);
return type.substring(0, lastDot) + "." + matcher.replaceAll(replaceString) + suffix;
}
}