TableRenameConfigurationPlugin
This commit is contained in:
@@ -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
|
||||
<table tableName="tb">
|
||||
<domainObjectRenamingRule searchString="^" replaceString="DB1" />
|
||||
@@ -703,7 +704,7 @@ public class Test {
|
||||
<property name="searchString" value="^"/>
|
||||
<property name="replaceString" value="DB1"/>
|
||||
```
|
||||
>warning: 官方最新版本中已提供domainObjectRenamingRule支持,以后请尽量使用官方支持!
|
||||
>warning: 官方最新版本中已提供domainObjectRenamingRule支持(可以配合[表重命名配置插件](#18-表重命名配置插件)进行全局配置),以后请尽量使用官方支持!
|
||||
```xml
|
||||
<table tableName="tb">
|
||||
<domainObjectRenamingRule searchString="^T" replaceString="" />
|
||||
@@ -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>
|
||||
```
|
||||
+8
-1
@@ -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) {
|
||||
+23
-13
@@ -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")));
|
||||
+1
-1
@@ -24,7 +24,7 @@
|
||||
<!--导入属性配置 -->
|
||||
<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"/>
|
||||
</plugin>
|
||||
|
||||
+1
-1
@@ -24,7 +24,7 @@
|
||||
<!--导入属性配置 -->
|
||||
<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.replaceString" value="Increment"/>
|
||||
</plugin>
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
<?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>
|
||||
+1
-1
@@ -24,7 +24,7 @@
|
||||
<!--导入属性配置 -->
|
||||
<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.replaceString" value="Test"/>
|
||||
</plugin>
|
||||
+1
-1
@@ -24,7 +24,7 @@
|
||||
<!--导入属性配置 -->
|
||||
<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"/>
|
||||
</plugin>
|
||||
|
||||
+1
-1
@@ -24,7 +24,7 @@
|
||||
<!--导入属性配置 -->
|
||||
<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"/>
|
||||
</plugin>
|
||||
|
||||
+1
-1
@@ -24,7 +24,7 @@
|
||||
<!--导入属性配置 -->
|
||||
<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="replaceString" value="DB1"/>
|
||||
<property name="suffixForMapper" value="Dao"/>
|
||||
Reference in New Issue
Block a user