Commit abc2ef16 authored by hewei's avatar hewei

TableRenameConfigurationPlugin

parent 2878b982
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
* [查询结果选择性返回插件(SelectSelectivePlugin)](#15-查询结果选择性返回插件) * [查询结果选择性返回插件(SelectSelectivePlugin)](#15-查询结果选择性返回插件)
* [~~官方ConstructorBased配置BUG临时修正插件(ConstructorBasedBugFixPlugin)~~](#16-官方constructorbased配置bug临时修正插件) * [~~官方ConstructorBased配置BUG临时修正插件(ConstructorBasedBugFixPlugin)~~](#16-官方constructorbased配置bug临时修正插件)
* [乐观锁插件(OptimisticLockerPlugin)](#17-乐观锁插件) * [乐观锁插件(OptimisticLockerPlugin)](#17-乐观锁插件)
* [表重命名配置插件(TableConfigurationPlugin)](#18-表重命名配置插件)
--------------------------------------- ---------------------------------------
Maven引用: Maven引用:
...@@ -654,7 +655,7 @@ public class Test { ...@@ -654,7 +655,7 @@ public class Test {
项目中有时会遇到配置多数据源对应多业务的情况,这种情况下可能会出现不同数据源出现重复表名,造成异常冲突。 项目中有时会遇到配置多数据源对应多业务的情况,这种情况下可能会出现不同数据源出现重复表名,造成异常冲突。
该插件允许为表增加前缀,改变最终生成的Model、Mapper、Example类名以及xml名。 该插件允许为表增加前缀,改变最终生成的Model、Mapper、Example类名以及xml名。
>warning: 使用[Table重命名插件](12-table重命名插件)可以实现相同功能! >warning: 使用[Table重命名插件](12-table重命名插件)可以实现相同功能!
>warning: 官方最新版本中已提供domainObjectRenamingRule支持,以后请尽量使用官方支持! >warning: 官方最新版本中已提供domainObjectRenamingRule支持(可以配合[表重命名配置插件](#18-表重命名配置插件)进行全局配置),以后请尽量使用官方支持!
```xml ```xml
<table tableName="tb"> <table tableName="tb">
<domainObjectRenamingRule searchString="^" replaceString="DB1" /> <domainObjectRenamingRule searchString="^" replaceString="DB1" />
...@@ -703,7 +704,7 @@ public class Test { ...@@ -703,7 +704,7 @@ public class Test {
<property name="searchString" value="^"/> <property name="searchString" value="^"/>
<property name="replaceString" value="DB1"/> <property name="replaceString" value="DB1"/>
``` ```
>warning: 官方最新版本中已提供domainObjectRenamingRule支持,以后请尽量使用官方支持! >warning: 官方最新版本中已提供domainObjectRenamingRule支持(可以配合[表重命名配置插件](#18-表重命名配置插件)进行全局配置),以后请尽量使用官方支持!
```xml ```xml
<table tableName="tb"> <table tableName="tb">
<domainObjectRenamingRule searchString="^T" replaceString="" /> <domainObjectRenamingRule searchString="^T" replaceString="" />
...@@ -1216,3 +1217,23 @@ public class Test { ...@@ -1216,3 +1217,23 @@ public class Test {
} }
} }
``` ```
### 18. 表重命名配置插件
官方提供了domainObjectRenamingRule(官方最新版本已提供)、columnRenamingRule分别进行生成的表名称和对应表字段的重命名支持,但是它需要每个表单独进行配置,对于常用的如表附带前缀“t_”、字段前缀“f_”这种全局性替换会比较麻烦。
该插件提供了一种全局替换机制,当表没有单独指定domainObjectRenamingRule、columnRenamingRule时采用全局性配置。同时该插件会修复官方domainObjectRenamingRule的bug(没有进行正确的首字母大写)。
- 全局domainObjectRenamingRule
```xml
<xml>
<!-- 表重命名配置插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.TableRenameConfigurationPlugin">
<property name="domainObjectRenamingRule.searchString" value="^T"/>
<property name="domainObjectRenamingRule.replaceString" value=""/>
</plugin>
<table tableName="tb">
<!-- 这里可以单独表配置,覆盖全局配置 -->
<property name="customizedNextVersion" value="false"/>
<!-- 指定版本列 -->
<property name="versionColumn" value="version"/>
</table>
</xml>
```
\ No newline at end of file
...@@ -46,7 +46,7 @@ import static org.mybatis.generator.internal.util.StringUtility.stringHasValue; ...@@ -46,7 +46,7 @@ import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
* @time:2018/5/21 11:23 * @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_SEARCH_STRING = "domainObjectRenamingRule.searchString"; // 查找 property
public static final String PRO_TABLE_REPLACE_STRING = "domainObjectRenamingRule.replaceString"; // 替换 property public static final String PRO_TABLE_REPLACE_STRING = "domainObjectRenamingRule.replaceString"; // 替换 property
public static final String PRO_TABLE_REPLACE_DISABLE = "domainObjectRenamingRule.disable"; // 替换 property public static final String PRO_TABLE_REPLACE_DISABLE = "domainObjectRenamingRule.disable"; // 替换 property
...@@ -164,6 +164,13 @@ public class TableConfigurationPlugin extends BasePlugin implements ITableConfig ...@@ -164,6 +164,13 @@ public class TableConfigurationPlugin extends BasePlugin implements ITableConfig
sb.append(this.clientSuffix); sb.append(this.clientSuffix);
} }
introspectedTable.setMyBatis3FallbackSqlMapNamespace(sb.toString()); 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 // 2. example
if (this.exampleSuffix != null) { if (this.exampleSuffix != null) {
......
...@@ -37,10 +37,10 @@ import java.util.List; ...@@ -37,10 +37,10 @@ import java.util.List;
* @time:2018/5/22 13:22 * @time:2018/5/22 13:22
* --------------------------------------------------------------------------- * ---------------------------------------------------------------------------
*/ */
public class TableConfigurationPluginTest { public class TableRenameConfigurationPluginTest {
@BeforeClass @BeforeClass
public static void init() throws Exception { 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 { ...@@ -48,9 +48,19 @@ public class TableConfigurationPluginTest {
*/ */
@Test @Test
public void testDomainObjectRenamingRule() throws Exception { public void testDomainObjectRenamingRule() throws Exception {
// 规则 ^T 替换成 Test // 规则 ^T 替换成空,也就是去掉前缀
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/TableConfigurationPlugin/mybatis-generator-with-domainObjectRenamingRule.xml"); MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/TableRenameConfigurationPlugin/mybatis-generator-with-domainObjectRenamingRule-relacePrefix.xml");
MyBatisGenerator myBatisGenerator = tool.generate(); 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()){ for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()){
String name = file.getCompilationUnit().getType().getShortName(); String name = file.getCompilationUnit().getType().getShortName();
if (!(name.matches("Testb.*") || name.matches("TbBlobs.*"))){ if (!(name.matches("Testb.*") || name.matches("TbBlobs.*"))){
...@@ -58,7 +68,7 @@ public class TableConfigurationPluginTest { ...@@ -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 @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TestbMapper"))); ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TestbMapper")));
...@@ -83,7 +93,7 @@ public class TableConfigurationPluginTest { ...@@ -83,7 +93,7 @@ public class TableConfigurationPluginTest {
@Test @Test
public void testColumnRenamingRule() throws Exception { public void testColumnRenamingRule() throws Exception {
// 规则 ^T 替换成 Test // 规则 ^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(); MyBatisGenerator myBatisGenerator = tool.generate();
for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()){ for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()){
if (file.getFileName().equals("Tb.java")){ if (file.getFileName().equals("Tb.java")){
...@@ -107,7 +117,7 @@ public class TableConfigurationPluginTest { ...@@ -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 @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper"))); ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
...@@ -142,7 +152,7 @@ public class TableConfigurationPluginTest { ...@@ -142,7 +152,7 @@ public class TableConfigurationPluginTest {
*/ */
@Test @Test
public void testClientSuffix() throws Exception { 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(); MyBatisGenerator myBatisGenerator = tool.generate();
boolean find = false; boolean find = false;
...@@ -164,7 +174,7 @@ public class TableConfigurationPluginTest { ...@@ -164,7 +174,7 @@ public class TableConfigurationPluginTest {
Assert.assertTrue(find); Assert.assertTrue(find);
// 执行一条语句确认其可用 // 执行一条语句确认其可用
tool.generate(() -> DBHelper.resetDB("scripts/TableConfigurationPlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/TableRenameConfigurationPlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbDao = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbDao"))); ObjectUtil tbDao = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbDao")));
...@@ -188,7 +198,7 @@ public class TableConfigurationPluginTest { ...@@ -188,7 +198,7 @@ public class TableConfigurationPluginTest {
*/ */
@Test @Test
public void testExampleSuffix() throws Exception { 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(); MyBatisGenerator myBatisGenerator = tool.generate();
boolean find = false; boolean find = false;
...@@ -200,7 +210,7 @@ public class TableConfigurationPluginTest { ...@@ -200,7 +210,7 @@ public class TableConfigurationPluginTest {
} }
Assert.assertTrue(find); Assert.assertTrue(find);
// 执行一条语句确认其可用 // 执行一条语句确认其可用
tool.generate(() -> DBHelper.resetDB("scripts/TableConfigurationPlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/TableRenameConfigurationPlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper"))); ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
...@@ -224,7 +234,7 @@ public class TableConfigurationPluginTest { ...@@ -224,7 +234,7 @@ public class TableConfigurationPluginTest {
*/ */
@Test @Test
public void testModelSuffix() throws Exception { 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(); MyBatisGenerator myBatisGenerator = tool.generate();
boolean find = false; boolean find = false;
...@@ -236,7 +246,7 @@ public class TableConfigurationPluginTest { ...@@ -236,7 +246,7 @@ public class TableConfigurationPluginTest {
} }
Assert.assertTrue(find); Assert.assertTrue(find);
// 执行一条语句确认其可用 // 执行一条语句确认其可用
tool.generate(() -> DBHelper.resetDB("scripts/TableConfigurationPlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/TableRenameConfigurationPlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper"))); ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
<!--导入属性配置 --> <!--导入属性配置 -->
<context id="default" targetRuntime="MyBatis3"> <context id="default" targetRuntime="MyBatis3">
<!-- 插件 --> <!-- 插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.TableConfigurationPlugin"> <plugin type="com.itfsw.mybatis.generator.plugins.TableRenameConfigurationPlugin">
<property name="clientSuffix" value="Dao"/> <property name="clientSuffix" value="Dao"/>
</plugin> </plugin>
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
<!--导入属性配置 --> <!--导入属性配置 -->
<context id="default" targetRuntime="MyBatis3"> <context id="default" targetRuntime="MyBatis3">
<!-- 插件 --> <!-- 插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.TableConfigurationPlugin"> <plugin type="com.itfsw.mybatis.generator.plugins.TableRenameConfigurationPlugin">
<property name="columnRenamingRule.searchString" value="^inc"/> <property name="columnRenamingRule.searchString" value="^inc"/>
<property name="columnRenamingRule.replaceString" value="Increment"/> <property name="columnRenamingRule.replaceString" value="Increment"/>
</plugin> </plugin>
......
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2018.
~
~ 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.
-->
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<properties resource="db.properties"/>
<!--导入属性配置 -->
<context id="default" targetRuntime="MyBatis3">
<!-- 插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.TableRenameConfigurationPlugin">
<property name="domainObjectRenamingRule.searchString" value="^T"/>
<property name="domainObjectRenamingRule.replaceString" value=""/>
</plugin>
<!--jdbc的数据库连接 -->
<jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}" />
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator targetPackage="" targetProject="">
<!-- 是否对model添加 构造函数 -->
<property name="constructorBased" value="true"/>
<!-- 给Model添加一个父类 -->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator targetPackage="" targetProject="" />
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
<javaClientGenerator targetPackage="" targetProject="" type="XMLMAPPER"/>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<table tableName="tb">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
</context>
</generatorConfiguration>
\ No newline at end of file
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
<!--导入属性配置 --> <!--导入属性配置 -->
<context id="default" targetRuntime="MyBatis3"> <context id="default" targetRuntime="MyBatis3">
<!-- 插件 --> <!-- 插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.TableConfigurationPlugin"> <plugin type="com.itfsw.mybatis.generator.plugins.TableRenameConfigurationPlugin">
<property name="domainObjectRenamingRule.searchString" value="^T"/> <property name="domainObjectRenamingRule.searchString" value="^T"/>
<property name="domainObjectRenamingRule.replaceString" value="Test"/> <property name="domainObjectRenamingRule.replaceString" value="Test"/>
</plugin> </plugin>
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
<!--导入属性配置 --> <!--导入属性配置 -->
<context id="default" targetRuntime="MyBatis3"> <context id="default" targetRuntime="MyBatis3">
<!-- 插件 --> <!-- 插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.TableConfigurationPlugin"> <plugin type="com.itfsw.mybatis.generator.plugins.TableRenameConfigurationPlugin">
<property name="exampleSuffix" value="Query"/> <property name="exampleSuffix" value="Query"/>
</plugin> </plugin>
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
<!--导入属性配置 --> <!--导入属性配置 -->
<context id="default" targetRuntime="MyBatis3"> <context id="default" targetRuntime="MyBatis3">
<!-- 插件 --> <!-- 插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.TableConfigurationPlugin"> <plugin type="com.itfsw.mybatis.generator.plugins.TableRenameConfigurationPlugin">
<property name="modelSuffix" value="Entity"/> <property name="modelSuffix" value="Entity"/>
</plugin> </plugin>
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
<!--导入属性配置 --> <!--导入属性配置 -->
<context id="default" targetRuntime="MyBatis3"> <context id="default" targetRuntime="MyBatis3">
<!-- 插件 --> <!-- 插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.TableConfigurationPlugin"> <plugin type="com.itfsw.mybatis.generator.plugins.TableRenameConfigurationPlugin">
<property name="searchString" value="^"/> <property name="searchString" value="^"/>
<property name="replaceString" value="DB1"/> <property name="replaceString" value="DB1"/>
<property name="suffixForMapper" value="Dao"/> <property name="suffixForMapper" value="Dao"/>
......
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