TableRenameConfigurationPlugin

This commit is contained in:
hewei
2018-05-28 16:35:49 +08:00
parent 2878b98290
commit abc2ef16b0
11 changed files with 116 additions and 22 deletions
@@ -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) {
@@ -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")));
@@ -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>
@@ -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>
@@ -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>
@@ -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>
@@ -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>
@@ -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>
@@ -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"/>