diff --git a/README.md b/README.md
index 9ff87b5..8ae5270 100644
--- a/README.md
+++ b/README.md
@@ -23,6 +23,7 @@
* [查询结果选择性返回插件(SelectSelectivePlugin)](#15-查询结果选择性返回插件)
* [~~官方ConstructorBased配置BUG临时修正插件(ConstructorBasedBugFixPlugin)~~](#16-官方constructorbased配置bug临时修正插件)
* [乐观锁插件(OptimisticLockerPlugin)](#17-乐观锁插件)
+* [表重命名配置插件(TableConfigurationPlugin)](#18-表重命名配置插件)
---------------------------------------
Maven引用:
@@ -654,7 +655,7 @@ public class Test {
项目中有时会遇到配置多数据源对应多业务的情况,这种情况下可能会出现不同数据源出现重复表名,造成异常冲突。
该插件允许为表增加前缀,改变最终生成的Model、Mapper、Example类名以及xml名。
>warning: 使用[Table重命名插件](12-table重命名插件)可以实现相同功能!
->warning: 官方最新版本中已提供domainObjectRenamingRule支持,以后请尽量使用官方支持!
+>warning: 官方最新版本中已提供domainObjectRenamingRule支持(可以配合[表重命名配置插件](#18-表重命名配置插件)进行全局配置),以后请尽量使用官方支持!
```xml
@@ -703,7 +704,7 @@ public class Test {
```
->warning: 官方最新版本中已提供domainObjectRenamingRule支持,以后请尽量使用官方支持!
+>warning: 官方最新版本中已提供domainObjectRenamingRule支持(可以配合[表重命名配置插件](#18-表重命名配置插件)进行全局配置),以后请尽量使用官方支持!
```xml
@@ -1216,3 +1217,23 @@ public class Test {
}
}
```
+### 18. 表重命名配置插件
+官方提供了domainObjectRenamingRule(官方最新版本已提供)、columnRenamingRule分别进行生成的表名称和对应表字段的重命名支持,但是它需要每个表单独进行配置,对于常用的如表附带前缀“t_”、字段前缀“f_”这种全局性替换会比较麻烦。
+该插件提供了一种全局替换机制,当表没有单独指定domainObjectRenamingRule、columnRenamingRule时采用全局性配置。同时该插件会修复官方domainObjectRenamingRule的bug(没有进行正确的首字母大写)。
+- 全局domainObjectRenamingRule
+```xml
+
+
+
+
+
+
+
+
+
+```
\ No newline at end of file
diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/TableConfigurationPlugin.java b/src/main/java/com/itfsw/mybatis/generator/plugins/TableRenameConfigurationPlugin.java
similarity index 96%
rename from src/main/java/com/itfsw/mybatis/generator/plugins/TableConfigurationPlugin.java
rename to src/main/java/com/itfsw/mybatis/generator/plugins/TableRenameConfigurationPlugin.java
index 1fb20f4..4d35aec 100644
--- a/src/main/java/com/itfsw/mybatis/generator/plugins/TableConfigurationPlugin.java
+++ b/src/main/java/com/itfsw/mybatis/generator/plugins/TableRenameConfigurationPlugin.java
@@ -46,7 +46,7 @@ import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
* @time:2018/5/21 11:23
* ---------------------------------------------------------------------------
*/
-public class TableConfigurationPlugin extends BasePlugin implements ITableConfigurationHook {
+public class TableRenameConfigurationPlugin extends BasePlugin implements ITableConfigurationHook {
public static final String PRO_TABLE_SEARCH_STRING = "domainObjectRenamingRule.searchString"; // 查找 property
public static final String PRO_TABLE_REPLACE_STRING = "domainObjectRenamingRule.replaceString"; // 替换 property
public static final String PRO_TABLE_REPLACE_DISABLE = "domainObjectRenamingRule.disable"; // 替换 property
@@ -164,6 +164,13 @@ public class TableConfigurationPlugin extends BasePlugin implements ITableConfig
sb.append(this.clientSuffix);
}
introspectedTable.setMyBatis3FallbackSqlMapNamespace(sb.toString());
+ // xml file
+ sb.setLength(0);
+ sb.append(fullyQualifiedTable.getDomainObjectName());
+ sb.append(this.clientSuffix);
+ sb.append(".xml");
+
+ introspectedTable.setMyBatis3XmlMapperFileName(sb.toString());
}
// 2. example
if (this.exampleSuffix != null) {
diff --git a/src/test/java/com/itfsw/mybatis/generator/plugins/TableConfigurationPluginTest.java b/src/test/java/com/itfsw/mybatis/generator/plugins/TableRenameConfigurationPluginTest.java
similarity index 86%
rename from src/test/java/com/itfsw/mybatis/generator/plugins/TableConfigurationPluginTest.java
rename to src/test/java/com/itfsw/mybatis/generator/plugins/TableRenameConfigurationPluginTest.java
index 516e032..384d238 100644
--- a/src/test/java/com/itfsw/mybatis/generator/plugins/TableConfigurationPluginTest.java
+++ b/src/test/java/com/itfsw/mybatis/generator/plugins/TableRenameConfigurationPluginTest.java
@@ -37,10 +37,10 @@ import java.util.List;
* @time:2018/5/22 13:22
* ---------------------------------------------------------------------------
*/
-public class TableConfigurationPluginTest {
+public class TableRenameConfigurationPluginTest {
@BeforeClass
public static void init() throws Exception {
- DBHelper.createDB("scripts/TableConfigurationPlugin/init.sql");
+ DBHelper.createDB("scripts/TableRenameConfigurationPlugin/init.sql");
}
/**
@@ -48,9 +48,19 @@ public class TableConfigurationPluginTest {
*/
@Test
public void testDomainObjectRenamingRule() throws Exception {
- // 规则 ^T 替换成 Test
- MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/TableConfigurationPlugin/mybatis-generator-with-domainObjectRenamingRule.xml");
+ // 规则 ^T 替换成空,也就是去掉前缀
+ MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/TableRenameConfigurationPlugin/mybatis-generator-with-domainObjectRenamingRule-relacePrefix.xml");
MyBatisGenerator myBatisGenerator = tool.generate();
+ for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()){
+ String name = file.getCompilationUnit().getType().getShortName();
+ if (!name.matches("B.*")){
+ Assert.assertTrue(false);
+ }
+ }
+
+ // 规则 ^T 替换成 Test
+ tool = MyBatisGeneratorTool.create("scripts/TableRenameConfigurationPlugin/mybatis-generator-with-domainObjectRenamingRule.xml");
+ myBatisGenerator = tool.generate();
for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()){
String name = file.getCompilationUnit().getType().getShortName();
if (!(name.matches("Testb.*") || name.matches("TbBlobs.*"))){
@@ -58,7 +68,7 @@ public class TableConfigurationPluginTest {
}
}
// 执行一条语句确认其可用
- tool.generate(() -> DBHelper.resetDB("scripts/TableConfigurationPlugin/init.sql"), new AbstractShellCallback() {
+ tool.generate(() -> DBHelper.resetDB("scripts/TableRenameConfigurationPlugin/init.sql"), new AbstractShellCallback() {
@Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TestbMapper")));
@@ -83,7 +93,7 @@ public class TableConfigurationPluginTest {
@Test
public void testColumnRenamingRule() throws Exception {
// 规则 ^T 替换成 Test
- MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/TableConfigurationPlugin/mybatis-generator-with-columnRenamingRule.xml");
+ MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/TableRenameConfigurationPlugin/mybatis-generator-with-columnRenamingRule.xml");
MyBatisGenerator myBatisGenerator = tool.generate();
for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()){
if (file.getFileName().equals("Tb.java")){
@@ -107,7 +117,7 @@ public class TableConfigurationPluginTest {
}
// 执行一条语句确认其可用
- tool.generate(() -> DBHelper.resetDB("scripts/TableConfigurationPlugin/init.sql"), new AbstractShellCallback() {
+ tool.generate(() -> DBHelper.resetDB("scripts/TableRenameConfigurationPlugin/init.sql"), new AbstractShellCallback() {
@Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
@@ -142,7 +152,7 @@ public class TableConfigurationPluginTest {
*/
@Test
public void testClientSuffix() throws Exception {
- MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/TableConfigurationPlugin/mybatis-generator-with-clientSuffix.xml");
+ MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/TableRenameConfigurationPlugin/mybatis-generator-with-clientSuffix.xml");
MyBatisGenerator myBatisGenerator = tool.generate();
boolean find = false;
@@ -164,7 +174,7 @@ public class TableConfigurationPluginTest {
Assert.assertTrue(find);
// 执行一条语句确认其可用
- tool.generate(() -> DBHelper.resetDB("scripts/TableConfigurationPlugin/init.sql"), new AbstractShellCallback() {
+ tool.generate(() -> DBHelper.resetDB("scripts/TableRenameConfigurationPlugin/init.sql"), new AbstractShellCallback() {
@Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbDao = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbDao")));
@@ -188,7 +198,7 @@ public class TableConfigurationPluginTest {
*/
@Test
public void testExampleSuffix() throws Exception {
- MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/TableConfigurationPlugin/mybatis-generator-with-exampleSuffix.xml");
+ MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/TableRenameConfigurationPlugin/mybatis-generator-with-exampleSuffix.xml");
MyBatisGenerator myBatisGenerator = tool.generate();
boolean find = false;
@@ -200,7 +210,7 @@ public class TableConfigurationPluginTest {
}
Assert.assertTrue(find);
// 执行一条语句确认其可用
- tool.generate(() -> DBHelper.resetDB("scripts/TableConfigurationPlugin/init.sql"), new AbstractShellCallback() {
+ tool.generate(() -> DBHelper.resetDB("scripts/TableRenameConfigurationPlugin/init.sql"), new AbstractShellCallback() {
@Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
@@ -224,7 +234,7 @@ public class TableConfigurationPluginTest {
*/
@Test
public void testModelSuffix() throws Exception {
- MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/TableConfigurationPlugin/mybatis-generator-with-modelSuffix.xml");
+ MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/TableRenameConfigurationPlugin/mybatis-generator-with-modelSuffix.xml");
MyBatisGenerator myBatisGenerator = tool.generate();
boolean find = false;
@@ -236,7 +246,7 @@ public class TableConfigurationPluginTest {
}
Assert.assertTrue(find);
// 执行一条语句确认其可用
- tool.generate(() -> DBHelper.resetDB("scripts/TableConfigurationPlugin/init.sql"), new AbstractShellCallback() {
+ tool.generate(() -> DBHelper.resetDB("scripts/TableRenameConfigurationPlugin/init.sql"), new AbstractShellCallback() {
@Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
diff --git a/src/test/resources/scripts/TableConfigurationPlugin/init.sql b/src/test/resources/scripts/TableRenameConfigurationPlugin/init.sql
similarity index 100%
rename from src/test/resources/scripts/TableConfigurationPlugin/init.sql
rename to src/test/resources/scripts/TableRenameConfigurationPlugin/init.sql
diff --git a/src/test/resources/scripts/TableConfigurationPlugin/mybatis-generator-with-clientSuffix.xml b/src/test/resources/scripts/TableRenameConfigurationPlugin/mybatis-generator-with-clientSuffix.xml
similarity index 96%
rename from src/test/resources/scripts/TableConfigurationPlugin/mybatis-generator-with-clientSuffix.xml
rename to src/test/resources/scripts/TableRenameConfigurationPlugin/mybatis-generator-with-clientSuffix.xml
index 6d9ed68..fa17522 100644
--- a/src/test/resources/scripts/TableConfigurationPlugin/mybatis-generator-with-clientSuffix.xml
+++ b/src/test/resources/scripts/TableRenameConfigurationPlugin/mybatis-generator-with-clientSuffix.xml
@@ -24,7 +24,7 @@
-
+
diff --git a/src/test/resources/scripts/TableConfigurationPlugin/mybatis-generator-with-columnRenamingRule.xml b/src/test/resources/scripts/TableRenameConfigurationPlugin/mybatis-generator-with-columnRenamingRule.xml
similarity index 97%
rename from src/test/resources/scripts/TableConfigurationPlugin/mybatis-generator-with-columnRenamingRule.xml
rename to src/test/resources/scripts/TableRenameConfigurationPlugin/mybatis-generator-with-columnRenamingRule.xml
index bac4caa..0e1c7b4 100644
--- a/src/test/resources/scripts/TableConfigurationPlugin/mybatis-generator-with-columnRenamingRule.xml
+++ b/src/test/resources/scripts/TableRenameConfigurationPlugin/mybatis-generator-with-columnRenamingRule.xml
@@ -24,7 +24,7 @@
-
+
diff --git a/src/test/resources/scripts/TableRenameConfigurationPlugin/mybatis-generator-with-domainObjectRenamingRule-relacePrefix.xml b/src/test/resources/scripts/TableRenameConfigurationPlugin/mybatis-generator-with-domainObjectRenamingRule-relacePrefix.xml
new file mode 100644
index 0000000..f051997
--- /dev/null
+++ b/src/test/resources/scripts/TableRenameConfigurationPlugin/mybatis-generator-with-domainObjectRenamingRule-relacePrefix.xml
@@ -0,0 +1,56 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/test/resources/scripts/TableConfigurationPlugin/mybatis-generator-with-domainObjectRenamingRule.xml b/src/test/resources/scripts/TableRenameConfigurationPlugin/mybatis-generator-with-domainObjectRenamingRule.xml
similarity index 97%
rename from src/test/resources/scripts/TableConfigurationPlugin/mybatis-generator-with-domainObjectRenamingRule.xml
rename to src/test/resources/scripts/TableRenameConfigurationPlugin/mybatis-generator-with-domainObjectRenamingRule.xml
index 50f7b7e..ac3ae12 100644
--- a/src/test/resources/scripts/TableConfigurationPlugin/mybatis-generator-with-domainObjectRenamingRule.xml
+++ b/src/test/resources/scripts/TableRenameConfigurationPlugin/mybatis-generator-with-domainObjectRenamingRule.xml
@@ -24,7 +24,7 @@
-
+
diff --git a/src/test/resources/scripts/TableConfigurationPlugin/mybatis-generator-with-exampleSuffix.xml b/src/test/resources/scripts/TableRenameConfigurationPlugin/mybatis-generator-with-exampleSuffix.xml
similarity index 96%
rename from src/test/resources/scripts/TableConfigurationPlugin/mybatis-generator-with-exampleSuffix.xml
rename to src/test/resources/scripts/TableRenameConfigurationPlugin/mybatis-generator-with-exampleSuffix.xml
index a69b40a..0282ffd 100644
--- a/src/test/resources/scripts/TableConfigurationPlugin/mybatis-generator-with-exampleSuffix.xml
+++ b/src/test/resources/scripts/TableRenameConfigurationPlugin/mybatis-generator-with-exampleSuffix.xml
@@ -24,7 +24,7 @@
-
+
diff --git a/src/test/resources/scripts/TableConfigurationPlugin/mybatis-generator-with-modelSuffix.xml b/src/test/resources/scripts/TableRenameConfigurationPlugin/mybatis-generator-with-modelSuffix.xml
similarity index 96%
rename from src/test/resources/scripts/TableConfigurationPlugin/mybatis-generator-with-modelSuffix.xml
rename to src/test/resources/scripts/TableRenameConfigurationPlugin/mybatis-generator-with-modelSuffix.xml
index 972bfa6..be39e6b 100644
--- a/src/test/resources/scripts/TableConfigurationPlugin/mybatis-generator-with-modelSuffix.xml
+++ b/src/test/resources/scripts/TableRenameConfigurationPlugin/mybatis-generator-with-modelSuffix.xml
@@ -24,7 +24,7 @@
-
+
diff --git a/src/test/resources/scripts/TableConfigurationPlugin/mybatis-generator.xml b/src/test/resources/scripts/TableRenameConfigurationPlugin/mybatis-generator.xml
similarity index 97%
rename from src/test/resources/scripts/TableConfigurationPlugin/mybatis-generator.xml
rename to src/test/resources/scripts/TableRenameConfigurationPlugin/mybatis-generator.xml
index 8421164..b816658 100644
--- a/src/test/resources/scripts/TableConfigurationPlugin/mybatis-generator.xml
+++ b/src/test/resources/scripts/TableRenameConfigurationPlugin/mybatis-generator.xml
@@ -24,7 +24,7 @@
-
+